Skip to content

Commit ae6dbeb

Browse files
fix(arborist): surface undeclared workspaces under the linked strategy (npm#9657)
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`, a declared workspace that the root does not depend on is installed on disk but was invisible to `npm ls --all`, `npm ls --workspaces`, and `npm query ':root > *'`, all of which reported an empty tree. The hoisted strategy lists every workspace. Undeclared workspaces are intentionally not symlinked into the root `node_modules` under the linked strategy, so when `loadActual` rebuilds the actual tree (from the filesystem scan or the hidden lockfile, neither of which records a `node_modules/<ws>` entry) the root's `workspace` edges resolve to nothing and the workspaces drop out of the tree that `ls` and `query` traverse. `npm ls` then hid those missing edges entirely to avoid a false `UNMET DEPENDENCY`. The fix adds `loadActual` post-processing that, for the linked strategy, synthesizes an in-memory `Link` at `node_modules/<ws>` for each undeclared workspace so its root edge resolves, matching the logical layout the regular `package-lock.json` already records. This is introspection-only and writes nothing to disk. A declared workspace whose root link is genuinely missing is left alone so it still reports `UNMET`, an existing root child of the same name is never replaced, and a workspace that was not loaded is skipped. With the edges now resolving, the workspace-skip branch that `npm ls` used to suppress them is removed. ## References Fixes npm#9618
1 parent 0c33947 commit ae6dbeb

5 files changed

Lines changed: 195 additions & 42 deletions

File tree

‎lib/commands/ls.js‎

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class LS extends ArboristWorkspaceCmd {
135135
omit,
136136
}) : () => true)
137137
.filter(installStrategy === 'linked'
138-
? filterLinkedStrategyEdges({ node, currentDepth })
138+
? filterLinkedStrategyEdges({ currentDepth })
139139
: () => true)
140140
.map(mapEdgesToNodes({ seenPaths }))
141141
.concat(appendExtraneousChildren({ node, seenPaths }))
@@ -442,32 +442,15 @@ const getJsonOutputItem = (node, { global, long }) => {
442442
return augmentItemWithIncludeMetadata(node, item)
443443
}
444444

445-
// In linked strategy, two types of edges produce false UNMET DEPENDENCYs:
446-
// 1. Workspace edges for undeclared workspaces: the lockfile records edges from root to ALL workspaces, but only declared workspaces are hoisted to root/node_modules in linked mode. Undeclared ones are intentionally absent.
447-
// 2. Dev edges on non-root packages: store package link targets have no parent in the node tree, so they are treated as "top" nodes and their devDependencies are loaded as edges. Those devDeps are never installed.
448-
const filterLinkedStrategyEdges = ({ node, currentDepth }) => {
449-
const declaredDeps = new Set(Object.keys(Object.assign({},
450-
node.target.package.dependencies,
451-
node.target.package.devDependencies,
452-
node.target.package.optionalDependencies,
453-
node.target.package.peerDependencies
454-
)))
455-
456-
return (edge) => {
457-
// Skip workspace edges for undeclared workspaces at root level
458-
if (currentDepth === 0 && edge.type === 'workspace' && edge.missing) {
459-
if (!declaredDeps.has(edge.name)) {
460-
return false
461-
}
462-
}
463-
464-
// Skip dev edges for non-root packages (store packages)
465-
if (currentDepth > 0 && edge.dev) {
466-
return false
467-
}
468-
469-
return true
445+
// In linked strategy, dev edges on non-root packages produce false UNMET DEPENDENCYs: store package link targets have no parent in the node tree, so they are treated as "top" nodes and their devDependencies are loaded as edges. Those devDeps are never installed.
446+
// Undeclared workspaces no longer need filtering here: loadActual synthesizes their root links so their edges resolve instead of reporting missing.
447+
const filterLinkedStrategyEdges = ({ currentDepth }) => (edge) => {
448+
// Skip dev edges for non-root packages (store packages)
449+
if (currentDepth > 0 && edge.dev) {
450+
return false
470451
}
452+
453+
return true
471454
}
472455

473456
const filterByEdgesTypes = ({ link, omit }) => (edge) => {

‎test/lib/commands/ls.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5425,6 +5425,7 @@ t.test('ls --install-strategy=linked', async t => {
54255425
const output = cleanCwd(result())
54265426
t.notMatch(output, /UNMET DEPENDENCY/, 'should not report undeclared workspace as UNMET DEPENDENCY')
54275427
t.match(output, /workspace-a/, 'should list declared workspace')
5428+
t.match(output, /workspace-b/, 'should list undeclared workspace (npm/cli#9618)')
54285429
})
54295430

54305431
t.test('should not report devDeps of store packages as UNMET DEPENDENCY', async t => {

‎test/lib/commands/query.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,24 @@ t.test('missing', async t => {
489489
await npm.exec('query', [':missing'])
490490
t.matchSnapshot(joinedOutput(), 'should return missing node')
491491
})
492+
493+
t.test('linked strategy surfaces undeclared workspaces', async t => {
494+
// npm/cli#9618: undeclared workspaces are not symlinked into root node_modules under linked, but must remain visible to `npm query`.
495+
const { npm, joinedOutput } = await loadMockNpm(t, {
496+
config: { 'install-strategy': 'linked' },
497+
prefixDir: {
498+
'package.json': JSON.stringify({
499+
name: 'root',
500+
version: '1.0.0',
501+
workspaces: ['packages/*'],
502+
}),
503+
packages: {
504+
a: { 'package.json': JSON.stringify({ name: 'a', version: '1.0.0' }) },
505+
b: { 'package.json': JSON.stringify({ name: 'b', version: '1.0.0' }) },
506+
},
507+
},
508+
})
509+
await npm.exec('query', [':root > *'])
510+
const names = JSON.parse(joinedOutput()).map(n => n.name).sort()
511+
t.same(names, ['a', 'b'], 'both undeclared workspaces are listed')
512+
})

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

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ module.exports = cls => class ActualLoader extends cls {
9393
transplantFilter = () => true,
9494
ignoreMissing = false,
9595
forceActual = false,
96-
// always present: the public loadActual merges this.options, which sets installStrategy
97-
installStrategy,
9896
} = options
9997
this.#filter = filter
10098
this.#transplantFilter = transplantFilter
@@ -145,6 +143,7 @@ module.exports = cls => class ActualLoader extends cls {
145143
root: this.#actualTree,
146144
})
147145
await this[_setWorkspaces](this.#actualTree)
146+
this.#linkActualWorkspaces()
148147

149148
this.#transplant(root)
150149
return this.#actualTree
@@ -175,22 +174,10 @@ module.exports = cls => class ActualLoader extends cls {
175174
}
176175
}
177176
await Promise.all(promises)
178-
179-
// Linked undeclared workspaces aren't symlinked into root node_modules, so their edges resolve to null and flags never propagate.
180-
// Synthesize the links from the loaded targets. Gated to linked; under hoisted a null workspace edge is a real missing link.
181-
if (installStrategy === 'linked') {
182-
for (const [name, path] of this.#actualTree.workspaces.entries()) {
183-
const edge = this.#actualTree.edgesOut.get(name)
184-
// skip workspaces already linked into root node_modules (declared deps)
185-
if (edge.to) {
186-
continue
187-
}
188-
const target = this.#cache.get(path)
189-
new Link({ parent: this.#actualTree, name, realpath: path, target, pkg: target.package })
190-
}
191-
}
192177
}
193178

179+
this.#linkActualWorkspaces()
180+
194181
// .npm-extension runs before packageExtensions, matching the ideal-tree resolution order
195182
await this.#applyNpmExtension()
196183
this.#applyPackageExtensions()
@@ -235,6 +222,48 @@ module.exports = cls => class ActualLoader extends cls {
235222
return this.#actualTree
236223
}
237224

225+
// Under the linked (isolated) strategy, workspaces the root does not depend on are not symlinked into the root node_modules, so neither the on-disk scan nor the hidden lockfile gives the root's workspace edges a node_modules/<ws> link to resolve to.
226+
// Synthesize those links from the authoritative workspaces map so npm ls and npm query surface the workspaces, matching hoisted and the logical package-lock.
227+
#linkActualWorkspaces () {
228+
const root = this.#actualTree
229+
if (this.options.installStrategy !== 'linked' || !root.workspaces) {
230+
return
231+
}
232+
// Declared workspaces ARE symlinked at root node_modules under linked, so a missing link there is a real problem that must surface as UNMET.
233+
// Only undeclared workspaces are intentionally absent from root node_modules and need a synthesized link so their root edges resolve.
234+
const pkg = root.package
235+
const declared = new Set(Object.keys(Object.assign({},
236+
pkg.dependencies,
237+
pkg.devDependencies,
238+
pkg.optionalDependencies,
239+
pkg.peerDependencies
240+
)))
241+
// index loaded workspace targets by both path and realpath, so a workspace reached through a symlinked dir still matches
242+
const byLoc = new Map()
243+
for (const node of root.fsChildren) {
244+
byLoc.set(node.path, node)
245+
byLoc.set(node.realpath, node)
246+
}
247+
for (const [name, path] of root.workspaces.entries()) {
248+
// declared workspaces, and any name already a root child, resolve their edge without a synthesized link
249+
if (declared.has(name) || root.children.has(name)) {
250+
continue
251+
}
252+
const target = byLoc.get(path)
253+
if (target) {
254+
// eslint-disable-next-line no-new
255+
new Link({
256+
name,
257+
path: resolve(root.path, 'node_modules', name),
258+
realpath: target.realpath,
259+
target,
260+
parent: root,
261+
root,
262+
})
263+
}
264+
}
265+
}
266+
238267
#transplant (root) {
239268
if (!root || root === this.#actualTree) {
240269
return

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

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,125 @@ t.test('linked strategy propagates dep flags into undeclared workspaces', async
346346
t.equal(tools.dev, false, 'tools workspace is prod, not overwritten by the dev app link')
347347
})
348348

349+
t.test('linked strategy surfaces undeclared workspaces', async t => {
350+
// Under linked, undeclared workspaces are not symlinked into root node_modules, so loadActual must synthesize their root links for their edges to resolve (npm/cli#9618).
351+
const fixture = {
352+
'package.json': JSON.stringify({
353+
name: 'root',
354+
version: '1.0.0',
355+
workspaces: ['packages/*'],
356+
// declared workspace is symlinked at root; undeclared one is not
357+
dependencies: { a: '*' },
358+
}),
359+
node_modules: {
360+
a: t.fixture('symlink', '../packages/a'),
361+
},
362+
packages: {
363+
a: { 'package.json': JSON.stringify({ name: 'a', version: '1.2.3' }) },
364+
b: { 'package.json': JSON.stringify({ name: 'b', version: '1.2.3' }) },
365+
},
366+
}
367+
368+
const assertVisible = (t, tree) => {
369+
const aLink = tree.children.get('a')
370+
const bLink = tree.children.get('b')
371+
t.ok(aLink, 'declared workspace a present (from disk symlink)')
372+
t.ok(bLink, 'undeclared workspace b synthesized')
373+
t.equal(tree.edgesOut.get('b').to, bLink, 'workspace edge b resolves to its link')
374+
t.notOk(tree.edgesOut.get('b').missing, 'workspace edge b is not missing')
375+
t.equal(bLink.target.version, '1.2.3', 'b target loaded')
376+
}
377+
378+
t.test('filesystem scan', async t => {
379+
const path = t.testdir(fixture)
380+
assertVisible(t, await loadActual(path, { installStrategy: 'linked' }))
381+
})
382+
383+
t.test('hidden lockfile', async t => {
384+
const path = t.testdir({
385+
...fixture,
386+
node_modules: {
387+
a: t.fixture('symlink', '../packages/a'),
388+
'.package-lock.json': JSON.stringify({
389+
name: 'root',
390+
lockfileVersion: 3,
391+
requires: true,
392+
packages: {
393+
'node_modules/a': { resolved: 'packages/a', link: true },
394+
'packages/a': { version: '1.2.3' },
395+
'packages/b': { version: '1.2.3' },
396+
},
397+
}),
398+
},
399+
})
400+
const hidden = resolve(path, 'node_modules/.package-lock.json')
401+
const then = Date.now() + 10000
402+
fs.utimesSync(hidden, new Date(then), new Date(then))
403+
assertVisible(t, await loadActual(path, { installStrategy: 'linked' }))
404+
})
405+
406+
t.test('no workspaces is a no-op', async t => {
407+
const path = t.testdir({
408+
'package.json': JSON.stringify({ name: 'root', version: '1.0.0' }),
409+
})
410+
const tree = await loadActual(path, { installStrategy: 'linked' })
411+
t.notOk(tree.workspaces, 'no workspaces set')
412+
t.equal(tree.children.size, 0, 'no links synthesized')
413+
})
414+
415+
t.test('does not clobber an existing root child', async t => {
416+
// an undeclared workspace that already has a root symlink keeps that child instead of a synthesized link
417+
const path = t.testdir({
418+
'package.json': JSON.stringify({
419+
name: 'root',
420+
version: '1.0.0',
421+
workspaces: ['packages/*'],
422+
}),
423+
node_modules: {
424+
b: t.fixture('symlink', '../packages/b'),
425+
},
426+
packages: {
427+
a: { 'package.json': JSON.stringify({ name: 'a', version: '1.2.3' }) },
428+
b: { 'package.json': JSON.stringify({ name: 'b', version: '1.2.3' }) },
429+
},
430+
})
431+
const tree = await loadActual(path, { installStrategy: 'linked' })
432+
t.equal(tree.children.get('b').realpath, resolve(path, 'packages/b'), 'existing b child preserved')
433+
t.ok(tree.children.get('a'), 'undeclared a still synthesized')
434+
})
435+
436+
t.test('skips a workspace with no loaded target', async t => {
437+
// hidden lockfile omits packages/b, so it is in the workspaces map but never loaded; it must be skipped, not crash
438+
const path = t.testdir({
439+
'package.json': JSON.stringify({
440+
name: 'root',
441+
version: '1.0.0',
442+
workspaces: ['packages/*'],
443+
}),
444+
node_modules: {
445+
'.package-lock.json': JSON.stringify({
446+
name: 'root',
447+
lockfileVersion: 3,
448+
requires: true,
449+
packages: {
450+
'packages/a': { version: '1.2.3' },
451+
},
452+
}),
453+
},
454+
packages: {
455+
a: { 'package.json': JSON.stringify({ name: 'a', version: '1.2.3' }) },
456+
b: { 'package.json': JSON.stringify({ name: 'b', version: '1.2.3' }) },
457+
},
458+
})
459+
const hidden = resolve(path, 'node_modules/.package-lock.json')
460+
const then = Date.now() + 10000
461+
fs.utimesSync(hidden, new Date(then), new Date(then))
462+
const tree = await loadActual(path, { installStrategy: 'linked' })
463+
t.ok(tree.children.get('a'), 'loaded workspace a synthesized')
464+
t.notOk(tree.children.get('b'), 'unloaded workspace b skipped')
465+
})
466+
})
467+
349468
t.test('load workspaces when loading from hidden lockfile', async t => {
350469
const path = t.testdir({
351470
'package.json': JSON.stringify({

0 commit comments

Comments
 (0)