Skip to content

Commit 2b976b5

Browse files
fix(arborist): invalid filterNode crash under the linked strategy (#9637)
In continuation of our exploration of using `install-strategy=linked` in the [Gutenberg monorepo](WordPress/gutenberg#75814), which powers the WordPress Block Editor. Under `install-strategy=linked`, several common installs failed with `npm error invalid filterNode: outside idealTree/actualTree`, with no workaround besides dropping the linked strategy. Hoisted handled all of them. This fixes two distinct paths that both produced that error. ## Why A linked reify diffs the ideal tree against a synthesized actual wrapper (`#linkedActualForDiff`) rather than `this.actualTree`. `Diff.calculate` rejects any filter node whose root is neither the ideal nor the actual it was given, so a filter node taken from the real `this.actualTree` is "outside" the diff and throws. Two places fed it such nodes: - `--workspaces=false` and `-w <ws> --include-workspace-root` go through the `includeRootDeps` branch of `_diffTrees()`, which collected root-dep edge targets from both `this.idealTree` and `this.actualTree`. The actual-side targets are rooted at the real actual tree, not the wrapper, so they tripped the guard. The sibling `includeWorkspaces` branch already accounted for this; the root-dep branch did not. - A global install with a per-call `installStrategy: 'linked'` re-engaged the linked path even though the constructor normalizes global installs to `shallow` (the linked layout is unsupported for globals). Re-installing an already-present global package then hit the global explicit-request branch, which pushes actual-side nodes, and tripped the same guard. Suppressing the crash there was worse: the isolated reifier does not materialize the global layout and removed the package instead. ## How `_diffTrees()` now iterates only the ideal tree for root-dep filter nodes when the linked wrapper is in use, matching the existing workspace-node handling. The ideal-side nodes are sufficient to scope the diff, and the post-reify orphan sweep continues to prune deps removed from the manifest. `reify()` now honors the constructor's global-to-shallow normalization when deriving the `linked` flag, so a global install never engages the linked path regardless of a per-call `installStrategy`. Global installs fall back to shallow, which materializes and upgrades packages correctly. No change to the global explicit-request branch is needed once global is never linked. ## References Fixes #9614 Part of #9608
1 parent 0ffce98 commit 2b976b5

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎workspaces/arborist/lib/arborist/reify.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ module.exports = cls => class Reifier extends cls {
9090

9191
// public method
9292
async reify (options = {}) {
93-
const linked = (options.installStrategy || this.options.installStrategy) === 'linked'
93+
// Global installs are normalized to the shallow strategy in the constructor; honor that here so a per-call installStrategy:'linked' can't re-engage the unsupported linked path.
94+
const linked = !this.options.global &&
95+
(options.installStrategy || this.options.installStrategy) === 'linked'
9496

9597
if (this.options.packageLockOnly && this.options.global) {
9698
const er = new Error('cannot generate lockfile for global packages')
@@ -473,7 +475,10 @@ module.exports = cls => class Reifier extends cls {
473475
}
474476
if (includeRootDeps) {
475477
// add all non-workspace nodes to filterNodes
476-
for (const tree of [this.idealTree, this.actualTree]) {
478+
// Skip the actual tree under the linked diff wrapper: its edge targets have root===actualTree, not the wrapper, which trips Diff.calculate's filterNode guard.
479+
// The ideal-side targets alone scope the diff.
480+
const trees = this.#linkedActualForDiff ? [this.idealTree] : [this.idealTree, this.actualTree]
481+
for (const tree of trees) {
477482
for (const { type, to } of tree.edgesOut.values()) {
478483
if (type !== 'workspace' && to) {
479484
filterNodes.push(to)

‎workspaces/arborist/test/arborist/reify.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4401,6 +4401,64 @@ t.test('install strategy linked', async (t) => {
44014401
})
44024402
})
44034403

4404+
t.test('linked strategy --workspaces=false and --include-workspace-root do not crash', async t => {
4405+
// Regression for #9614. Under linked, the root-dep filter nodes came from the real actual tree, not the synthesized diff wrapper, tripping Diff.calculate's "invalid filterNode" guard.
4406+
const manifest = deps => JSON.stringify({
4407+
name: 'root',
4408+
version: '1.0.0',
4409+
workspaces: ['packages/*'],
4410+
dependencies: deps,
4411+
})
4412+
const path = t.testdir({
4413+
'package.json': manifest({ abbrev: '1.1.1', wrappy: '1.0.2' }),
4414+
packages: {
4415+
a: {
4416+
'package.json': JSON.stringify({ name: 'a', version: '1.0.0' }),
4417+
},
4418+
},
4419+
})
4420+
4421+
createRegistry(t, true)
4422+
await reify(path, { installStrategy: 'linked' })
4423+
4424+
// --workspaces=false: only root deps are in scope.
4425+
await t.resolves(
4426+
reify(path, { installStrategy: 'linked', workspacesEnabled: false }),
4427+
'--workspaces=false does not crash'
4428+
)
4429+
4430+
// -w a --include-workspace-root: workspace a plus root deps in scope.
4431+
await t.resolves(
4432+
reify(path, { installStrategy: 'linked', workspaces: ['a'], includeWorkspaceRoot: true }),
4433+
'-w a --include-workspace-root does not crash'
4434+
)
4435+
4436+
t.ok(fs.lstatSync(resolve(path, 'node_modules/abbrev')).isSymbolicLink(), 'root dep still linked')
4437+
4438+
// Dropping the actual-side filter nodes must not stop a filtered install from pruning a removed root dep.
4439+
fs.writeFileSync(resolve(path, 'package.json'), manifest({ abbrev: '1.1.1' }))
4440+
await reify(path, { installStrategy: 'linked', workspacesEnabled: false })
4441+
t.notOk(fs.existsSync(resolve(path, 'node_modules/wrappy')), 'removed root dep pruned under filtered install')
4442+
t.ok(fs.lstatSync(resolve(path, 'node_modules/abbrev')).isSymbolicLink(), 'remaining root dep still linked')
4443+
})
4444+
4445+
t.test('global install ignores a per-call linked strategy', async t => {
4446+
// Regression for #9614. Global installs are normalized to shallow; a per-call installStrategy:'linked' must not re-engage the linked path, which would trip Diff.calculate's filterNode guard on re-install and delete the global package.
4447+
const path = t.testdir({ lib: {} })
4448+
const lib = resolve(path, 'lib')
4449+
const nm = resolve(lib, 'node_modules')
4450+
4451+
createRegistry(t, true)
4452+
await reify(lib, { add: ['[email protected]'], global: true })
4453+
4454+
// Re-install the already-present package under linked: must not crash and must not remove it.
4455+
await t.resolves(
4456+
reify(lib, { add: ['[email protected]'], global: true, installStrategy: 'linked' }),
4457+
'global re-install under linked does not crash'
4458+
)
4459+
t.strictSame(fs.readdirSync(nm), ['abbrev'], 'global package retained, no .store created')
4460+
})
4461+
44044462
t.test('linked strategy exposes store node_modules via NODE_PATH for lifecycle scripts', async t => {
44054463
// Regression for #9549. In the linked strategy a store package's deps are symlinked siblings in its store node_modules.
44064464
// A separate bin invoked by the script (e.g. napi-postinstall) resolves modules from its own store realpath and cannot see them, so npm exposes them via NODE_PATH.

0 commit comments

Comments
 (0)