Skip to content

Commit f580889

Browse files
fix(arborist): load transitive optional deps into linked actual tree (#9665)
Backport of #9654 to `release/v11`. Co-authored-by: Manzoor Wani <[email protected]>
1 parent 8f13beb commit f580889

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,10 @@ module.exports = cls => class ActualLoader extends cls {
371371

372372
const depPromises = []
373373
for (const [name, edge] of node.edgesOut.entries()) {
374-
const notMissing = !edge.missing &&
375-
!(edge.to && (edge.to.dummy || edge.to.parent !== node))
376-
if (notMissing) {
374+
// An unresolved optional edge reports missing === false, so check the target directly.
375+
// Otherwise an installed optional dep that lives only as a store sibling is never loaded.
376+
const resolved = edge.to && !edge.to.dummy && edge.to.parent === node
377+
if (resolved) {
377378
continue
378379
}
379380

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,43 @@ t.test('store nodes do not load devDependencies as required edges', async t => {
531531
t.notOk(dep.edgesOut.get('a-dev-dep'), 'devDependency of a store node is not a required edge')
532532
})
533533

534+
t.test('loads an installed transitive optional dep from the linked store', async t => {
535+
// A transitive optional dep lives as a store sibling, and its edge reports missing === false despite having no target.
536+
// #findMissingEdges must still walk it, or npm sbom/query omit the installed dep.
537+
const path = t.testdir({
538+
'package.json': JSON.stringify({
539+
name: 'root',
540+
version: '1.0.0',
541+
dependencies: { dep: '1.0.0' },
542+
}),
543+
node_modules: {
544+
dep: t.fixture('symlink', '.store/[email protected]/node_modules/dep'),
545+
'.store': {
546+
547+
node_modules: {
548+
dep: {
549+
'package.json': JSON.stringify({
550+
name: 'dep',
551+
version: '1.0.0',
552+
optionalDependencies: { opt: '^1.0.0' },
553+
}),
554+
},
555+
// the optional dep is installed as a store sibling of its consumer
556+
opt: { 'package.json': JSON.stringify({ name: 'opt', version: '1.0.0' }) },
557+
},
558+
},
559+
},
560+
},
561+
})
562+
563+
const tree = await loadActual(path)
564+
const dep = tree.children.get('dep').target
565+
const edge = dep.edgesOut.get('opt')
566+
t.ok(edge && !edge.error, 'optional edge resolves')
567+
t.equal(edge.to?.name, 'opt', 'edge resolves to the installed package')
568+
t.ok([...tree.inventory.values()].some(n => n.name === 'opt'), 'opt is present in the inventory')
569+
})
570+
534571
t.test('a project located under a .store path still loads its own devDependencies', async t => {
535572
// The loaded root must never be treated as a store node, even when its own path happens to sit under a node_modules/.store directory.
536573
const path = t.testdir({

0 commit comments

Comments
 (0)