Skip to content

Commit 968e42f

Browse files
fix(arborist): apply overrides across a file:/workspace link boundary (npm#9671)
A root `overrides` entry targeting a transitive dependency was silently dropped when the path to that dependency crossed a `file:`/workspace link, so the dependency resolved to its un-overridden version and the lockfile pinned the wrong version. It reproduced under both the `hoisted` and `linked` install strategies, while the same override applied correctly when the dependency was reached without crossing a link. Override rules propagate through dependency edges, but a Link and its target are not edge-connected, so they are bridged by forwarding the Link's `OverrideSet` to its target. That forwarding ran while the target's subtree was still unbuilt, so its guard found no matching rule and never forwarded, leaving the target and its descendant edges without the rule. `buildIdealTree` now forwards a link's overrides to its target before the target's subtree is resolved, so descendant edges inherit the rule as they are added, matching how a registry node always inherits its ancestor's `OverrideSet`. `loadActual` now repropagates overrides through links once all edges are resolved, so a transitive override reached through a `file:` link is reported as `overridden` rather than `invalid` by `npm ls`. ## References Fixes npm#9659
1 parent ae6dbeb commit 968e42f

4 files changed

Lines changed: 112 additions & 3 deletions

File tree

‎workspaces/arborist/lib/arborist/build-ideal-tree.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,10 @@ This is a one-time fix-up, please be patient...
17291729
!link.target.parent &&
17301730
!link.target.fsParent ||
17311731
unseenLink) {
1732+
// Forward the link's overrides before its subtree resolves, so a root override reaches a transitive dep across the link boundary (npm/cli#9659).
1733+
if (link.overrides) {
1734+
link.target.updateOverridesEdgeInAdded(link.overrides)
1735+
}
17321736
this.addTracker('idealTree', link.target.name, link.target.location)
17331737
this.#depsQueue.push(link.target)
17341738
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module.exports = cls => class ActualLoader extends cls {
7272
// only reset root flags if we're not re-rooting,
7373
// otherwise leave as-is
7474
calcDepFlags(tree, !options.root)
75+
this.#repropagateOverrides()
7576
this.actualTree = treeCheck(tree)
7677
return this.actualTree
7778
})
@@ -204,8 +205,6 @@ module.exports = cls => class ActualLoader extends cls {
204205

205206
this.#transplant(root)
206207

207-
this.#repropagateOverrides()
208-
209208
if (global) {
210209
// need to depend on the children, or else all of them
211210
// will end up being flagged as extraneous, since the
@@ -436,7 +435,7 @@ module.exports = cls => class ActualLoader extends cls {
436435
}
437436
}
438437

439-
// 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).
438+
// Re-forward overrides through links once all edges are resolved, since a Link may forward before its subtree resolves and miss a transitive match (npm/cli#9619, #9659).
440439
#repropagateOverrides () {
441440
if (!this.#actualTree.overrides) {
442441
return

‎workspaces/arborist/test/arborist/build-ideal-tree.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,6 +3565,46 @@ t.test('overrides', async t => {
35653565
t.equal(barEdge.to.version, '2.0.0')
35663566
})
35673567

3568+
t.test('overrides a nested dependency reached through a file: link — npm/cli#9659', async (t) => {
3569+
// A root override targeting a transitive dep must apply even when the path to that dep crosses a file:/workspace link boundary.
3570+
const registry = createRegistry(t, false)
3571+
const barPackuments = registry.packuments([
3572+
{ version: '1.0.0', dependencies: { baz: '^1.0.0' } },
3573+
], 'bar')
3574+
const barManifest = registry.manifest({ name: 'bar', packuments: barPackuments })
3575+
const bazPackuments = registry.packuments(['1.0.0', '2.0.0'], 'baz')
3576+
const bazManifest = registry.manifest({ name: 'baz', packuments: bazPackuments })
3577+
await registry.package({ manifest: barManifest })
3578+
await registry.package({ manifest: bazManifest })
3579+
3580+
const path = t.testdir({
3581+
'package.json': JSON.stringify({
3582+
name: 'root',
3583+
dependencies: {
3584+
a: 'file:./pkgs/a',
3585+
},
3586+
overrides: {
3587+
baz: '2.0.0',
3588+
},
3589+
}),
3590+
pkgs: {
3591+
a: {
3592+
'package.json': JSON.stringify({
3593+
name: 'a',
3594+
version: '1.0.0',
3595+
dependencies: { bar: '1.0.0' },
3596+
}),
3597+
},
3598+
},
3599+
})
3600+
3601+
const tree = await buildIdeal(path)
3602+
3603+
const barNode = tree.inventory.query('name', 'bar').values().next().value
3604+
const bazEdge = barNode.edgesOut.get('baz')
3605+
t.equal(bazEdge.to.version, '2.0.0', 'override applies across the file: link')
3606+
})
3607+
35683608
t.test('does not override a nested dependency when parent spec does not match', async (t) => {
35693609
const registry = createRegistry(t, false)
35703610
const fooPackuments = registry.packuments([

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,72 @@ t.test('forwards a transitive override through a linked store link — npm/cli#9
774774
t.equal(leafEdge.to.target.version, '2.0.0', 'edge resolves to the overridden package')
775775
})
776776

777+
t.test('forwards a transitive override across a file: link boundary — npm/cli#9659', async t => {
778+
// The override path crosses a file: link (root -> a) before entering the store chain (a -> b -> leaf).
779+
// Loading from the hidden lockfile, the file link target's subtree resolves late, so override repropagation must run once all edges are resolved or `npm ls` reports the edge `invalid`.
780+
const path = t.testdir({
781+
'package.json': JSON.stringify({
782+
name: 'root',
783+
version: '1.0.0',
784+
dependencies: { a: 'file:./pkgs/a' },
785+
overrides: { leaf: '2.0.0' },
786+
}),
787+
pkgs: {
788+
a: {
789+
'package.json': JSON.stringify({ name: 'a', version: '1.0.0', dependencies: { b: '1.0.0' } }),
790+
node_modules: {
791+
b: t.fixture('symlink', '../../../node_modules/.store/[email protected]/node_modules/b'),
792+
},
793+
},
794+
},
795+
node_modules: {
796+
a: t.fixture('symlink', '../pkgs/a'),
797+
'.store': {
798+
799+
node_modules: {
800+
// leaf is declared ^1.0.0 but overridden to 2.0.0, outside that range
801+
b: { 'package.json': JSON.stringify({ name: 'b', version: '1.0.0', dependencies: { leaf: '^1.0.0' } }) },
802+
leaf: t.fixture('symlink', '../../[email protected]/node_modules/leaf'),
803+
},
804+
},
805+
806+
node_modules: {
807+
leaf: { 'package.json': JSON.stringify({ name: 'leaf', version: '2.0.0' }) },
808+
},
809+
},
810+
},
811+
'.package-lock.json': JSON.stringify({
812+
name: 'root',
813+
lockfileVersion: 3,
814+
requires: true,
815+
packages: {
816+
'': { name: 'root', version: '1.0.0', dependencies: { a: 'file:./pkgs/a' }, overrides: { leaf: '2.0.0' } },
817+
'node_modules/a': { resolved: 'pkgs/a', link: true },
818+
'pkgs/a': { version: '1.0.0', dependencies: { b: '1.0.0' } },
819+
'pkgs/a/node_modules/b': { resolved: 'node_modules/.store/[email protected]/node_modules/b', link: true },
820+
'node_modules/.store/[email protected]': {},
821+
'node_modules/.store/[email protected]/node_modules/b': { version: '1.0.0', dependencies: { leaf: '^1.0.0' } },
822+
'node_modules/.store/[email protected]/node_modules/leaf': { resolved: 'node_modules/.store/[email protected]/node_modules/leaf', link: true },
823+
'node_modules/.store/[email protected]': {},
824+
'node_modules/.store/[email protected]/node_modules/leaf': { version: '2.0.0' },
825+
},
826+
}),
827+
},
828+
})
829+
// make the hidden lockfile the newest entry so loadActual loads from it
830+
const hidden = resolve(path, 'node_modules/.package-lock.json')
831+
const then = Date.now() + 10000
832+
fs.utimesSync(hidden, new Date(then), new Date(then))
833+
834+
const tree = await loadActual(path)
835+
const b = tree.children.get('a').target.edgesOut.get('b').to.target
836+
const leafEdge = b.edgesOut.get('leaf')
837+
t.ok(leafEdge && !leafEdge.error, 'transitive overridden edge resolves without error')
838+
t.ok(leafEdge.overrides, 'edge carries the override rule')
839+
t.equal(leafEdge.spec, '2.0.0', 'edge spec is the overridden version')
840+
t.equal(leafEdge.to.target.version, '2.0.0', 'edge resolves to the overridden package')
841+
})
842+
777843
t.test('store nodes do not load devDependencies as required edges', async t => {
778844
// 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).
779845
const path = t.testdir({

0 commit comments

Comments
 (0)