Skip to content

Commit 541c286

Browse files
fix(arborist): forward transitive overrides through linked store links (#9658)
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`, an override that forces a **transitive** dependency to a version outside its dependent's declared range was applied on disk but reported as `invalid` by `npm ls --all`, which then exited 1 (`ELSPROBLEMS`). The hoisted strategy reports the same edge as `overridden` and exits 0. The bug only surfaced when the overridden package's dependent was itself a transitive (store) package — a direct dependency of the root was handled correctly. `npm ls` rebuilds the actual tree from the `.store` layout, and an OverrideSet propagates down the tree through `Link.recalculateOutEdgesOverrides`, which forwards the set from a store symlink to its target node. A prior fix (#9357) gated that forwarding on a rule naming a **direct** dependency of the target, to avoid flipping an unrelated target to "has overrides" and making `npm ci` re-resolve lockfile-pinned edges. That gate was too narrow: a store link whose own direct deps do not name the overridden package never forwarded the set, so the chain to the deeper `dependent → overridden` edge never received the rule and was reported `invalid`. The fix walks the target's subtree (following resolved edges, dereferencing links) and forwards the set when an override rule actually applies to any reachable edge, matched via `getEdgeRule` on name and spec so a non-applicable version-qualified rule still does not flip an intermediate node. Because override propagation is event-driven during load, a store link can run its check before its subtree is resolved; `loadActual` therefore re-forwards through links once the tree is complete, so the filesystem-scan path resolves transitive overrides as `overridden` rather than `invalid`. ## References Fixes #9619
1 parent f9e3a80 commit 541c286

4 files changed

Lines changed: 145 additions & 11 deletions

File tree

‎workspaces/arborist/lib/arborist/load-actual.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ module.exports = cls => class ActualLoader extends cls {
217217

218218
this.#transplant(root)
219219

220+
this.#repropagateOverrides()
221+
220222
if (global) {
221223
// need to depend on the children, or else all of them
222224
// will end up being flagged as extraneous, since the
@@ -405,6 +407,18 @@ module.exports = cls => class ActualLoader extends cls {
405407
}
406408
}
407409

410+
// Re-forward overrides through links after the tree is complete, since a store Link may forward before its subtree resolves and miss a transitive match (npm/cli#9619).
411+
#repropagateOverrides () {
412+
if (!this.#actualTree.overrides) {
413+
return
414+
}
415+
for (const node of this.#actualTree.inventory.values()) {
416+
if (node.isLink && node.overrides) {
417+
node.recalculateOutEdgesOverrides()
418+
}
419+
}
420+
}
421+
408422
// .npm-extension transformManifest, like packageExtensions, never rewrites a package's package.json, so re-derive its edges and provenance on a filesystem-scanned actual tree.
409423
// This executes the root extension code; ignore-extension (and ignore-scripts via flatten) disables it.
410424
async #applyNpmExtension () {

‎workspaces/arborist/lib/link.js‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,12 @@ class Link extends Node {
109109
// so this is a no-op
110110
[_loadDeps] () {}
111111

112-
// When a Link receives overrides (via edgesIn), forward them to the target node which holds the actual edgesOut — but only when the OverrideSet has at least one rule that names a dep the target actually depends on.
113-
// Without this scope, the link forwards a generic ancestor OverrideSet that has no real effect on the target's edges, but still flips the target to "has overrides", which changes downstream `canReplaceWith` / placement decisions and causes `npm ci` to re-resolve lockfile-pinned edges from the registry.
114-
// See npm/cli#9357.
112+
// Forward overrides to the target only when a rule names a dep it needs, directly or transitively (npm/cli#9357, #9619).
115113
recalculateOutEdgesOverrides () {
116114
if (!this.target || !this.overrides) {
117115
return
118116
}
119-
let hasMatchingRule = false
120-
for (const rule of this.overrides.ruleset.values()) {
121-
if (this.target.edgesOut.has(rule.name)) {
122-
hasMatchingRule = true
123-
break
124-
}
125-
}
126-
if (!hasMatchingRule) {
117+
if (!overrideMatchesSubtree(this.overrides, this.target)) {
127118
return
128119
}
129120
this.target.updateOverridesEdgeInAdded(this.overrides)
@@ -143,4 +134,29 @@ class Link extends Node {
143134
}
144135
}
145136

137+
// True when an override rule applies to an edge reachable from the target at any depth.
138+
// getEdgeRule matches on name and spec, returning the set itself when nothing applies, so a non-applicable version-qualified rule doesn't flip an intermediate node to "has overrides" (npm/cli#9357).
139+
// Not a method: runs from the Node super-constructor, before subclass private methods exist.
140+
const overrideMatchesSubtree = (overrides, target) => {
141+
const seen = new Set()
142+
const stack = [target]
143+
while (stack.length) {
144+
const node = stack.pop()
145+
const resolved = node.isLink ? node.target : node
146+
if (seen.has(resolved)) {
147+
continue
148+
}
149+
seen.add(resolved)
150+
for (const edge of resolved.edgesOut.values()) {
151+
if (overrides.getEdgeRule(edge).name === edge.name) {
152+
return true
153+
}
154+
if (edge.to) {
155+
stack.push(edge.to)
156+
}
157+
}
158+
}
159+
return false
160+
}
161+
146162
module.exports = Link

‎workspaces/arborist/test/arborist/load-actual.js‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,72 @@ t.test('applies root packageExtensions to a linked actual tree', async t => {
589589
t.strictSame(brokenLink.packageExtensionsApplied, applied, 'provenance mirrored onto the link')
590590
})
591591

592+
t.test('forwards a transitive override through a linked store link — npm/cli#9619', async t => {
593+
// The override must propagate through the intermediate store Link whose own direct deps don't name the overridden package, or `npm ls` reports the edge `invalid` instead of `overridden`.
594+
// Shaped like glob -> minimatch -> brace-expansion: the override target sits two links deep, with a shared node reached by two paths.
595+
const path = t.testdir({
596+
'package.json': JSON.stringify({
597+
name: 'root',
598+
version: '1.0.0',
599+
dependencies: { a: '1.0.0' },
600+
overrides: { leaf: '2.0.0' },
601+
}),
602+
node_modules: {
603+
a: t.fixture('symlink', '.store/[email protected]/node_modules/a'),
604+
'.store': {
605+
606+
node_modules: {
607+
a: { 'package.json': JSON.stringify({ name: 'a', version: '1.0.0', dependencies: { b: '1.0.0', c: '1.0.0' } }) },
608+
b: t.fixture('symlink', '../../[email protected]/node_modules/b'),
609+
c: t.fixture('symlink', '../../[email protected]/node_modules/c'),
610+
},
611+
},
612+
613+
node_modules: {
614+
// leaf is declared ^1.0.0 but overridden to 2.0.0, which is outside that range
615+
b: { 'package.json': JSON.stringify({ name: 'b', version: '1.0.0', dependencies: { leaf: '^1.0.0', shared: '1.0.0' } }) },
616+
leaf: t.fixture('symlink', '../../[email protected]/node_modules/leaf'),
617+
shared: t.fixture('symlink', '../../[email protected]/node_modules/shared'),
618+
},
619+
},
620+
621+
node_modules: {
622+
// c's subtree has no overridden package but reaches shared via both c and c->d, so its walk revisits a seen node
623+
c: { 'package.json': JSON.stringify({ name: 'c', version: '1.0.0', dependencies: { d: '1.0.0', shared: '1.0.0' } }) },
624+
d: t.fixture('symlink', '../../[email protected]/node_modules/d'),
625+
shared: t.fixture('symlink', '../../[email protected]/node_modules/shared'),
626+
},
627+
},
628+
629+
node_modules: {
630+
d: { 'package.json': JSON.stringify({ name: 'd', version: '1.0.0', dependencies: { shared: '1.0.0' } }) },
631+
shared: t.fixture('symlink', '../../[email protected]/node_modules/shared'),
632+
},
633+
},
634+
635+
node_modules: {
636+
leaf: { 'package.json': JSON.stringify({ name: 'leaf', version: '2.0.0' }) },
637+
},
638+
},
639+
640+
node_modules: {
641+
shared: { 'package.json': JSON.stringify({ name: 'shared', version: '1.0.0' }) },
642+
},
643+
},
644+
},
645+
},
646+
})
647+
648+
const tree = await loadActual(path)
649+
const b = tree.children.get('a').target.edgesOut.get('b').to.target
650+
const leafEdge = b.edgesOut.get('leaf')
651+
t.ok(leafEdge && !leafEdge.error, 'transitive overridden edge resolves without error')
652+
t.ok(leafEdge.overrides, 'edge carries the override rule')
653+
t.equal(leafEdge.spec, '2.0.0', 'edge spec is the overridden version')
654+
t.equal(leafEdge.rawSpec, '^1.0.0', 'edge rawSpec is the original declared range')
655+
t.equal(leafEdge.to.target.version, '2.0.0', 'edge resolves to the overridden package')
656+
})
657+
592658
t.test('store nodes do not load devDependencies as required edges', async t => {
593659
// A package in the linked store is structurally a tree top, so without the isInStore guard its devDependencies would load as required edges and surface as missing (e.g. npm sbom ESBOMPROBLEMS).
594660
const path = t.testdir({

‎workspaces/arborist/test/link.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,44 @@ t.test('recalculateOutEdgesOverrides does not forward when no rule matches a tar
301301
t.end()
302302
})
303303

304+
t.test('recalculateOutEdgesOverrides does not forward a version-qualified rule that does not apply', t => {
305+
// The rule names the target's dep but its keySpec (^3) does not intersect the declared range (^1), so the override does not apply and must not flip the target to "has overrides".
306+
const root = new Node({
307+
path: '/path/to/root',
308+
pkg: {
309+
name: 'root',
310+
dependencies: { foo: '1.0.0' },
311+
overrides: { 'leaf@^3': '3.0.0' },
312+
},
313+
loadOverrides: true,
314+
})
315+
316+
const target = new Node({
317+
path: '/path/to/store/foo',
318+
pkg: {
319+
name: 'foo',
320+
version: '1.0.0',
321+
dependencies: { leaf: '^1.0.0' },
322+
},
323+
root,
324+
})
325+
326+
// eslint-disable-next-line no-new
327+
new Link({
328+
pkg: { name: 'foo', version: '1.0.0' },
329+
path: '/path/to/root/node_modules/foo',
330+
realpath: '/path/to/store/foo',
331+
target,
332+
parent: root,
333+
})
334+
335+
t.notOk(target.overrides, 'target.overrides stays undefined for a non-applicable rule')
336+
const leafEdge = target.edgesOut.get('leaf')
337+
t.notOk(leafEdge.overrides, 'edge keeps edge.overrides undefined')
338+
t.equal(leafEdge.spec, '^1.0.0', 'edge spec is unchanged')
339+
t.end()
340+
})
341+
304342
t.test('link to root path gets root as target', t => {
305343
const root = new Node({
306344
path: '/project/root',

0 commit comments

Comments
 (0)