Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(release): validate both manifests before writing either

Etienne Lescot73431d9f04b5
diff --git a/.github/scripts/set-release-version.mjs b/.github/scripts/set-release-version.mjs
index 0559e611b..24f6d2315 100644
--- a/.github/scripts/set-release-version.mjs
+++ b/.github/scripts/set-release-version.mjs
@@ -29,29 +29,35 @@ import { argv } from "node:process";
export function setReleaseVersion(version, dir) {
if (!version) throw new Error("a version is required");
- const edit = (name, mutate) => {
+ // Both manifests are read and validated before a single byte is written. The
+ // obvious order — write package.json, then validate the lockfile — left
+ // package.json bumped and the lockfile untouched whenever validation failed,
+ // which is precisely the half-bump this script exists to end. Its own error
+ // path must not reproduce the bug it fixes.
+ const load = (name) => {
const file = join(dir, name);
- const json = JSON.parse(readFileSync(file, "utf8"));
- mutate(json);
- writeFileSync(file, `${JSON.stringify(json, null, "\t")}\n`);
+ return { file, json: JSON.parse(readFileSync(file, "utf8")) };
};
- edit("package.json", (pkg) => {
- pkg.version = version;
- });
+ const pkg = load("package.json");
+ const lock = load("package-lock.json");
+
+ // lockfileVersion 3 repeats the root version inside packages[""]. Optional
+ // chaining would quietly skip it if the shape ever changed — the same
+ // silent half-bump this script exists to end — so demand it instead.
+ if (!lock.json.packages?.[""]) {
+ throw new Error(
+ 'package-lock.json has no packages[""] entry; the lockfile format changed and this script needs updating',
+ );
+ }
+
+ pkg.json.version = version;
+ lock.json.version = version;
+ lock.json.packages[""].version = version;
- edit("package-lock.json", (lock) => {
- lock.version = version;
- // lockfileVersion 3 repeats the root version inside packages[""]. Optional
- // chaining would quietly skip it if the shape ever changed — the same
- // silent half-bump this script exists to end — so demand it instead.
- if (!lock.packages?.[""]) {
- throw new Error(
- 'package-lock.json has no packages[""] entry; the lockfile format changed and this script needs updating',
- );
- }
- lock.packages[""].version = version;
- });
+ for (const { file, json } of [pkg, lock]) {
+ writeFileSync(file, `${JSON.stringify(json, null, "\t")}\n`);
+ }
}
// Only run when invoked directly, so the test can import the function.
diff --git a/.github/scripts/set-release-version.test.mjs b/.github/scripts/set-release-version.test.mjs
index baf80a8cb..5e7a15619 100644
--- a/.github/scripts/set-release-version.test.mjs
+++ b/.github/scripts/set-release-version.test.mjs
@@ -1,4 +1,5 @@
-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { execFileSync } from "node:child_process";
+import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
@@ -107,9 +108,34 @@ describe("setReleaseVersion", () => {
);
expect(() => setReleaseVersion("1.9.0", dir)).toThrow(/packages/);
+
+ // The throw alone was never the property this test claims. package.json
+ // used to be written before the lockfile was validated, so this case left
+ // exactly the half-bump the name promises it prevents.
+ expect(JSON.parse(read("package.json")).version).toBe("1.8.0");
+ expect(read("package-lock.json")).not.toContain("1.9.0");
});
it("requires a version", () => {
expect(() => setReleaseVersion("", dir)).toThrow(/version is required/);
});
+
+ // The workflows invoke this with a repository-relative path, and the
+ // direct-invocation guard compares against `import.meta.filename`, which is
+ // absolute. Node resolves argv[1] before exposing it, so the two match — but
+ // nothing pinned that, and the whole script is dead code if it ever stops
+ // being true. Invoked here the way promote.yml and prerelease.yml do.
+ it("runs when invoked directly through a relative path", () => {
+ const scripts = join(dir, "scripts");
+ mkdirSync(scripts);
+ copyFileSync(
+ join(import.meta.dirname, "set-release-version.mjs"),
+ join(scripts, "set-release-version.mjs"),
+ );
+
+ execFileSync("node", ["scripts/set-release-version.mjs", "1.9.0"], { cwd: dir });
+
+ expect(JSON.parse(read("package.json")).version).toBe("1.9.0");
+ expect(JSON.parse(read("package-lock.json")).packages[""].version).toBe("1.9.0");
+ });
});