Repository navigation
test(ci): fail when a package.json and its lockfile disagree - #345
Merged
Merged
Conversation
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.
4 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 cirefuses to install whenpackage.jsonandpackage-lock.jsondisagree. So a manifest edited without regenerating the lock does not fail where the mistake is — it fails inside whichever job installs first, saying:Nothing in that names the directory or the actual error. This now fails in the
testjob, 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 questionnpm ciasks, from a single checkout.The obvious alternative — fail when a PR touches
package.jsonwithout touchingpackage-lock.json— is worse on three counts: it needs a base ref, so it cannot run under a plaingo test; it does not run at all on a direct push tomain; 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:
@types/nodeto^99.0.0in the manifest, leave the lockleft-padtodependenciesonlyaxiosfrom the manifestsdks/c/package.jsonThat last one is why the directory list is written out rather than discovered. A discovered list would need to exclude
node_modulesitself; 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) anddocs-platform(#214), which arrived at lockfiles two releases apart and have the same exposure. Both pass today, and the three currently-open Dependabot PRs againstdocs-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 cianswers that, andsdk-metricsanddocs-siteboth run it. This is the cheap gate for the common case, not a replacement for the install.Also here
markdownFilesindocs_test.gonow calls thetrackedFileshelper this adds instead of carrying its own copy of thegit ls-filescall — #338 introduced that call one commit ago, and this needed the same set for a different pathspec.One lint finding, fixed rather than ignored:
G204fires onexec.CommandContextwith a variable pathspec, which it was not before the helper was shared. Suppressed with the reasoning inline — both callers pass a compile-time constant andexecbuilds argv with no shell — and the comment says what would make the suppression dishonest.Verification