Skip to content

Commit 719de1e

Browse files
fix(arborist): apply overrides across a file: link (backport release/v11) (#9673)
Backport of #9671 to `release/v11`. 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. `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`. Backport notes: clean cherry-pick except for the `#repropagateOverrides` comment, which conflicted only because `release/v11` does not have the adjacent `#applyPackageExtensions` method; resolved by keeping the v11 structure and applying the comment update. ## References Fixes #9659 Backport of #9671
1 parent cd57139 commit 719de1e

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
@@ -1595,6 +1595,10 @@ This is a one-time fix-up, please be patient...
15951595
!link.target.parent &&
15961596
!link.target.fsParent ||
15971597
unseenLink) {
1598+
// Forward the link's overrides before its subtree resolves, so a root override reaches a transitive dep across the link boundary (npm/cli#9659).
1599+
if (link.overrides) {
1600+
link.target.updateOverridesEdgeInAdded(link.overrides)
1601+
}
15981602
this.addTracker('idealTree', link.target.name, link.target.location)
15991603
this.#depsQueue.push(link.target)
16001604
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ module.exports = cls => class ActualLoader extends cls {
7070
// only reset root flags if we're not re-rooting,
7171
// otherwise leave as-is
7272
calcDepFlags(tree, !options.root)
73+
this.#repropagateOverrides()
7374
this.actualTree = treeCheck(tree)
7475
return this.actualTree
7576
})
@@ -198,8 +199,6 @@ module.exports = cls => class ActualLoader extends cls {
198199

199200
this.#transplant(root)
200201

201-
this.#repropagateOverrides()
202-
203202
if (global) {
204203
// need to depend on the children, or else all of them
205204
// will end up being flagged as extraneous, since the
@@ -402,7 +401,7 @@ module.exports = cls => class ActualLoader extends cls {
402401
}
403402
}
404403

405-
// 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).
404+
// 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).
406405
#repropagateOverrides () {
407406
if (!this.#actualTree.overrides) {
408407
return

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

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

3658+
t.test('overrides a nested dependency reached through a file: link — npm/cli#9659', async (t) => {
3659+
// A root override targeting a transitive dep must apply even when the path to that dep crosses a file:/workspace link boundary.
3660+
const registry = createRegistry(t, false)
3661+
const barPackuments = registry.packuments([
3662+
{ version: '1.0.0', dependencies: { baz: '^1.0.0' } },
3663+
], 'bar')
3664+
const barManifest = registry.manifest({ name: 'bar', packuments: barPackuments })
3665+
const bazPackuments = registry.packuments(['1.0.0', '2.0.0'], 'baz')
3666+
const bazManifest = registry.manifest({ name: 'baz', packuments: bazPackuments })
3667+
await registry.package({ manifest: barManifest })
3668+
await registry.package({ manifest: bazManifest })
3669+
3670+
const path = t.testdir({
3671+
'package.json': JSON.stringify({
3672+
name: 'root',
3673+
dependencies: {
3674+
a: 'file:./pkgs/a',
3675+
},
3676+
overrides: {
3677+
baz: '2.0.0',
3678+
},
3679+
}),
3680+
pkgs: {
3681+
a: {
3682+
'package.json': JSON.stringify({
3683+
name: 'a',
3684+
version: '1.0.0',
3685+
dependencies: { bar: '1.0.0' },
3686+
}),
3687+
},
3688+
},
3689+
})
3690+
3691+
const tree = await buildIdeal(path)
3692+
3693+
const barNode = tree.inventory.query('name', 'bar').values().next().value
3694+
const bazEdge = barNode.edgesOut.get('baz')
3695+
t.equal(bazEdge.to.version, '2.0.0', 'override applies across the file: link')
3696+
})
3697+
36583698
t.test('does not override a nested dependency when parent spec does not match', async (t) => {
36593699
const registry = createRegistry(t, false)
36603700
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
@@ -727,6 +727,72 @@ t.test('forwards a transitive override through a linked store link — npm/cli#9
727727
t.equal(leafEdge.to.target.version, '2.0.0', 'edge resolves to the overridden package')
728728
})
729729

730+
t.test('forwards a transitive override across a file: link boundary — npm/cli#9659', async t => {
731+
// The override path crosses a file: link (root -> a) before entering the store chain (a -> b -> leaf).
732+
// 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`.
733+
const path = t.testdir({
734+
'package.json': JSON.stringify({
735+
name: 'root',
736+
version: '1.0.0',
737+
dependencies: { a: 'file:./pkgs/a' },
738+
overrides: { leaf: '2.0.0' },
739+
}),
740+
pkgs: {
741+
a: {
742+
'package.json': JSON.stringify({ name: 'a', version: '1.0.0', dependencies: { b: '1.0.0' } }),
743+
node_modules: {
744+
b: t.fixture('symlink', '../../../node_modules/.store/[email protected]/node_modules/b'),
745+
},
746+
},
747+
},
748+
node_modules: {
749+
a: t.fixture('symlink', '../pkgs/a'),
750+
'.store': {
751+
752+
node_modules: {
753+
// leaf is declared ^1.0.0 but overridden to 2.0.0, outside that range
754+
b: { 'package.json': JSON.stringify({ name: 'b', version: '1.0.0', dependencies: { leaf: '^1.0.0' } }) },
755+
leaf: t.fixture('symlink', '../../[email protected]/node_modules/leaf'),
756+
},
757+
},
758+
759+
node_modules: {
760+
leaf: { 'package.json': JSON.stringify({ name: 'leaf', version: '2.0.0' }) },
761+
},
762+
},
763+
},
764+
'.package-lock.json': JSON.stringify({
765+
name: 'root',
766+
lockfileVersion: 3,
767+
requires: true,
768+
packages: {
769+
'': { name: 'root', version: '1.0.0', dependencies: { a: 'file:./pkgs/a' }, overrides: { leaf: '2.0.0' } },
770+
'node_modules/a': { resolved: 'pkgs/a', link: true },
771+
'pkgs/a': { version: '1.0.0', dependencies: { b: '1.0.0' } },
772+
'pkgs/a/node_modules/b': { resolved: 'node_modules/.store/[email protected]/node_modules/b', link: true },
773+
'node_modules/.store/[email protected]': {},
774+
'node_modules/.store/[email protected]/node_modules/b': { version: '1.0.0', dependencies: { leaf: '^1.0.0' } },
775+
'node_modules/.store/[email protected]/node_modules/leaf': { resolved: 'node_modules/.store/[email protected]/node_modules/leaf', link: true },
776+
'node_modules/.store/[email protected]': {},
777+
'node_modules/.store/[email protected]/node_modules/leaf': { version: '2.0.0' },
778+
},
779+
}),
780+
},
781+
})
782+
// make the hidden lockfile the newest entry so loadActual loads from it
783+
const hidden = resolve(path, 'node_modules/.package-lock.json')
784+
const then = Date.now() + 10000
785+
fs.utimesSync(hidden, new Date(then), new Date(then))
786+
787+
const tree = await loadActual(path)
788+
const b = tree.children.get('a').target.edgesOut.get('b').to.target
789+
const leafEdge = b.edgesOut.get('leaf')
790+
t.ok(leafEdge && !leafEdge.error, 'transitive overridden edge resolves without error')
791+
t.ok(leafEdge.overrides, 'edge carries the override rule')
792+
t.equal(leafEdge.spec, '2.0.0', 'edge spec is the overridden version')
793+
t.equal(leafEdge.to.target.version, '2.0.0', 'edge resolves to the overridden package')
794+
})
795+
730796
t.test('store nodes do not load devDependencies as required edges', async t => {
731797
// 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).
732798
const path = t.testdir({

0 commit comments

Comments
 (0)