From 0aa70edcec6de18281e097f47f36eaba85bb4a52 Mon Sep 17 00:00:00 2001 From: Apostolos Mantzaris Date: Tue, 3 Nov 2026 00:49:30 +0200 Subject: [PATCH] fix(lockfile): read the commit index Preserve the hook index only for staged lock reads. Cover every\ncommit form with a real hook and scope divergence notes to the\nstaged verdict. Refs #5207. --- CONTRIBUTING.md | 2 +- scripts/check-lockfile-sync.mjs | 13 ++-- scripts/lib/git-fixture-env.mjs | 8 +- tests/scripts/check-lockfile-sync.test.ts | 91 +++++++++++++++++++++++ 4 files changed, 103 insertions(+), 11 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8c51f8cd4..105ca2d91 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -22,7 +22,7 @@ npm test Pull requests must pass `npm run lint`. Run targeted test files for touched seams locally after `npm run build`; the full `npm test` suite is CI's job. CI also runs `npm run check:lockfile` and a production `--omit=dev` build (`npm run build:dist`), so keep `package-lock.json` in sync with `package.json`. -Regenerate the lockfile with the npm version `package.json`'s `packageManager` pins, never the npm on your PATH: npm older than the pin writes no `libc` field, and `npm run check:lockfile` refuses a change that drops one from a package that stays in the lock (#5207). The check judges what the commit records — the staged blob when git stages one, the working copy otherwise — and names the two sides it compared, so the pre-commit hook catches the drop before the commit exists. +Regenerate the lockfile with the npm version `package.json`'s `packageManager` pins, never the npm on your PATH: npm older than the pin writes no `libc` field, and `npm run check:lockfile` refuses a change that drops one from a package that stays in the lock (#5207). The check judges what the commit records — the blob in Git’s exported `GIT_INDEX_FILE` (including the temporary index for `-a`, path, `-o`, and `-i` commits), the default index outside a commit, and the working copy when that index has no lock change — and names the two sides it compared, so the pre-commit hook catches the drop before the commit exists. Source builds read the exact esbuild and TypeScript versions from the root entries in `package-lock.json`. Both esbuild bundles and the TypeScript fallback retain an isolated npm exec prefix and approve only the exact package spec they install. Keep the source lockfile available when running `build:dist`; missing or invalid tool versions fail the build rather than falling back to independent pins. diff --git a/scripts/check-lockfile-sync.mjs b/scripts/check-lockfile-sync.mjs index 85ef1e37f..288c95736 100644 --- a/scripts/check-lockfile-sync.mjs +++ b/scripts/check-lockfile-sync.mjs @@ -63,12 +63,13 @@ function read(file) { * to classify, and only the forms that mean "nothing committed to compare" may * be read as a skip (#5213 F2). */ -function git(args) { +function git(args, gitFixtureEnv) { try { return { text: String( gitExecFileSync(args, { cwd: process.cwd(), + gitFixtureEnv, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: GIT_TIMEOUT_MS, @@ -131,7 +132,9 @@ function readRefFile(ref, file) { * exactly that object (#5213 F1). */ function readStagedFile(file) { - const listed = git(["ls-files", "--stage", "--", file]); + const listed = git(["ls-files", "--stage", "--", file], { + keep: ["GIT_INDEX_FILE"], + }); if (listed.error) return { failure: gitFailureDetail(listed.error) }; const entry = listed.text.trim(); if (entry === "") return { absent: true }; @@ -279,19 +282,17 @@ function libcDriftOutcome(workingText, workingLock) { LOCKFILE, "HEAD", `HEAD → ${LOCKFILE}`, - note, ); const base = committedBase(headRef.sha); - if (base.unverifiable) return { unverifiable: base.unverifiable, note }; - if (base.skipped) return { skipped: base.skipped, note }; + if (base.unverifiable) return { unverifiable: base.unverifiable }; + if (base.skipped) return { skipped: base.skipped }; return judgeLibc( base.lock, headLock.lock, LOCKFILE, base.recordedBy, `${base.recordedBy} → HEAD`, - note, ); } diff --git a/scripts/lib/git-fixture-env.mjs b/scripts/lib/git-fixture-env.mjs index f7b521ed2..c1d718a1f 100644 --- a/scripts/lib/git-fixture-env.mjs +++ b/scripts/lib/git-fixture-env.mjs @@ -60,9 +60,9 @@ function fixtureGitConfig() { * through `GIT_CONFIG_KEY_` and no fixture needs a writable global config. * The wrappers keep passing the directory the command runs in. */ -export function envFor(_cwd, overrides) { +export function envFor(_cwd, overrides, { keep = [] } = {}) { const env = { ...process.env, ...overrides }; - for (const name of SCRUBBED) delete env[name]; + for (const name of SCRUBBED) if (!keep.includes(name)) delete env[name]; for (const key of Object.keys(env)) if (CONFIG_KV_PATTERN.test(key)) delete env[key]; env.GIT_CONFIG_GLOBAL = fixtureGitConfig(); @@ -74,7 +74,7 @@ export function gitExecFileSync(args, options = {}) { const cwd = options.cwd ?? process.cwd(); return execFileSync("git", args, { ...options, - env: envFor(cwd, options.env), + env: envFor(cwd, options.env, options.gitFixtureEnv), }); } @@ -86,6 +86,6 @@ export function gitExecSync(command, options = {}) { // pi-lens-ignore: ast-grep:unchecked-throwing-call return execSync(command, { ...options, - env: envFor(cwd, options.env), + env: envFor(cwd, options.env, options.gitFixtureEnv), }); } diff --git a/tests/scripts/check-lockfile-sync.test.ts b/tests/scripts/check-lockfile-sync.test.ts index fdf249c04..f11bfbfdf 100644 --- a/tests/scripts/check-lockfile-sync.test.ts +++ b/tests/scripts/check-lockfile-sync.test.ts @@ -463,6 +463,97 @@ describe("package-lock libc drift guard (#5207)", () => { return probeGit(["rev-parse", "HEAD"], root).trim(); } + // Recurrence: #5213 v2 F1, git prepares a commit-specific index. + it.each([ + ["plain", []], + ["all", ["-a"]], + ["path", ["package-lock.json"]], + ["only", ["-o", "package-lock.json"]], + ["include", ["-i", "package-lock.json"]], + ["amend", ["--amend"]], + ["merge", []], + ["outside", []], + ])("refuses a recorded libc drop through a real hook: %s", (form, flags) => { + const good = libcLock({ demo: GLYDE }); + const root = committedRepo(good); + const dropped = libcLock({ demo: { version: GLYDE.version } }); + if (form === "merge") { + probeGit(["checkout", "-qb", "side"], root); + fs.writeFileSync(path.join(root, "side"), "side"); + commit(root, "side"); + probeGit(["checkout", "fixture-lock"], root); + probeGit( + [ + "-c", + "user.name=fixture", + "-c", + "user.email=fixture@example.test", + "merge", + "--no-commit", + "--no-ff", + "side", + ], + root, + ); + } + const hook = path.join(root, ".git/hooks/pre-commit"); + fs.writeFileSync( + hook, + `#!/bin/sh\nexec '${process.execPath}' '${CHECKER}'\n`, + { mode: 0o755 }, + ); + if (["plain", "amend", "merge", "outside"].includes(form)) { + stage(root, dropped); + writeWorking(root, good); + } else { + stage(root, { ...good, requires: false }); + writeWorking(root, dropped); + } + const before = probeGit(["rev-parse", "HEAD"], root); + let output = ""; + try { + if (form === "outside") { + const result = run(CHECKER, root, [], envFor(root)); + expect(result.status).toBe(1); + output = result.stderr; + } else { + probeGit( + [ + "-c", + "user.name=fixture", + "-c", + "user.email=fixture@example.test", + "commit", + "-m", + "drop", + ...flags, + ], + root, + ); + } + } catch (error) { + expect((error as { status: number }).status).toBe(1); + output = String((error as { stderr: unknown }).stderr); + } + expect(output).toContain('dropped the "libc" field'); + expect(probeGit(["rev-parse", "HEAD"], root)).toBe(before); + expect( + JSON.parse(probeGit(["show", "HEAD:package-lock.json"], root)).packages[ + "node_modules/demo" + ].libc, + ).toEqual(["glibc"]); + }); + + it("does not claim a staged read when judging the working copy (#5213 F2)", () => { + const good = libcLock({ demo: GLYDE }); + const result = check( + committedRepo(good), + libcLock({ demo: { version: GLYDE.version } }), + ); + expect(result.status).toBe(1); + expect(result.stdout).not.toContain("the check reads the staged copy"); + }); + it("refuses the npm-9.2 shape and names the pinned regeneration", () => { const good = libcLock({ "@rollup/rollup-linux-x64-gnu": GLYDE }); const dropped = libcLock({