Skip to content

fix: skip re-importing packages when global virtual store is warm - #10709

Merged
zkochan merged 4 commits into
pnpm:mainfrom
vsumner:fix/gvs-reattach-fast-path
Feb 27, 2026
Merged

zkochan merged 4 commits into
pnpm:mainfrom
vsumner:fix/gvs-reattach-fast-path

Conversation

@vsumner

@vsumner vsumner commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When node_modules is deleted but the global virtual store ({storeDir}/links/) remains intact, pnpm re-imports every package from the content-addressable store — even though the materialized package directories already exist globally.

The root cause: the skip logic in lockfileToDepGraph.ts depends on currentLockfile (from node_modules/.pnpm/lock.yaml). When node_modules is deleted, currentLockfile is null, currentPackages = {}, depIsPresent is always false, and both skip paths are unreachable. Every package falls through to fetchPackage().

rm -rf node_modules
  → readCurrentLockfile() returns null
  → currentPackages = {}
  → depIsPresent = false for ALL packages
  → existing skip paths unreachable
  → every package re-fetched + re-imported (expensive)

With the standard (local) virtual store this is unavoidable — dir points inside node_modules which was deleted. But with the global virtual store, dir points to {storeDir}/links/{hash}/... — a path outside node_modules that survives deletion. The pathExists(dir) check would succeed if it were reached, but it never is.

Solution

Add a reattach fast-path after the existing skip blocks, before the fetchPackage fallback:

if (!fetchResponse && opts.enableGlobalVirtualStore && !isDirectoryDep
    && !opts.force) {
  if (dirExists ?? await pathExists(dir)) {
    fetchResponse = {}
  }
}

Why this is safe

The global virtual store directory path is the integrity check. The depGraphHash in the path encodes:

  • Engine: Node.js version + platform
  • Full transitive dep graph: recursive hash of every dependency's pkgIdWithPatchHash + resolution.integrity

If any of these change, the hash changes, the path changes, and pathExists fails. Directory existence is a constructive proof of validity.

Guard conditions

Guard Reason
!fetchResponse Don't override if an existing skip already set it
opts.enableGlobalVirtualStore Only in GVS mode (dir points to global path)
!isDirectoryDep Exclude workspace file: protocol packages
!opts.force --force should bypass all skip logic

What fetchResponse = {} does downstream

This is the same pattern used by the existing secondary skip (line 253):

fetchResponse = {}
  → graph[dir].fetching = undefined
  → linkAllPkgs: if (!depNode.fetching) return
  → importPackage() is never called

Benchmark Results

Measured with benchmarks/bench.sh (5 runs, 1 warmup, fixture package with ~200 deps):

# Scenario main branch Delta
1 Headless (warm store+cache) 3.643s ± 0.062s 3.705s ± 0.019s no change
2 Re-resolution (add dep, warm) 6.525s ± 4.527s 6.585s ± 4.498s no change
3 Full resolution (warm, no lockfile) 6.366s ± 0.085s 6.317s ± 0.050s no change
4 Headless (cold store+cache) 8.368s ± 0.058s 8.275s ± 0.059s no change
5 Cold install (nothing warm) 9.231s ± 0.138s 9.219s+ no change
6 GVS warm reinstall 5.136s ± 0.073s 0.695s ± 0.009s 7.4x faster

No regressions on any existing scenario. GVS warm reinstall goes from 5.1s to 695ms — a 7.4x speedup.

Related issues

  • #9696 — GVS improvements tracker
  • #10681 — Allow GVS in CI (directly benefits "pre-warmed cache" CI workflows)
  • #8190 — Original GVS implementation

Test plan

  • New test: install with GVS → delete only node_modules (keep global store warm) → reinstall with frozenLockfile: true → verify node_modules recreated correctly
  • Spy on fetchPackage confirms 0 calls during warm reinstall (fast-path actually fires)
  • All 4 globalVirtualStore tests pass
  • Lint passes
  • Benchmark shows 7.4x speedup with zero regressions

When node_modules is deleted but the global virtual store directories
survive, pnpm previously re-fetched every package because the skip
logic required currentLockfile to be present. Add a fast-path that
checks pathExists(dir) for GVS directories even when currentLockfile
is missing, since the GVS directory hash encodes engine, integrity,
and full dependency subgraph.
@vsumner
vsumner requested a review from zkochan as a code owner February 27, 2026 16:33
Copilot AI review requested due to automatic review settings February 27, 2026 16:33
@welcome

welcome Bot commented Feb 27, 2026

Copy link
Copy Markdown

💖 Thanks for opening this pull request! 💖
Please be patient and we will get back to you as soon as we can.

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 optimizes installs when using pnpm’s global virtual store by avoiding unnecessary package re-imports after node_modules is deleted but the global virtual store directories remain present (“warm”), and adds a regression test plus a changeset entry.

Changes:

  • Add a “reattach” fast-path in lockfileToDepGraph to skip fetching/importing when the computed global virtual store package directory already exists.
  • Add an install test that deletes only node_modules and reinstalls with frozenLockfile.
  • Add a changeset documenting the patch release.

Reviewed changes

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

File Description
deps/graph-builder/src/lockfileToDepGraph.ts Adds a new warm-global-store existence check to set fetchResponse = {} and avoid re-import.
pkg-manager/core/test/install/globalVirtualStore.ts Adds a test case for reinstalling after deleting only node_modules while keeping the global virtual store.
.changeset/global-virtual-store-reattach.md Declares patch releases and describes the behavior change.

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

@vsumner vsumner mentioned this pull request Feb 27, 2026
4 tasks done
The includeUnchangedDeps flag is true whenever currentHoistPattern
differs from the desired hoistPattern. After deleting node_modules,
currentHoistPattern is always undefined (read from .modules.yaml),
so the flag is always true when hoisting is configured — defeating
the optimization in the exact scenario it targets.

The guard is unnecessary because the fast-path only skips fetch/import
(fetchResponse = {}), not graph inclusion. The package is still added
to the graph with children populated, so hoisting recalculation works.
Adds benchmark 6: frozen lockfile reinstall with a warm global virtual
store after deleting node_modules. This measures the reattach fast-path
where all packages are skipped (no fetch/import) because their GVS
hash directories already exist.
@vsumner
vsumner force-pushed the fix/gvs-reattach-fast-path branch from 5ce02a5 to eba438a Compare February 27, 2026 17:59

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


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

@zkochan

zkochan commented Feb 27, 2026

Copy link
Copy Markdown
Member

That's awesome!

@zkochan
zkochan merged commit 97f049f into pnpm:main Feb 27, 2026
10 checks passed
@zkochan

zkochan commented Feb 27, 2026

Copy link
Copy Markdown
Member

I wonder if the same issue is present during non-headless install.

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