Skip to content

fix(pnpm): stop the lockfile carrying shard hosts, at write time - #77

Merged
stuffbucket merged 2 commits into
mainfrom
fix/pnpmfile-strip-hosts
Sep 7, 2026
Merged

stuffbucket merged 2 commits into
mainfrom
fix/pnpmfile-strip-hosts

Conversation

@stuffbucket

Copy link
Copy Markdown
Owner

Answers "can we just run strip on install?" — yes, and better than that: at
write time, so the hosts are never in the lockfile at all.

.pnpmfile.cjs drops the rotating ms-feed-N.pkgs.visualstudio.com tarball URLs
in afterAllResolved, which runs on the in-memory lockfile before pnpm
serializes it.

Why this beats the strip script

scripts/strip-lockfile-hosts.mjs can only repair a lockfile someone remembered
to run it against. Dependabot cannot be asked to run anything — every
dependency PR arrived with ~1720 host-pinned entries and failed CI; six were open
at once. A hook travels with the repository, so it applies to whoever resolves.

It also cannot be a postinstall hook, which is the obvious thing to reach for:
pnpm's check rejects a recorded host before lifecycle scripts run, so the
install is already dead by the time a hook would fire. afterAllResolved is
earlier than that — it is part of resolution, not a lifecycle script.

Scoped to the shard hosts deliberately. A tarball URL that is genuinely not
reconstructible — a git dependency, a GitHub release — is pinned there on
purpose, and blanking it would break resolution rather than harden it. Same rule
the script follows.

Verification

  • pnpm install --force over the whole workspace: drops 1740 URLs, lockfile
    byte-identical, zero hosts.
  • A subsequent pnpm run and a second forced install: unchanged.
  • verify:workspace passes; container suite 15/15, which exercises
    --frozen-lockfile with the hook present.

The single lockfile change is pnpmfileChecksum — pnpm hashing the hook so the
lockfile re-resolves if it is edited.

What I could not verify from here

That Dependabot's updater honours the pnpmfile. pnpm loads .pnpmfile.cjs
from the workspace root without configuration, so it should, but the proof is the
next dependency PR arriving clean.

Until that is observed, #76 (CI-side normalisation) is worth keeping as a
backstop — and its main-only assertion is what would tell us if this hook ever
stops applying. If the next Dependabot PR comes through clean, #76 becomes
belt-and-braces rather than load-bearing.

On strip vs the settings pnpm offers

Since it came up — they are not equivalent, and neither pnpm setting does what
this hook does:

