Skip to content

test(ci): fail when a package.json and its lockfile disagree - #345

Merged
scttfrdmn merged 1 commit into
mainfrom
ci/lockfile-sync-gate
Aug 4, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
ci/lockfile-sync-gate

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #332 — its fourth checkbox, the one it calls "the durable fix." The other three were cleanup of a queue that has since drained: #310 merged, #309 was closed and superseded by #335, and every npm PR opened before the lockfile has been re-run or closed.

The failure this replaces

npm ci refuses to install when package.json and package-lock.json disagree. So a manifest edited without regenerating the lock does not fail where the mistake is — it fails inside whichever job installs first, saying:

npm error `npm ci` can only install packages when your package.json and package-lock.json are in sync.
npm error Invalid: lock file's @types/[email protected] does not satisfy @types/[email protected]

Nothing in that names the directory or the actual error. This now fails in the test job, in under a second, before anything installs.

Why compare the lockfile's root entry rather than diff the PR's files

packages[""] is npm's record of the root project, and npm writes the manifest's own ranges into it verbatim. Comparing against it asks the same question npm ci asks, from a single checkout.

The obvious alternative — fail when a PR touches package.json without touching package-lock.json — is worse on three counts: it needs a base ref, so it cannot run under a plain go test; it does not run at all on a direct push to main; and it passes a hand edit that touches both files without regenerating the tree, which is a mismatch it exists to catch.

Mutation-checked, not predicted

Each direction was verified by breaking the real tree and watching the failure, per the standing practice on this repository:

mutation result
bump @types/node to ^99.0.0 in the manifest, leave the lock fails: "requires @types/node@^99.0.0 but the lockfile was generated against @types/node@^26.1.2"
add left-pad to dependencies only fails: "the lockfile does not record it at all"
delete axios from the manifest fails: "the lockfile records axios@^1.4.0 but package.json no longer requires it, so the lockfile predates a removal"
add an unlisted sdks/c/package.json fails: "not in npmDirectories, so nothing checks it against a lockfile"

That last one is why the directory list is written out rather than discovered. A discovered list would need to exclude node_modules itself; a named list plus a completeness check fails loudly instead of quietly skipping a directory nobody noticed.

Scope

Both npm directories are covered — sdks/javascript (#201) and docs-platform (#214), which arrived at lockfiles two releases apart and have the same exposure. Both pass today, and the three currently-open Dependabot PRs against docs-platform (#339, #340, #341) each carry both files, which is Dependabot behaving correctly now that a lockfile exists for it to regenerate.

What this does not check is the resolved tree below the root: whether the locked versions satisfy the ranges, and whether every transitive dependency is present. Only npm ci answers that, and sdk-metrics and docs-site both run it. This is the cheap gate for the common case, not a replacement for the install.

Also here

markdownFiles in docs_test.go now calls the trackedFiles helper this adds instead of carrying its own copy of the git ls-files call — #338 introduced that call one commit ago, and this needed the same set for a different pathspec.

One lint finding, fixed rather than ignored: G204 fires on exec.CommandContext with a variable pathspec, which it was not before the helper was shared. Suppressed with the reasoning inline — both callers pass a compile-time constant and exec builds argv with no shell — and the comment says what would make the suppression dishonest.

Verification

go test ./internal/config/ -count=1 -race                                    ok
golangci-lint run --new-from-merge-base=origin/main --timeout=10m            0 issues
npx markdownlint-cli CHANGELOG.md                                            361 (baseline)

Closes #332.

`npm ci` refuses to install on a mismatch, so a manifest edited without
regenerating the lock surfaced as an npm error deep inside whichever job
installed first — phrased as a version-satisfaction complaint that names
neither the directory nor the mistake. #332 hit exactly that when
Dependabot PRs predating the lockfile edited only the manifest.

TestNPMLockfilesAgreeWithTheirManifests compares each manifest four
dependency tables against the same tables in the lockfile packages[""]
entry, which is npm own record of what the manifest said when the lock
was generated. That makes the check answerable from a single checkout:
a changed-files diff needs a base ref, does not work under go test, and
passes a hand edit touching both files without regenerating the tree.

Three mismatch directions, all mutation-checked against the real tree
rather than predicted: a range the lock does not record, a range recorded
at a different version, and a package the lock still records after the
manifest dropped it. A fourth subtest globs git ls-files for tracked
package.json and fails on a directory not in npmDirectories, so a third
npm directory cannot arrive without a gate; also mutation-checked.

markdownFiles now calls the shared trackedFiles helper this adds, rather
than carrying its own copy of the git ls-files call.
@scttfrdmn scttfrdmn added type: ci-cd Continuous integration or deployment improvements priority: medium Important but not urgent area: ci-cd CI workflows, gates, and release automation area: sdk Client SDKs (Python, Java, JavaScript) labels Aug 4, 2026
@scttfrdmn scttfrdmn added the effort: small Small effort - 2-4 hours label Aug 4, 2026
@scttfrdmn
scttfrdmn merged commit 5385b2d into main Aug 4, 2026
30 checks passed
@scttfrdmn
scttfrdmn deleted the ci/lockfile-sync-gate branch August 4, 2026 04:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ci-cd CI workflows, gates, and release automation area: sdk Client SDKs (Python, Java, JavaScript) effort: small Small effort - 2-4 hours priority: medium Important but not urgent type: ci-cd Continuous integration or deployment improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Guard against a package.json change without a matching package-lock.json, so npm ci cannot fail late

1 participant