Skip to content

fix(arborist): derive linked bin cleanup paths through bin-links (v10 backport) - #10346

Open
jasperjjhe wants to merge 2 commits into
npm:release/v10from
jasperjjhe:fix/bin-cleanup-paths-v10
Open

jasperjjhe wants to merge 2 commits into
npm:release/v10from
jasperjjhe:fix/bin-cleanup-paths-v10

Conversation

@jasperjjhe

@jasperjjhe jasperjjhe commented Nov 25, 2026 •

Copy link
Copy Markdown

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.binPaths already routes through bin-links#getPaths, so hoisted installs produce normalized bin paths. The linked install strategy's cleanup derivation in isolated-reifier still 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 through bin-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

  • Replace linked-mode manual bin path derivation with bin-links#getPaths() using the dependency's consumer-facing installation path, not its store path.
  • Add Node.binPaths regression 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.
  • Add hoisted and linked optional-extraction-failure regressions that verify an external sentinel survives cleanup with scripts disabled, plus a linked ideal-tree unit test asserting normalized bin paths (adapted from fix(arborist): derive linked bin cleanup paths through bin-links #10057 to the v10 test fixtures: the failure is triggered by the shared fixture server's built-in 404 paths (@isaacs/testing-missing-tgz tarball, @isaacs/this-does-not-exist-at-all packument) 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

Validation

  • Patch applies cleanly to release/v10 at 3475294 (git apply --check verified against a pristine checkout).
  • Production change verified byte-identical to the merged fix(arborist): derive linked bin cleanup paths through bin-links #10057 fix.
  • The touched test suites were executed in a network-isolated Linux container (node:22-bookworm, deps from the lockfile via npm ci): tap test/arborist/reify.js test/node.js passes 107/107 and 56/56 including the adapted tests, and eslint on the three touched files is clean.
  • The linked-strategy tests were validated differentially as well: with only the production change reverted, the linked optional-extraction test removes the external sentinel and the linked ideal-tree test produces a cleanup path outside the project, and both pass again with this change. The hoisted test and the Node.binPaths test pass either way, since hoisted cleanup already routes through getPaths on v10.
  • One note for reviewers: v10's tnock fixture 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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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({

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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({

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Comment on lines +1129 to +1131
const { resolve: r } = require('node:path')

t.strictSame(root.children.get('evil').binPaths, [

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.
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