this hook / strip trustLockfile lockfileIncludeTarballUrl
URLs in lockfile none present, rewritten every install unchanged — only drops reconstructible URLs, and ours are not
Diff noise per install none ~1700 lines ~1700 lines
Install fetches from derived registry URL the recorded shard the recorded shard
If that shard rotates away fine breaks breaks
Anti-tamper binding (pnpm/pnpm#12134) nothing to tamper with disabled intact

trustLockfile only silences the mismatch error; it leaves the churn and gives
up the binding. Only four shards answer anonymously, so a recorded one going away
is a live failure mode, not a theoretical one.

Comment thread .pnpmfile.cjs Fixed
`.pnpmfile.cjs` drops the rotating `ms-feed-N.pkgs.visualstudio.com`
tarball URLs in `afterAllResolved`, which runs on the in-memory lockfile
before pnpm serializes it. The hosts are therefore never written rather
than removed afterwards.

That distinction is the whole point. `scripts/strip-lockfile-hosts.mjs`
can only repair a lockfile someone remembered to run it against, and
Dependabot cannot be asked to run anything: every dependency PR arrived
with ~1720 host-pinned entries and failed CI, six of them open at once.
A hook travels with the repository, so it applies to whoever resolves.

It cannot be a `postinstall` hook, which is the obvious thing to reach
for: pnpm rejects a recorded host BEFORE lifecycle scripts run, so the
install is already dead by then. `afterAllResolved` is part of
resolution, which is earlier.

The pattern moves to `scripts/lockfile-shard-hosts.cjs`, shared with the
repair script. They had already drifted -- the hook matched the whole
`pkgs.visualstudio.com` domain while the script matched numbered shards
only -- and one of them would have kept a URL the other dropped. They
cannot share the edit, since one holds an object and the other holds
lines, so they share the only thing that must agree.

Tested, and two of these changed the code:

  fresh resolution from no lockfile   0 hosts, byte-identical to the
                                      committed file (~1740 without it)
  install --force                     0 hosts, lockfile unchanged
  add / update / dedupe / install     0 hosts
  frozen install, clean lockfile      passes
  frozen install, poisoned lockfile   STILL FAILS -- resolution is
                                      skipped so the hook never runs.
                                      CI must keep normalising before
                                      it installs.
  pnpmfile edited, not re-resolved    every frozen install fails with
                                      ERR_PNPM_LOCKFILE_CONFIG_MISMATCH
                                      until the lockfile is re-resolved
  lockfile with no `packages` map     warns instead of silently doing
                                      nothing
  `packages: null`                    threw, because typeof null is
                                      "object" -- would have failed
                                      every install. Now guarded.
  git and GitHub-release tarballs     kept, as before

verify:workspace passes and the container suite is 15/15. The one
lockfile change is `pnpmfileChecksum`.

Not verified from here: that Dependabot's updater honours the pnpmfile.
pnpm loads it from the workspace root without configuration, so it
should, but the proof is the next dependency PR arriving clean. The
frozen-install result above is why that does not matter much either way
-- CI normalises regardless.
The rule justified it by rotating hosts, which .pnpmfile.cjs now keeps
out of the lockfile at write time. The reason it is still required is
the one that was never written down: it fails when the lockfile
disagrees with the manifests, where a plain install silently re-resolves
and rewrites.

  specifiers in the lockfile don't match specifiers in package.json:
  * 1 dependencies were added: [email protected]

Confirmed both ways -- frozen refuses, plain install accepts and adds
the dependency to the lockfile.
@stuffbucket

Copy link
Copy Markdown
Owner Author

Tested the hook against the cases we need to cover. Two of them changed the code.

case result
fresh resolution from no lockfile 0 hosts, byte-identical to committed (~1740 without the hook)
install --force 0 hosts, lockfile unchanged
add / update / dedupe / install 0 hosts
frozen install, clean lockfile passes
frozen install, poisoned lockfile still fails — resolution is skipped, so the hook never runs
pnpmfile edited, not re-resolved every frozen install fails with ERR_PNPM_LOCKFILE_CONFIG_MISMATCH
lockfile with no packages map warns instead of silently doing nothing
packages: null threw — typeof null === "object", so it slipped the guard and Object.values(null) would have failed every install. Guarded now.
git / GitHub-release tarballs kept

Two fixes came out of that:

The null crash. Found only by testing the resilience path I'd added — the guard I wrote to make a shape change safe was itself the thing that would have broken installs.

A drift between the two removers. The hook matched the whole pkgs.visualstudio.com domain; the script matched numbered shards only. One would have kept a URL the other dropped. The pattern now lives in scripts/lockfile-shard-hosts.cjs, shared by both — they can't share the edit (one holds an object, the other holds lines), so they share the only thing that must agree.

On --frozen-lockfile

Since it came up: still needed, but not for the reason SOURCES.md gave. That rule cited rotating hosts, which this hook now prevents at write time. What it actually still buys is reproducibility:

specifiers in the lockfile don't match specifiers in package.json:
* 1 dependencies were added: [email protected]

A plain pnpm install silently re-resolves and rewrites instead. Rule updated to say that.

CI

#76 stays, and the frozen-install row above is why — it is load-bearing rather than a backstop. Its comments now attribute it to "a lockfile that arrives host-pinned" rather than to Dependabot specifically, since the hook covers anything resolved from this repo.

@stuffbucket
stuffbucket merged commit 6046b62 into main Sep 7, 2026
6 checks passed
@stuffbucket
stuffbucket deleted the fix/pnpmfile-strip-hosts branch September 7, 2026 07:38
stuffbucket added a commit that referenced this pull request Sep 7, 2026
Closes #26.

pnpm 11 defaults `verifyDepsBeforeRun` to `install`, so every `pnpm run`,
`pnpm test` and `pnpm --filter ... run ...` silently performs a full
install first. That install re-resolves, and re-resolution re-records the
rotating `ms-feed-N` tarball hosts -- all 1721 of them -- that
scripts/strip-lockfile-hosts.mjs exists to remove.

The effect is that stripping the lockfile and then running literally any
script put every host straight back, so the next --frozen-lockfile
install rejected the lockfile: the Docker test image, and CI. Working on
this repo meant re-stripping after almost every command, and forgetting
once produced a failure that reads like a dependency break rather than
like a stale lockfile.

`warn` keeps the check the default exists for -- it still reports a
node_modules that has drifted from the lockfile -- and does not write.

Verified: with the setting in place, `pnpm run verify:workspace`,
`pnpm --filter maximal-site run build`, `pnpm test` and a full container
suite all leave the lockfile byte-identical, with zero recorded hosts.
Without it, one `pnpm run` re-adds 1721.

Rebased after #77 landed, which changes what this is for. `.pnpmfile.cjs`
now drops the shard hosts as the lockfile is written, so an implicit
install no longer poisons anything and the acute failure is gone. The
setting is still worth having, for the smaller reason it was always also
true: a `pnpm run` should not resolve and rewrite the tree behind the
script it was asked to run. The prose in SOURCES.md and
pnpm-workspace.yaml says that rather than the pre-#77 rationale, and the
first of the two caveats below is now obsolete -- kept as the record of
why the hook exists.

Two things this does NOT fix, recorded so the next person does not go
looking:

  The tarball URLs themselves. The proxy serves tarballs from
  ms-feed-N.pkgs.visualstudio.com while the registry is
  packagefeedproxy.microsoft.io, and pnpm records a tarball URL whenever
  it cannot rebuild it from registry + name + version -- which it never
  can when the hosts differ. `lockfileIncludeTarballUrl` does not help:
  it only drops URLs that ARE reconstructible. There is no setting to
  disable the check. pnpm/pnpm#13619 reproduces this against this exact
  proxy and these exact hosts. (Superseded by #77: the pnpmfile hook
  drops them at write time instead.)

  The need to strip after a deliberate `pnpm install`. That is
  unchanged, and SOURCES.md already documents it. What changes is that
  it is now needed after an install rather than after every command.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants