Skip to content

Commit a147da4

Browse files
committed
fix(scripts): release helper rejects versions that would move a channel backwards
Preflight only checked the exact version was unused, so a target cut from a dev branch whose version line trails main (dev 2.18.0 vs npm latest 2.19.0) could publish an obsolete version over the channel tip. The preflight now reads npm dist-tags and requires the proposed version to be strictly newer than the channel's current version (semver-ordered, preview channel compared against preview).
1 parent cc8e5a3 commit a147da4

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎scripts/release.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,65 @@ async function githubReleaseExists(tagName: string): Promise<boolean> {
154154
process.exit(1);
155155
}
156156

157+
/** Order two semver strings per the semver.org rules (numeric identifiers numerically,
158+
* numeric < alphanumeric prerelease, prerelease < release). Returns negative/0/positive. */
159+
export function compareReleaseVersions(left: string, right: string): number {
160+
const parse = (value: string) => {
161+
const [main, ...preParts] = value.split("-");
162+
const nums = (main ?? "").split(".").map(part => Number(part));
163+
return { nums, pre: preParts.length ? preParts.join("-").split(".") : null };
164+
};
165+
const a = parse(left);
166+
const b = parse(right);
167+
for (let i = 0; i < 3; i += 1) {
168+
const delta = (a.nums[i] ?? 0) - (b.nums[i] ?? 0);
169+
if (delta !== 0) return delta;
170+
}
171+
if (a.pre === null && b.pre === null) return 0;
172+
if (a.pre === null) return 1;
173+
if (b.pre === null) return -1;
174+
const len = Math.max(a.pre.length, b.pre.length);
175+
for (let i = 0; i < len; i += 1) {
176+
const x = a.pre[i];
177+
const y = b.pre[i];
178+
if (x === undefined) return -1;
179+
if (y === undefined) return 1;
180+
const xn = /^\d+$/.test(x) ? Number(x) : null;
181+
const yn = /^\d+$/.test(y) ? Number(y) : null;
182+
if (xn !== null && yn !== null && xn !== yn) return xn - yn;
183+
if (xn !== null && yn === null) return -1;
184+
if (xn === null && yn !== null) return 1;
185+
if (xn === null && yn === null && x !== y) return x < y ? -1 : 1;
186+
}
187+
return 0;
188+
}
189+
190+
/** The proposed version must move its npm channel FORWARD: an unused-but-obsolete
191+
* target (e.g. cut from a dev branch whose version line trails main) would otherwise
192+
* pass the unused-version check and publish a regression over the channel tip. */
193+
async function assertChannelVersionMovesForward(packageName: string, version: string, channel: string): Promise<void> {
194+
const result = await runQuiet(["npm", "view", packageName, "dist-tags", "--json"]);
195+
if (result.exitCode !== 0) {
196+
console.error(`✗ failed to read npm dist-tags for ${packageName}`);
197+
if (result.stderr) console.error(result.stderr);
198+
process.exit(1);
199+
}
200+
let distTags: Record<string, string>;
201+
try {
202+
distTags = JSON.parse(result.stdout) as Record<string, string>;
203+
} catch {
204+
console.error(`✗ npm dist-tags response for ${packageName} was not JSON`);
205+
process.exit(1);
206+
}
207+
const current = distTags[channel];
208+
if (!current) return; // channel not published yet — nothing to regress
209+
if (compareReleaseVersions(version, current) <= 0) {
210+
console.error(`✗ release version ${version} does not move the '${channel}' channel forward (current: ${current}).`);
211+
console.error("Reconcile the version line first: dev's package.json may trail the latest release; pick a version strictly newer than the channel tip.");
212+
process.exit(1);
213+
}
214+
}
215+
157216
async function assertUnusedReleaseVersion(packageName: string, version: string): Promise<void> {
158217
const releaseTag = `v${version}`;
159218
const [npmUsed, tagSha, releaseUsed] = await Promise.all([
@@ -298,6 +357,7 @@ if ((await capture(["git", "status", "--porcelain"])).trim()) { console.error("
298357
const packageName = await readPackageName();
299358
console.log(`→ release metadata preflight (${packageName}@${version})`);
300359
await assertUnusedReleaseVersion(packageName, version);
360+
await assertChannelVersionMovesForward(packageName, version, tag);
301361
console.log("→ dependency audit");
302362
await runLoud(["bun", "run", "audit:high"]);
303363
console.log("→ typecheck");

‎tests/release-helper.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ interface LoggedCall {
1818

1919
interface ReleaseScenario {
2020
branch?: string;
21+
npmLatest?: string;
22+
npmPreview?: string;
2123
headSha?: string;
2224
remoteHeadSha?: string;
2325
privacyExitCode?: number;
@@ -105,6 +107,14 @@ process.exit(1);
105107
const args = process.argv.slice(2);
106108
appendFileSync(process.env.FAKE_RELEASE_LOG, JSON.stringify({ name: "npm", args }) + "\\n");
107109
110+
if (args[0] === "view" && args.includes("dist-tags")) {
111+
process.stdout.write(JSON.stringify({
112+
latest: process.env.FAKE_NPM_LATEST ?? "0.0.1",
113+
preview: process.env.FAKE_NPM_PREVIEW ?? "0.0.1-preview.0",
114+
}) + "\\n");
115+
process.exit(0);
116+
}
117+
108118
if (args[0] === "view") {
109119
console.error("npm ERR! code E404");
110120
process.exit(1);
@@ -216,6 +226,8 @@ function runRelease(version: string, scenario: ReleaseScenario = {}) {
216226
FAKE_BUN_TSC_EXIT_CODE: String(scenario.typecheckExitCode ?? 0),
217227
FAKE_BUN_TEST_EXIT_CODE: String(scenario.testExitCode ?? 0),
218228
FAKE_BUN_PRIVACY_EXIT_CODE: String(scenario.privacyExitCode ?? 0),
229+
...(scenario.npmLatest ? { FAKE_NPM_LATEST: scenario.npmLatest } : {}),
230+
...(scenario.npmPreview ? { FAKE_NPM_PREVIEW: scenario.npmPreview } : {}),
219231
},
220232
encoding: "utf8",
221233
});
@@ -255,6 +267,28 @@ describe("release helper", () => {
255267
expect(dispatchIndex).toBeGreaterThan(versionIndex);
256268
});
257269

270+
test("an obsolete version that would move latest backwards aborts before the bump", () => {
271+
const { calls, result } = runRelease("9.9.8", { npmLatest: "9.9.9" });
272+
273+
expect(result.status).not.toBe(0);
274+
expect(result.stderr ?? "").toContain("does not move the 'latest' channel forward");
275+
expect(findCallIndex(calls, "npm", call => call.args[0] === "version")).toBe(-1);
276+
expect(findCallIndex(calls, "git", call => call.args[0] === "commit")).toBe(-1);
277+
});
278+
279+
test("a version newer than the channel tip passes the forward guard", () => {
280+
const { calls, result } = runRelease("9.9.10", { npmLatest: "9.9.9" });
281+
282+
expect(`${result.status}\n${result.stderr ?? ""}`.trim()).toBe("0");
283+
expect(findCallIndex(calls, "npm", call => call.args.join(" ") === "version 9.9.10 --no-git-tag-version")).toBeGreaterThanOrEqual(0);
284+
});
285+
286+
test("preview releases compare against the preview channel, not latest", () => {
287+
const { result } = runRelease("9.9.9-preview.2", { branch: "preview", npmLatest: "10.0.0", npmPreview: "9.9.9-preview.1" });
288+
289+
expect(`${result.status}\n${result.stderr ?? ""}`.trim()).toBe("0");
290+
});
291+
258292
test("failed privacy scan aborts before version bump, commit, and push", () => {
259293
const { calls, result } = runRelease("9.9.9", { privacyExitCode: 1 });
260294

0 commit comments

Comments
 (0)