Skip to content

fix: keep installs of one package version apart when their dependencies differ - #29

Merged
nitedani merged 22 commits into
nitedani:mainfrom
nitedani-agent:per-install-copies
Oct 30, 2026
Merged

nitedani merged 22 commits into
nitedani:mainfrom
nitedani-agent:per-install-copies

Conversation

@nitedani-agent

@nitedani-agent nitedani-agent commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

ClaudeCodex Claude Opus 5.5 (high effort) wrote this; GPT-6.1-Sol (Codex, high effort) reviewed it.

When pnpm installs one package version twice because its importers give it different peer versions, the output keeps one copy of it, and both importers get the dependency versions of whichever install was linked last.

Case Observed on main Expected
An app using @sentry/node@10.53.1 (core 2.x) next to its own @sentry/opentelemetry@10.53.1 on @opentelemetry/core@1.30.1 @sentry/node's @sentry/opentelemetry loads core 1.30.1 it loads core 2.12.0, as when the app runs from source

This PR still keeps one copy per name and version, and splits it only where installs load different copies of one dependency, checked through their dependencies' own dependencies, so any two copies of a version differ in what they load. The one exception is a dependency cycle through several split packages whose grouping never settles: there, installs share a copy only when they load the very same folders, which can leave a copy more than needed. Identical installs stay one copy, as you asked in #28 (comment). CI moves to Node 22, the lowest version engines allows, because on Node 20 the lookup for prebuilt .node binaries finds nothing.

Closes #28

What you should see

1. The Sentry app from #28 loads the same versions after the build as from source:

$ node index.js
@sentry/node itself: core 2.12.0; @sentry/node's @sentry/opentelemetry: core 2.12.0; app's @sentry/opentelemetry: core 1.30.1 function function
$ node build.mjs && cd dist && node index.mjs     # main
@sentry/node itself: core 2.12.0; @sentry/node's @sentry/opentelemetry: core 1.30.1; app's @sentry/opentelemetry: core 1.30.1 function function
$ node build.mjs && cd dist && node index.mjs     # this PR (44c5922)
@sentry/node itself: core 2.12.0; @sentry/node's @sentry/opentelemetry: core 2.12.0; app's @sentry/opentelemetry: core 1.30.1 function function

2. It adds only the copy that's needed: the Sentry output grows from 1178 to 1186 files, because @sentry/opentelemetry is now two copies. test/vite builds the same 91 files as on main.

3. test/peers checks the versions each install loads and how many copies ship, including installs that differ only in peers they never load, and installs that load only part of a package. With bundle: false, it also checks that the app's other files load the app's versions.

Approach

Rated 8/10: per name and version, each install joins the first copy it doesn't conflict with, in a fixed order, repeated until nothing changes (if it never settles, installs share a copy only when they load the very same folders); the top-level link goes to the copy the app imports. The exact fewest copies is graph colouring: it took 128 s for 44 installs of one version, so this PR doesn't promise it. Keying every copy by its source folder (4/10) copies identical installs too. Full record: https://gist.github.com/nitedani-agent/3b9f560a214be09ffffde48f5dfc584d

Loop A: 16 passes, the last on d3c14e4 found nothing

Fixed, by area:

  • Extra copies when installs load only part of a package's peers, at one and two levels (5bd3d29, 1e23f91), then one rule for all of them: every pass regroups each version from scratch (28aa041). An exact fewest-copies search (75895a0) took 128 s for 44 installs, so e40cb4c went back to the greedy pass, now in a fixed order.
  • The top-level link went to the newest copy, not the app's own (eac82a3), and could differ between identical builds (09efc4a, e11228b). With bundle: false it missed what the app's other files import (5aad5b0, 75ead6d, 503b554).
  • When no pass settles, identical installs each got a copy (d1c8437), and identical nested installs had their prebuilt binary copied twice (02bbf8e).

Below the bar: a dependency cycle through three split packages, each loaded only in part, can keep a copy more than needed, with the output still right. Full reports: https://gist.github.com/nitedani-agent/664c00e5c0d55d8901bf71328875533d

Loop B: seven rounds, the last clean on d3c14e4

Fixed: no test showed that each split copy ships its prebuilt binary (6bb268c, after a4c7838 moved CI to Node 22), and a greedy grouping that could keep three copies where two serve all installs (led to 75895a0, then e40cb4c). A pass over the file's history reused uniquePackages (4418038). Full reports: https://gist.github.com/nitedani-agent/3db4150663fc0ed9bbaa0747a566f41f

Fresh read: nine reads as you, the last says merge as is on d3c14e4

Fixed: identical installs each got a copy when no pass settles (d1c8437), a prebuilt binary copied twice (02bbf8e), the top-level link with bundle: false (5aad5b0, 75ead6d, 503b554), and under Yarn PnP, installs whose npm: aliases point at different versions or packages shared a copy (39ec600, d3c14e4). Below the bar, and on main too: a rebuild into an existing output folder traces nothing and keeps the first build's dependencies, and an optional peer missing from one install (debug's supports-color) is found after copying, because single-version packages sit at the output's top level. Full reports: https://gist.github.com/nitedani-agent/6ae84d184f7feafa41e038c8c6b9fa3d

