Skip to content

Commit ede32d3

Browse files
fix(arborist): forward transitive overrides through linked store links (#9658) (backport release/v11) (#9668)
Backport of #9658 to `release/v11`. Under `install-strategy=linked`, an override forcing a transitive dependency outside its dependent's declared range was applied on disk but reported as `invalid` by `npm ls --all`, which then exited 1 (`ELSPROBLEMS`); hoisted reports it as `overridden` and exits 0. It only surfaced when the overridden package's dependent was itself a transitive (store) package. `Link.recalculateOutEdgesOverrides` only forwarded an OverrideSet to a store link's target when a rule named a direct dependency of the target, so the chain to a deeper `dependent -> overridden` edge never received the rule. The fix walks the target's subtree and forwards when a rule actually applies to a reachable edge (matched via `getEdgeRule` on name and spec, preserving #9357), and `loadActual` re-forwards through links once the tree is complete. Cherry-picked cleanly except for a context-only conflict in `load-actual.js` and its test file: the unrelated `packageExtensions` and `.npm-extension` methods and the `applies root packageExtensions to a linked actual tree` test are not present on `release/v11`, so they were dropped from the resolution. Only this fix's changes are included. ## References Backports #9658
1 parent f503b07 commit ede32d3

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
@@ -211,6 +211,8 @@ module.exports = cls => class ActualLoader extends cls {
211211

212212
this.#transplant(root)
213213

214+
this.#repropagateOverrides()
215+
214216
if (global) {
215217
// need to depend on the children, or else all of them
216218
// will end up being flagged as extraneous, since the
@@ -371,6 +373,18 @@ module.exports = cls => class ActualLoader extends cls {
371373
}
372374
}
373375

376+
// 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).
377+
#repropagateOverrides () {
378+
if (!this.#actualTree.overrides) {
379+
return
380+
}
381+
for (const node of this.#actualTree.inventory.values()) {
382+
if (node.isLink && node.overrides) {
383+
node.recalculateOutEdgesOverrides()
384+
}
385+
}
386+
}
387+
374388
async #findMissingEdges () {
375389
// try to resolve any missing edges by walking up the directory tree,
376390
// checking for the package in each node_modules folder. stop at the

‎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
@@ -542,6 +542,72 @@ t.test('loading a workspace maintains overrides', async t => {
542542
t.equal(tree.overrides, fooEdge.overrides, 'foo edge got the correct overrides')
543543
})
544544

545+
t.test('forwards a transitive override through a linked store link — npm/cli#9619', async t => {
546+
// 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`.
547+
// Shaped like glob -> minimatch -> brace-expansion: the override target sits two links deep, with a shared node reached by two paths.
548+
const path = t.testdir({
549+
'package.json': JSON.stringify({
550+
name: 'root',
551+
version: '1.0.0',
552+
dependencies: { a: '1.0.0' },
553+
overrides: { leaf: '2.0.0' },
554+
}),
555+
node_modules: {
556+
a: t.fixture('symlink', '.store/[email protected]/node_modules/a'),
557+
'.store': {
558+
559+
node_modules: {
560+
a: { 'package.json': JSON.stringify({ name: 'a', version: '1.0.0', dependencies: { b: '1.0.0', c: '1.0.0' } }) },
561+
b: t.fixture('symlink', '../../[email protected]/node_modules/b'),
562+
c: t.fixture('symlink', '../../[email protected]/node_modules/c'),
563+
},
564+
},
565+
566+
node_modules: {
567+
// leaf is declared ^1.0.0 but overridden to 2.0.0, which is outside that range
568+
b: { 'package.json': JSON.stringify({ name: 'b', version: '1.0.0', dependencies: { leaf: '^1.0.0', shared: '1.0.0' } }) },
569+
leaf: t.fixture('symlink', '../../[email protected]/node_modules/leaf'),
570+
shared: t.fixture('symlink', '../../[email protected]/node_modules/shared'),
571+
},
572+
},
573+
574+
node_modules: {
575+
// c's subtree has no overridden package but reaches shared via both c and c->d, so its walk revisits a seen node
576+
c: { 'package.json': JSON.stringify({ name: 'c', version: '1.0.0', dependencies: { d: '1.0.0', shared: '1.0.0' } }) },
577+
d: t.fixture('symlink', '../../[email protected]/node_modules/d'),
578+
shared: t.fixture('symlink', '../../[email protected]/node_modules/shared'),
579+
},
580+
},
581+
582+
node_modules: {
583+
d: { 'package.json': JSON.stringify({ name: 'd', version: '1.0.0', dependencies: { shared: '1.0.0' } }) },
584+
shared: t.fixture('symlink', '../../[email protected]/node_modules/shared'),
585+
},
586+
},
587+
588+
node_modules: {
589+
leaf: { 'package.json': JSON.stringify({ name: 'leaf', version: '2.0.0' }) },
590+
},
591+
},
592+
593+
node_modules: {
594+
shared: { 'package.json': JSON.stringify({ name: 'shared', version: '1.0.0' }) },
595+
},
596+
},
597+
},
598+
},
599+
})
600+
601+
const tree = await loadActual(path)
602+
const b = tree.children.get('a').target.edgesOut.get('b').to.target
603+
const leafEdge = b.edgesOut.get('leaf')
604+
t.ok(leafEdge && !leafEdge.error, 'transitive overridden edge resolves without error')
605+
t.ok(leafEdge.overrides, 'edge carries the override rule')
606+
t.equal(leafEdge.spec, '2.0.0', 'edge spec is the overridden version')
607+
t.equal(leafEdge.rawSpec, '^1.0.0', 'edge rawSpec is the original declared range')
608+
t.equal(leafEdge.to.target.version, '2.0.0', 'edge resolves to the overridden package')
609+
})
610+
545611
t.test('store nodes do not load devDependencies as required edges', async t => {
546612
// 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).
547613
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)