Repository navigation
fix(arborist): derive linked bin cleanup paths through bin-links (v10 backport) - #10346
jasperjjhe wants to merge 2 commits into
Conversation
Backport of npm#10057 to release/v10.
There was a problem hiding this comment.
🟡 Changes recommended
Arborist still allows bin-links 5.0.0, leaving standalone consumers exposed to cleanup paths outside .bin, and Windows cleanup can delete command shims belonging to surviving dependencies.
4 open issues
What changed in this PR
Backports linked-install bin cleanup hardening to npm v10, aligning Arborist’s linked strategy with the bin normalization already used by hoisted installs.
Changes:
- Derives linked cleanup paths through
bin-links#getPaths()using consumer-facing installation paths. - Adds bin-path normalization and metadata-preservation coverage.
- Adds optional-extraction-failure cleanup tests and a linked ideal-tree regression test.
| File | Description |
|---|---|
workspaces/arborist/test/node.js |
Adds traversal, absolute-path, Windows-style, and invalid-bin cases. |
workspaces/arborist/test/arborist/reify.js |
Adds cleanup sentinel tests and linked-tree path assertions. |
workspaces/arborist/lib/arborist/isolated-reifier.js |
Replaces manual bin-path joins with bin-links#getPaths(). |
Review effort: Max
| target.binPaths.push(join(dep.root.localPath, nmFolder, '.bin', bn)) | ||
| } | ||
| } | ||
| target.binPaths.push(...getBinPaths({ |
There was a problem hiding this comment.
Thanks. The CLI's bundled copy is already pinned to bin-links@5.0.1 in the v10 lockfile (that pin landed with #10177), so the shipped CLI gets the normalization. The published @npmcli/arborist range staying ^5.0.0 matches the choice made when 5.0.1 shipped to this branch, and #10057 itself changes no dependencies, so I kept the backport dependency-free to stay reviewable against upstream. A minimum-version bump for standalone consumers seems reasonable as a separate change if maintainers want it, and I'm happy to send a follow-up.
| target.binPaths.push(join(dep.root.localPath, nmFolder, '.bin', bn)) | ||
| } | ||
| } | ||
| target.binPaths.push(...getBinPaths({ |
There was a problem hiding this comment.
Thanks. This matches the hoisted strategy's existing behavior: Node.binPaths already derives the .cmd and .ps1 shims through getPaths, and hoisted cleanup trashes them today, so the linked strategy joining that behavior is the parity this backport intends. The shared-name collision predates the change as well; the old code trashed the extensionless path for a shared name too. #10057 accepted the same shape on the v11 and v12 lines. A reconciliation or relink pass would be a behavior change beyond this backport, but it reads like a reasonable upstream hardening idea.
| packageLockOnly: true, | ||
| }) | ||
|
|
||
| await arb.buildIdealTree() |
There was a problem hiding this comment.
You're right, and I verified it: with the production change reverted, the tests as originally filed passed on stock v10, because the tree constructors normalize the metadata they ingest before the reifier runs. Reworked in 5c590b9: the raw traversal bin is restored on the ideal tree after buildIdealTree(), and the cleanup tests build the ideal tree, restore the raw bin, then call reify() on the same instance, which reuses the built tree. Differential check in a container: against stock release/v10 the reworked linked tests now fail as expected (the linked ideal tree produces a cleanup path outside the project, and the linked optional-extraction test removes the external sentinel), and both pass with this change.
| const { resolve: r } = require('node:path') | ||
|
|
||
| t.strictSame(root.children.get('evil').binPaths, [ |
There was a problem hiding this comment.
Confirmed: lib/node.js:124 runs normalize() on directly supplied package metadata, so the constructor had already sanitized the bin before the getter. Reworked in 5c590b9: the raw bin is assigned after construction, and the test now also asserts the getter does not replace the node's own package bin metadata. To set expectations, this one exercises the pre-existing v10 getter normalization (which already routes through getPaths) rather than the reifier change in this diff, so it is coverage for the normalization family rather than a regression test for the linked cleanup fix.
The v10 tree constructors normalize the package metadata they ingest, so the regression tests now restore the raw traversal bin after tree construction, keeping the assertions on the isolated reifier's own path derivation.


Summary
Manual backport of #10057 to
release/v10, as requested by the backport automation note on that PR (the cherry-pick to release/v10 conflicted; the v11 backport landed as #10266).npm 10 bundles bin-links@5.0.1, which includes the bin metadata normalization from bin-links#193 (the v5 maintenance-line release of npm/bin-links#187).
Node.binPathsalready routes throughbin-links#getPaths, so hoisted installs produce normalized bin paths. The linked install strategy's cleanup derivation inisolated-reifierstill manually joins the dependency's bin keys, so traversal-bearing, absolute, or Windows-style bin metadata that reaches the linked cleanup derivation can produce cleanup paths outside the package's node_modules/.bin when an optional dependency fails to extract and reify cleans it up with scripts disabled. This routes that derivation throughbin-links#getPaths()using the dependency's consumer-facing installation path, the same fix shape as #10057, so both strategies share the bundled normalization. The v10 tree constructors normalize the package metadata they ingest (lib/node.js), so the exposure here is bin metadata that reaches the reifier unnormalized, for example metadata assigned after tree construction or supplied programmatically to Arborist as a library.This is the Arborist integration for CLI-CROW-GEN-002 on the v10 line. It does not introduce an Arborist-owned normalizer or modify the bundled dependency.
Changes
bin-links#getPaths()using the dependency's consumer-facing installation path, not its store path.Node.binPathsregression coverage for traversal-bearing, absolute, Windows-style and invalid bin metadata, including preservation of the caller's metadata. Adapted from fix(arborist): derive linked bin cleanup paths through bin-links #10057 to the v10 constructors, which normalize the metadata they ingest (lib/node.js): the raw bin is assigned after construction, and the test also asserts the node's own package bin metadata is preserved.@isaacs/testing-missing-tgztarball,@isaacs/this-does-not-exist-at-allpackument) instead of the v12 registry-mock helper, and the lockfile resolved URLs point at the local mock registry). The same v10 constructors normalize metadata during tree construction, so each of these tests restores the raw traversal bin on the built tree, keeping the assertion on the reifier's own derivation.Dependency status
No dependency changes. bin-links@5.0.1 (released 2026-10-26 via bin-links#193 with the getPaths normalization) is already pinned in the v10 lockfile, and no other v12-only change is required by the fix.
Notes
backport:v10label if that helps your automation.Validation
release/v10at 3475294 (git apply --checkverified against a pristine checkout).npm ci):tap test/arborist/reify.js test/node.jspasses 107/107 and 56/56 including the adapted tests, andeslinton the three touched files is clean.Node.binPathstest pass either way, since hoisted cleanup already routes through getPaths on v10.tnockfixture toggles nock's global net-connect state per test; the adapted tests deliberately avoid it and use the fixture server's built-in 404 paths instead, so the suite state machine is untouched.