Gates: install, build and the five test suites exit 0 on d3c14e4, on Node 22 and Node 26
Step Node 22.23.4 Node 26.12.1
pnpm install, pnpm build exit 0 exit 0
test/vite, test/isolated, test/assets exit 0 exit 0
test/peers exit 0 exit 0
test/pnp exit 0 exit 0

nitedani-agent and others added 19 commits October 29, 2026 16:55
…ependencies differ

Traced packages were grouped by name@version, so pnpm peer variants (e.g.
@sentry/opentelemetry@10.53.1 with @opentelemetry/core 1.30.1 and 2.12.0)
shared one output folder linked to whichever install came last. A group now
splits into `.versions/<name>@<version>_<n>` copies only while its installs
load different copies of a dependency; identical installs stay one copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
engines requires Node >=22 since bd236c4, whose native-binary lookup uses
fs.glob; on Node 20 that lookup finds nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Four installs of one version, two loading only part of its peers, could get
three copies where two serve them all, depending on trace order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… another

An install that loads only some of a package's dependencies could keep a copy
of its own when the copy it matches only appeared after its dependency split.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The split loop kept each install inside the copy it got in the pass before,
so a later split or merge one level down could leave extra copies above it.
Each pass now groups every name@version anew from the copies its
dependencies got, until a pass changes nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Picking the first matching copy could still leave one copy more than needed,
e.g. four installs that each load a different two of a package's peers. Two
installs need separate copies only when they load different copies of one
dependency, so each version now gets the fewest copies that keeps such
installs apart, found by trying 1, 2, ... copies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The exact search for the fewest copies took minutes once one version had about
40 installs that load only part of its dependencies, so each version goes back
to a single ordered pass, in which any two copies load a different copy of
some dependency. The order is now fixed: the app's own install first, then the
installs that load more, then by path, so identical builds give identical
output and the top-level link names the app's own copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With several copies of one package, the top-level node_modules/<name> linked
to the newest, so an app on an older version than an external dependency's
loaded that dependency's copy. It now links to the copy the app's own files
import, and to the newest only when the app imports none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the app imports none of a package's copies, the top-level link went to
whichever copy of the newest version nft traced first, so identical builds
could link different copies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…very build

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fallback gave every install its own copy, so two installs that load the
same dependency folders, or none, were copied twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sampling every folder copied a binary once per identical nested install into
the same output copy and globbed each of them. Copies are now assigned first,
binaries are looked up once per output copy, and optional dependencies are
sampled per name@version again, as on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With bundle: false the app's files beside its entry are traced as its own package,
so a version only they import lost the top-level link to the newest version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A package folder that only encloses the entry, such as the workspace root the app
imports, is a dependency and must not take the app's top-level link.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An entry under node_modules belongs to its node_modules/<name> folder, not to a
nearer package.json that only sets the module type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nitedani

Copy link
Copy Markdown
Owner

resolve conflict + refactor then I'll merge

…pies

PnP's local-fork version suffix now applies before assignCopies, so a fork and the npm
package of its name and version never share a copy; the PnP app import and npm: alias
links use output copies instead of versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nitedani-agent

Copy link
Copy Markdown
Contributor Author

Claude Claude Opus 5.5 (high effort) wrote this.

I'm resolving the conflict with #27 and refactoring now, and I'll push both by 2026-10-30T04:00Z.

nitedani-agent and others added 2 commits October 30, 2026 01:32
…ackage stay apart

Two npm: aliases of lodash at different versions are both traced as lodash, so the
installs that load them looked identical and shared a copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… version

An alias installing lodash@4.17.21 and one installing lodash-es@4.17.21 had the same
copy label, so installs that loaded them under one name shared a copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nitedani-agent

Copy link
Copy Markdown
Contributor Author

ClaudeCodex Claude Opus 5.5 (high effort) wrote this; GPT-6.1-Sol (Codex, high effort) reviewed it.

Done in 44c5922: it merges main, and #27's version for a local fork with an npm package's name and version now applies before installs are grouped into copies, so a fork never shares the npm package's copy. Checking it under PnP turned up two more cases, fixed in 39ec600 and d3c14e4: installs whose npm: aliases point at different versions or packages shared a copy. All five test suites pass on Node 22 and 26.

The refactor found nothing worth changing in the new code. Its ratings of what the alias fixes touch, before ⇒ after them:

Function 44c5922 ⇒ d3c14e4 Why
trace 7 ⇒ 7 hands the PnP aliases to assignCopies, and no grouping logic moves into it
assignCopies 7 ⇒ 7 takes the traced files and the aliases, and still returns one copy label per folder
load, inside assignCopies new ⇒ 8 one small helper for both kinds of import
test/pnp fixtures new ⇒ 9 each case is a tiny package, and the test checks which lodash each one loaded

processTracedFiles, createDependencySymlinks, addNativeBinaries and uniquePackages keep their ratings from before the merge (7, 8, 8 and 9).

CI is green on d3c14e4, so I'd squash-merge it.

@nitedani
nitedani merged commit b825576 into nitedani:main Oct 30, 2026
1 check passed
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.

One copy of a package wins when two copies have the same name and version

2 participants