Skip to content

fix(deps-restorer): skip post-import linking for warm GVS packages - #11020

Closed
vsumner wants to merge 3 commits into
pnpm:mainfrom
vsumner:fix/gvs-skip-linking-warm-packages
Closed

vsumner wants to merge 3 commits into
pnpm:mainfrom
vsumner:fix/gvs-skip-linking-warm-packages

Conversation

@vsumner

@vsumner vsumner commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When the Global Virtual Store is backed by a read-only content-addressable store (e.g. Nix), post-import operations that write into the package directory fail:

  • Self-dep symlinking creates a symlink at <pkg>/node_modules/<name>
  • Bin linking runs fs.mkdir(<pkg>/node_modules/.bin)

Other install operations succeed because they either target the writable project-local node_modules, or skip via safeToSkip when the package already exists.

Solution

In GVS mode, skip these operations when the package wasn't freshly imported and the target already exists on disk. Freshly imported packages are always writable, so linking proceeds unconditionally. Warm packages that already have their bins and self-dep symlinks in place are skipped. Warm packages that are missing these targets (e.g. populated by pnpm fetch with virtualStoreOnly, which skips post-import linking) still get linked.

This uses a freshlyImported flag on DependenciesGraphNode (set from importMethod) combined with existsSync checks on the target paths.

No try/catch, no error suppression, no new options on downstream packages.

Follows #10965 (virtualStoreOnly), #10709 (GVS warm reattach). Supersedes #11017.

Changes

@pnpm/deps.graph-builder — add freshlyImported?: boolean to DependenciesGraphNode

@pnpm/installing.deps-restorer:

  • Set depNode.freshlyImported = importMethod != null after importPackage
  • Self-dep: skip when enableGlobalVirtualStore && !freshlyImported && symlink exists
  • Bin linking: skip when enableGlobalVirtualStore && !freshlyImported && .bin dir exists

Verification

  • @pnpm/deps.graph-builder: compiles clean
  • @pnpm/installing.deps-restorer: compiles clean
  • Pre-push hooks (spellcheck, meta-updater, eslint): all pass
  • No test changes: linkAllPkgs and linkAllBins are private functions reachable only through headlessInstall, which requires a full store/registry integration setup. End-to-end validation requires a read-only Nix store environment.

@vsumner
vsumner requested a review from zkochan as a code owner March 18, 2026 22:42
Copilot AI review requested due to automatic review settings March 18, 2026 22:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent write attempts inside Global Virtual Store (GVS) package directories when those packages are already present (“warm”), which can fail when the store is backed by a read-only CAS (e.g. Nix).

Changes:

  • Add freshlyImported?: boolean to DependenciesGraphNode to track whether importPackage() actually wrote package contents.
  • Set depNode.freshlyImported after importPackage() and use it in GVS mode to skip self-dep symlinking and per-package bin linking for warm packages.
  • Thread enableGlobalVirtualStore into linkAllBins() so it can apply the warm-package skip in GVS mode.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
installing/deps-restorer/src/index.ts Tracks whether a package was freshly imported and skips post-import linking steps for warm packages in GVS mode.
deps/graph-builder/src/lockfileToDepGraph.ts Extends DependenciesGraphNode with an optional freshlyImported flag.
.changeset/skip-linking-warm-gvs-packages.md Adds a changeset documenting the behavior change as a patch.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread installing/deps-restorer/src/index.ts Resolved
When safeToSkip causes importPackage to skip (package already
present in the store), self-dep symlinking and bin linking are
also skipped. These operations were completed during the original
import, so re-running them is unnecessary and fails on read-only
stores (e.g. Nix).

Add freshlyImported flag to DependenciesGraphNode, set after
importPackage, and check it before self-dep and bin linking in
GVS mode.
@vsumner
vsumner force-pushed the fix/gvs-skip-linking-warm-packages branch from 086ff5e to c30599a Compare March 18, 2026 22:52
@vsumner
vsumner requested a review from Copilot March 18, 2026 22:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts Global Virtual Store (GVS) behavior to avoid post-import operations that write into package directories when those packages were not freshly imported (e.g. when the store path is read-only such as in Nix). It introduces a freshlyImported flag on dependency graph nodes and uses it plus filesystem existence checks to skip self-dep symlinking and bin linking for warm packages.

