chore: add pre-push hook guarding yarn.lock drift - #1220
Martin Hochel (Hotell) wants to merge 1 commit into
Conversation
CI runs `yarn install --immutable`, but that check only ever inspects the working tree. A lockfile that was regenerated locally and never committed therefore passes locally and fails on CI, which is exactly what happened in microsoft#1219. The hook closes both gaps: - `git diff --quiet HEAD -- yarn.lock` rejects a lockfile that differs from the commit being pushed (staged or unstaged) - `yarn install --immutable` rejects a manifest change, including peerDependencies, with no matching lockfile update Both are no-ops when everything is in sync.
📋 PR Validation SummaryCheck the Build react library job summary for detailed reports:
|
There was a problem hiding this comment.
Pull request overview
Adds a Husky pre-push hook to prevent pushing when yarn.lock has drifted locally (including the “lockfile regenerated but not committed” case that yarn install --immutable alone won’t catch), aligning local checks with CI expectations.
Changes:
- Add
.husky/pre-pushhook that blocks pushes whenyarn.lockdiffers fromHEAD. - Run
yarn install --immutableduringpre-pushto catch manifest/lockfile mismatches before pushing.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| fi | ||
|
|
||
| # Catches a manifest change (including peerDependencies) with no matching lockfile update. | ||
| yarn install --immutable |
There was a problem hiding this comment.
Could an uncommitted package.json change hide a mismatch in the commit here?
For example, if I commit a dependency change without updating yarn.lock, then undo the manifest change locally without committing, both checks seem to pass while CI would still fail.
Would it be worth checking the root and workspace manifests against HEAD too?
Problem
CI runs
yarn install --immutable, but that check only ever inspects the working tree.A lockfile that was regenerated locally and never committed therefore passes locally and
fails on CI.
That is exactly what happened in #1219:
peerDependencieswere edited afteryarn add,the lockfile was regenerated at some later point but the commit kept the stale copy, and
a local
yarn install --immutablestill reported success because it was validating therepaired working tree rather than the commit.
The gap
Two distinct failure modes exist, and
--immutableonly covers one:--immutable?Change
A
.husky/pre-pushhook covering both:The git check runs first because it is essentially free;
--immutableis a read-onlyno-op when the tree is already in sync, so the hook adds no meaningful cost to a normal push.
Note that
git status --porcelainis unsuitable here: it exits0even when it printsoutput, so it cannot gate a hook.
git diff --quiet HEADreturns non-zero and covers bothstaged and unstaged drift.
Verification
yarn.lockmodified but uncommittedyarn.lock differs from HEADpeerDependenciesadded with stale lockYN0028This PR's own push exercised the hook.