Repository navigation
Publish workspace ranges as real versions #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@codelitdev/platform": patch | ||
| --- | ||
|
|
||
| Declare the optional `@codelitdev/billing` peer as `>=0.4.0`. 0.4.0 published it as `workspace:*`, which npm refuses to install. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
docs/decisions/0015-resolve-workspace-ranges-before-publishing.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # ADR 0015: Resolve workspace ranges before publishing | ||
|
|
||
| Status: accepted | ||
| Date: 2026-10-09 | ||
|
|
||
| ## Context | ||
|
|
||
| Packages in this repository depend on each other with `workspace:` ranges. | ||
| `changeset publish` publishes with `npm publish`, which copies a manifest | ||
| as committed and does not rewrite `workspace:` ranges, unlike Bun and pnpm. | ||
| Until 0.4.0 these ranges appeared only in `devDependencies`, which npm | ||
| never installs for consumers. 0.4.0 published `@codelitdev/platform` with | ||
| an optional peer of `workspace:*`, and npm refuses to install it | ||
| (`EUNSUPPORTEDPROTOCOL`). Bun installs it, and `bun pm pack` rewrites the | ||
| range, so testing with packed tarballs did not catch it. | ||
|
|
||
| ## Decision | ||
|
|
||
| - Ranges that consumers read (`dependencies`, `peerDependencies`, | ||
| `optionalDependencies`) on another `@codelitdev/*` package are written as | ||
| real ranges, such as `>=0.4.0`. Changesets keeps those ranges up to date | ||
| when versions change. | ||
| - The `release` script runs `scripts/resolve-workspace-ranges.mjs` after | ||
| `verify` and before `changeset publish`. It rewrites every `workspace:` | ||
| range left in a public package's manifest to a real version: | ||
| - `workspace:^` becomes `^<version>` | ||
| - `workspace:~` becomes `~<version>` | ||
| - `workspace:*` becomes `<version>` | ||
| - `workspace:<range>` becomes `<range>` | ||
| - The rewrite happens only on the release job's checkout, which is thrown | ||
| away. It is never committed, and the release PR keeps `workspace:` ranges. | ||
| - Workspace patterns are expanded one folder level at a time. A level is a | ||
| literal name or `*`; any other pattern stops the release, so a public | ||
| package is never skipped. | ||
|
|
||
| ## Consequences | ||
|
|
||
| - A published manifest differs from the committed one only in its | ||
| `workspace:` ranges. Today those are only in `devDependencies`. | ||
| - A `workspace:` range written by mistake in a consumer-facing field still | ||
| publishes something npm can install. `workspace:*` becomes an exact | ||
| version, which is stricter than intended for a peer. | ||
| - Publishing by hand needs the same script first; `npm publish` alone | ||
| reintroduces the problem. | ||
| - A new workspace pattern that uses other wildcards needs the script | ||
| changed before the next release. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Rewrites `workspace:` dependency ranges in public packages to real version | ||
| // ranges before `changeset publish`. Changesets publishes with `npm publish`, | ||
| // which copies `workspace:^` into the published package.json as is, and npm | ||
| // cannot install that. Runs on CI's throwaway checkout only. | ||
| // See docs/decisions/0015-resolve-workspace-ranges-before-publishing.md. | ||
| // | ||
| // workspace:^ -> ^<version>, workspace:~ -> ~<version>, | ||
| // workspace:* -> <version>, workspace:<range> -> <range> | ||
| import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const root = | ||
| process.argv[2] || path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); | ||
| const rootManifest = JSON.parse(readFileSync(path.join(root, "package.json"), "utf8")); | ||
|
|
||
| // Expands a workspace pattern one path segment at a time. A segment is either | ||
| // a literal name or `*` (every directory at that level). Anything else throws, | ||
| // so a pattern this script cannot read never skips a public package silently. | ||
| function expand(pattern) { | ||
| let directories = [root]; | ||
| for (const segment of pattern.split("/")) { | ||
| if (segment === "*") { | ||
| directories = directories.flatMap((directory) => | ||
| readdirSync(directory, { withFileTypes: true }) | ||
| .filter((entry) => entry.isDirectory() && entry.name !== "node_modules") | ||
| .map((entry) => path.join(directory, entry.name)), | ||
| ); | ||
| } else if (/[*?[\]{}()!|+]/.test(segment)) { | ||
| throw new Error(`Unsupported workspace pattern: ${pattern}`); | ||
| } else { | ||
| directories = directories | ||
| .map((directory) => path.join(directory, segment)) | ||
| .filter((directory) => existsSync(directory)); | ||
| } | ||
| } | ||
| return directories | ||
| .map((directory) => path.join(directory, "package.json")) | ||
| .filter((file) => existsSync(file)); | ||
| } | ||
|
|
||
| const manifests = rootManifest.workspaces.flatMap(expand).map((file) => ({ | ||
| file, | ||
| json: JSON.parse(readFileSync(file, "utf8")), | ||
| })); | ||
| const versions = new Map(manifests.map(({ json }) => [json.name, json.version])); | ||
|
|
||
| function resolve(name, range) { | ||
| const version = versions.get(name); | ||
| if (!version) throw new Error(`No workspace package named ${name}`); | ||
| const spec = range.slice("workspace:".length); | ||
| if (spec === "^" || spec === "~") return `${spec}${version}`; | ||
| if (spec === "*") return version; | ||
| return spec; | ||
| } | ||
|
|
||
| const fields = [ | ||
| "dependencies", | ||
| "peerDependencies", | ||
| "optionalDependencies", | ||
| "devDependencies", | ||
| ]; | ||
| for (const { file, json } of manifests) { | ||
| if (json.private) continue; | ||
| let changed = false; | ||
| for (const field of fields) { | ||
| for (const [name, range] of Object.entries(json[field] || {})) { | ||
| if (typeof range !== "string" || !range.startsWith("workspace:")) continue; | ||
| json[field][name] = resolve(name, range); | ||
| console.log(`${json.name}: ${name} ${range} -> ${json[field][name]}`); | ||
| changed = true; | ||
| } | ||
| } | ||
| if (changed) writeFileSync(file, `${JSON.stringify(json, null, 2)}\n`); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.