Repository navigation
fix(release): stop dev from carrying a version behind its own releases #2739
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e4a85d1
fix(release): stop dev from carrying a version behind its own releases
lidge-jun a223a2b
fix(release): make the version-line guard bite on release commits and…
lidge-jun 7297bc8
ci: fetch tags without cloning full history for the version-line check
lidge-jun 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
Some comments aren't visible on the classic Files Changed page.
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
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,134 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { readFileSync } from "node:fs"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { compareReleaseTags } from "../scripts/release-notes"; | ||
|
|
||
| const repoRoot = fileURLToPath(new URL("../", import.meta.url)); | ||
|
|
||
| /** | ||
| * The in-tree version must never sit BEHIND a version this repository has already | ||
| * released. | ||
| * | ||
| * This is not hypothetical. dev accumulated 254 commits without ever touching | ||
| * package.json, so its version string (2.32.1-preview.20260825) fell behind the | ||
| * published latest dist-tag (2.33.0) once v2.33.0 was cut from main. One stale line, | ||
| * two distinct failure modes: | ||
| * | ||
| * - assertChannelVersionMovesForward in scripts/release.ts refuses the string as a | ||
| * channel regression, so a release cut from that tree cannot publish at all. | ||
| * - merging dev into main resolves package.json to main's side, producing a tree that | ||
| * claims to be the ALREADY-PUBLISHED 2.33.0 - a silent duplicate instead of a loud | ||
| * failure. | ||
| * | ||
| * Commit 32529c2b2 repaired precisely this by hand once, and nothing has enforced it | ||
| * since. The assertion reads the local tag set rather than the npm registry, so it needs | ||
| * no network and no edit at each release. | ||
| * | ||
| * compareReleaseTags comes from scripts/release-notes and not from scripts/release: the | ||
| * latter parses process.argv and calls process.exit at module scope, so importing it from | ||
| * a test kills the runner. release-notes guards its CLI behind import.meta.main. | ||
| */ | ||
|
|
||
| /** Release tags shaped vX.Y.Z[-pre]. Non-release tags are ignored, not an error. */ | ||
| const RELEASE_TAG = /^v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/; | ||
|
|
||
| function git(args: string[]): { ok: boolean; out: string } { | ||
| const result = Bun.spawnSync(["git", ...args], { cwd: repoRoot }); | ||
| return { | ||
| ok: result.exitCode === 0, | ||
| out: new TextDecoder().decode(result.stdout).trim(), | ||
| }; | ||
| } | ||
|
|
||
| function localReleaseTags(): string[] { | ||
| const result = git(["tag", "--list", "v*"]); | ||
| // A tarball install or a missing git binary lands here. Absent tags cannot prove a | ||
| // regression, so the check reports nothing rather than inventing a failure. | ||
| if (!result.ok) return []; | ||
| return result.out | ||
| .split("\n") | ||
| .map((line) => line.trim()) | ||
| .filter((line) => RELEASE_TAG.test(line)); | ||
| } | ||
|
|
||
| function inTreeVersion(): string { | ||
| const raw = readFileSync(new URL("../package.json", import.meta.url), "utf8"); | ||
| const version = (JSON.parse(raw) as { version?: unknown }).version; | ||
| if (typeof version !== "string") throw new Error("package.json has no string version"); | ||
| return version; | ||
| } | ||
|
|
||
| /** Newest tag by SemVer precedence, or null when the set is empty. */ | ||
| function highestReleaseTag(tags: string[]): string | null { | ||
| if (tags.length === 0) return null; | ||
| const sorted = [...tags].sort(compareReleaseTags); | ||
| return sorted[sorted.length - 1] ?? null; | ||
| } | ||
|
|
||
| /** | ||
| * True when the given tag names the commit under test. | ||
| * | ||
| * This is the difference between "equal is legal" and "equal is a duplicate". On a | ||
| * release commit — main at v2.33.0, preview at v2.33.0-preview.20260825 — package.json | ||
| * SHOULD equal the highest tag, and a test that demanded strictly-ahead everywhere would | ||
| * turn every released commit red. On any other commit, equal means the tree claims a | ||
| * version that is already published. | ||
| */ | ||
| function tagPointsAtHead(tag: string): boolean { | ||
| const head = git(["rev-parse", "HEAD^{commit}"]); | ||
| const tagged = git([`rev-parse`, `${tag}^{commit}`]); | ||
| return head.ok && tagged.ok && head.out.length > 0 && head.out === tagged.out; | ||
| } | ||
|
|
||
| describe("release version line", () => { | ||
| test("package.json carries a parseable SemVer version", () => { | ||
| expect(inTreeVersion()).toMatch(/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/); | ||
| }); | ||
|
|
||
| test("the in-tree version is never behind a released one", () => { | ||
| const tags = localReleaseTags(); | ||
| // A checkout with no tags cannot answer the question. CI fetches tags explicitly for | ||
| // the jobs that run this suite (see .github/workflows/ci.yml), so an empty set here | ||
| // means a tarball or a hand-made shallow clone, not a silently unprotected pipeline. | ||
| if (tags.length === 0) return; | ||
|
|
||
| const version = inTreeVersion(); | ||
| const highest = highestReleaseTag(tags); | ||
| expect(highest).not.toBeNull(); | ||
|
|
||
| const ordering = compareReleaseTags("v" + version, highest!); | ||
| if (ordering === 0) { | ||
| // Legal only on the release commit that tag names. | ||
| expect( | ||
| tagPointsAtHead(highest!), | ||
| "package.json version " + version + " equals release tag " + highest + | ||
| ", but this commit is not the one that tag names. The tree claims an " + | ||
| "already-published version: publishing is refused as a duplicate. Bump " + | ||
| "package.json.", | ||
| ).toBe(true); | ||
| return; | ||
| } | ||
|
|
||
| expect( | ||
| ordering, | ||
| "package.json version " + version + " is BEHIND the highest release tag " + | ||
| highest + | ||
| ". A release cut from this tree is rejected as a channel regression, and " + | ||
| "merging into main resolves package.json to main's side and silently " + | ||
| "republishes. Bump package.json.", | ||
| ).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test("a version behind the tag set is detected rather than tolerated", () => { | ||
| // Proves the comparison above is not vacuous: the same helper, given the exact string | ||
| // dev actually carried, must order it BEHIND the release that stranded it. | ||
| expect(compareReleaseTags("v2.32.1-preview.20260825", "v2.33.0")).toBeLessThan(0); | ||
| expect(compareReleaseTags("v2.33.0", "v2.33.0")).toBe(0); | ||
| expect(compareReleaseTags("v2.34.0", "v2.33.0")).toBeGreaterThan(0); | ||
| // A prerelease of a FUTURE version is ahead, not behind: dev may legitimately carry | ||
| // 2.35.0-preview.1 while v2.34.0 is the newest tag. | ||
| expect(compareReleaseTags("v2.35.0-preview.1", "v2.34.0")).toBeGreaterThan(0); | ||
| // ...but a prerelease of the SAME core is behind its own stable release. | ||
| expect(compareReleaseTags("v2.34.0-preview.1", "v2.34.0")).toBeLessThan(0); | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Use a strict SemVer validator.
The regex accepts
01.2.3,2.34.0-01, and2.34.0-alpha..1, although these are not valid SemVer. The test can therefore pass an invalidpackage.jsonversion. Reuse a strict shared parser, preferably the parser used bycompareReleaseTagsinscripts/release-notes.ts(Lines 66-79), instead of validating only the character shape.🤖 Prompt for AI Agents