Changes:

  • Add freshlyImported?: boolean to DependenciesGraphNode in @pnpm/deps.graph-builder.
  • Set depNode.freshlyImported based on importPackage() result and conditionally skip self-dep symlinking / .bin linking in GVS mode for warm packages when targets already exist.
  • Add a changeset for patch releases (and include a new .pi/tasks/...json metadata file).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
installing/deps-restorer/src/index.ts Thread enableGlobalVirtualStore into bin linking and skip post-import linking steps for warm GVS packages when targets already exist.
deps/graph-builder/src/lockfileToDepGraph.ts Extend DependenciesGraphNode with freshlyImported?: boolean.
.changeset/skip-linking-warm-gvs-packages.md Release note describing the new skip behavior for warm GVS packages.
.pi/tasks/tasks-dd9df0f0-94e3-462d-bc5d-39fcabac4d49.json Adds task-tracking metadata file (likely unintended repo artifact).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

"pnpm": patch
---

Skip self-dep symlinking and bin linking for warm Global Virtual Store packages. When `safeToSkip` causes the import to be skipped (package already present), post-import linking is also skipped since it was completed during the original import.
version: string
fetching?: () => Promise<PkgRequestFetchResult>
forceImportPackage?: boolean // Used to force re-imports from the store of local tarballs that have changed.
freshlyImported?: boolean // Set to true when importPackage wrote to disk (importMethod returned).
Comment on lines +960 to 965
// In GVS mode, skip when the self-dep symlink already exists —
// it was created during the original import and the directory may
// be read-only.
if (opts.enableGlobalVirtualStore && !depNode.freshlyImported &&
existsSync(path.join(targetModulesDir, depNode.name))) return
await limitLinking(async () => symlinkDependency(pkg.dir, targetModulesDir, depNode.name))
Comment on lines +960 to +964
// In GVS mode, skip when the self-dep symlink already exists —
// it was created during the original import and the directory may
// be read-only.
if (opts.enableGlobalVirtualStore && !depNode.freshlyImported &&
existsSync(path.join(targetModulesDir, depNode.name))) return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could skip it in all cases. Not only inside the gvs. But I am also not sure why symlink-dir tries to create the symlink if it already exists and is correct. I have specifically worked on this case to not recreate the symlinks.

Comment on lines +989 to +992
// In GVS mode, skip bin linking when the .bin directory already exists —
// the bins were linked during the original import and the directory may
// be read-only (e.g. Nix store).
if (opts.enableGlobalVirtualStore && !depNode.freshlyImported && existsSync(binPath)) return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe check the existence of every bin file individually?

@vsumner

vsumner commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

I'm going to step back and make sure I"m solving the right problem. Sorry for the churn.

@vsumner vsumner closed this Mar 18, 2026
zkochan added a commit that referenced this pull request Mar 26, 2026
… target (#11069)

## Summary

`linkBin()` unconditionally calls `cmdShim()` / `symlinkDir()` even when the target bin already points at the correct path. This causes redundant I/O on repeated installs and `EACCES` failures when the bin directory lives on a read-only filesystem (Docker layer caching, CI prewarm, NFS mounts).

This PR adds a check at the top of `linkBin()` that verifies the existing bin before skipping:

- **Symlinks**: `readlink` target is compared against `cmd.path`
- **Cmd-shim files**: checked via `isShimPointingAt()` from `@zkochan/cmd-shim` v9, which embeds a `# cmd-shim-target=<path>` marker in every generated sh shim
- Files larger than 4KB (binaries) are never skipped — they are not cmd-shims

Stale or incorrect bins (wrong target, missing marker, different provider) are always rewritten.

Follows up on feedback from #11020.

## Changes

- `bins/linker/src/index.ts` — add target verification check in `linkBin()`
- `bins/linker/test/index.ts` — tests for skip and rewrite behavior
- `pnpm-workspace.yaml` — upgrade `@zkochan/cmd-shim` to v9

---------

Co-authored-by: Zoltan Kochan <[email protected]>
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.

3 participants