Skip to content

Commit f9e3a80

Browse files
fix(arborist): correct dev/prod dep flags for workspaces under the linked strategy (npm#9655)
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`, `npm query` reports the wrong `dev`/`prod` flags for workspaces and their dependencies. In a workspace project the entire non-root tree is flagged `dev`, so `:is(.prod)` returns almost nothing and `:is(.dev)` returns almost everything — the opposite of the hoisted strategy. This breaks tooling that classifies dependencies via `npm query`, e.g. a license checker that selects `.prod` dependencies. ## Why Two compounding defects, both exercised only by the linked layout. First, the linked strategy does not symlink undeclared workspaces into the root's `node_modules`, so the root's `workspace` edges resolve to `null`. `calcDepFlags` walks outward from the root via edges, dead-ends immediately, and never reaches any workspace or its transitive deps, leaving them at their default `dev=true`. Second, the `node.isLink` branch in `calcDepFlags` assigned target flags unconditionally (`target.dev = link.dev`), unlike every other flag in that file which is only ever unset (true to false). When a target is reachable through more than one link — the norm under linked, where each workspace's own `node_modules` links to a shared target — the last link visited could overwrite an already-correct `dev=false` back to `true`. ## How Make the `calcDepFlags` link branch monotonic: only unset flags, matching the edge walk below it, and queue the target on first visit so its own deps are still walked. A target reachable through multiple links now keeps the most permissive flags regardless of visit order. In `loadActual`, when the install strategy is linked, synthesize the missing root-to-workspace links from the already-loaded workspace targets so the root's workspace edges resolve and flags propagate. The synthesis is gated to linked because under hoisted an unresolved workspace edge is a genuinely missing symlink that reify must recreate, not synthesize. Workspaces already linked into the root `node_modules` are skipped. This targets the path used by `npm query` and non-lockfile `npm sbom`, which force a filesystem read of the actual tree. Commands that load from the hidden lockfile (`npm ls`, `npm outdated`, `npm audit signatures`) are unchanged; their separate, pre-existing linked flag gap is left for a follow-up. ## References Fixes npm#9100
1 parent 6a5bf26 commit f9e3a80

4 files changed

Lines changed: 116 additions & 6 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ 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,
9698
} = options
9799
this.#filter = filter
98100
this.#transplantFilter = transplantFilter
@@ -173,6 +175,20 @@ module.exports = cls => class ActualLoader extends cls {
173175
}
174176
}
175177
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+
}
176192
}
177193

178194
// .npm-extension runs before packageExtensions, matching the ideal-tree resolution order

‎workspaces/arborist/lib/calc-dep-flags.js‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,32 @@ const calcDepFlags = (tree, resetRoot = true) => {
3535
if (node.target == null) {
3636
continue
3737
}
38-
node.target.dev = node.dev
39-
node.target.optional = node.optional
40-
node.target.devOptional = node.devOptional
41-
node.target.peer = node.peer
42-
node.target.extraneous = node.extraneous
43-
queue.push(node.target)
38+
// Only unset flags, matching the edge walk below, so multiple links to one target can't clobber a correct value.
39+
let changed = false
40+
if (node.target.dev && !node.dev) {
41+
node.target.dev = false
42+
changed = true
43+
}
44+
if (node.target.optional && !node.optional) {
45+
node.target.optional = false
46+
changed = true
47+
}
48+
if (node.target.devOptional && !node.devOptional) {
49+
node.target.devOptional = false
50+
changed = true
51+
}
52+
if (node.target.peer && !node.peer) {
53+
node.target.peer = false
54+
changed = true
55+
}
56+
if (node.target.extraneous && !node.extraneous) {
57+
node.target.extraneous = false
58+
changed = true
59+
}
60+
// queue target on first visit so its deps are walked
61+
if (changed || !seen.has(node.target)) {
62+
queue.push(node.target)
63+
}
4464
continue
4565
}
4666

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,49 @@ t.test('transplant workspace targets, even if links not present', async t => {
303303
}), 'do not transplant node named "a"')
304304
})
305305

306+
t.test('linked strategy propagates dep flags into undeclared workspaces', async t => {
307+
// Undeclared workspaces aren't in root node_modules under linked; without the fix their edges resolve to null and stay dev.
308+
// `tools` is also a devDependency of `app`, so it is reached by both a prod root link and a dev `app` link.
309+
// `app` is linked into root node_modules, so its edge already resolves and the synthesis is skipped.
310+
const path = t.testdir({
311+
'package.json': JSON.stringify({
312+
name: 'root',
313+
workspaces: ['packages/*'],
314+
}),
315+
node_modules: {
316+
'@test': {
317+
app: t.fixture('symlink', '../../packages/app'),
318+
},
319+
},
320+
packages: {
321+
app: {
322+
'package.json': JSON.stringify({
323+
name: '@test/app',
324+
version: '1.0.0',
325+
devDependencies: { '@test/tools': '*' },
326+
}),
327+
node_modules: {
328+
'@test': {
329+
tools: t.fixture('symlink', '../../../tools'),
330+
},
331+
},
332+
},
333+
tools: {
334+
'package.json': JSON.stringify({
335+
name: '@test/tools',
336+
version: '1.0.0',
337+
}),
338+
},
339+
},
340+
})
341+
const tree = await loadActual(path, { installStrategy: 'linked' })
342+
const byLocation = loc => [...tree.inventory.values()].find(n => n.location === loc)
343+
const app = byLocation('packages/app')
344+
const tools = byLocation('packages/tools')
345+
t.equal(app.dev, false, 'app workspace is prod')
346+
t.equal(tools.dev, false, 'tools workspace is prod, not overwritten by the dev app link')
347+
})
348+
306349
t.test('load workspaces when loading from hidden lockfile', async t => {
307350
const path = t.testdir({
308351
'package.json': JSON.stringify({

‎workspaces/arborist/test/calc-dep-flags.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,37 @@ t.test('set parents to not extraneous when visiting', t => {
343343
t.end()
344344
})
345345

346+
t.test('multiple links to one target keep the most permissive flags', async t => {
347+
// `tools` is reached by a prod root link and a dev `app` link; the dev link must not overwrite the prod dev flag.
348+
const root = new Node({
349+
path: '/r',
350+
realpath: '/r',
351+
pkg: { name: 'root', workspaces: ['app', 'tools'] },
352+
})
353+
const app = new Node({
354+
path: '/r/app',
355+
realpath: '/r/app',
356+
root,
357+
pkg: { name: 'app', version: '1.0.0', devDependencies: { tools: '*' } },
358+
})
359+
const tools = new Node({
360+
path: '/r/tools',
361+
realpath: '/r/tools',
362+
root,
363+
pkg: { name: 'tools', version: '1.0.0' },
364+
})
365+
new Link({ name: 'app', parent: root, realpath: '/r/app', target: app })
366+
new Link({ name: 'tools', parent: root, realpath: '/r/tools', target: tools })
367+
new Link({ name: 'tools', parent: app, realpath: '/r/tools', target: tools })
368+
root.workspaces = new Map([['app', '/r/app'], ['tools', '/r/tools']])
369+
370+
calcDepFlags(root)
371+
372+
t.equal(tools.dev, false, 'tools is prod via the root workspace link')
373+
t.equal(app.dev, false, 'app is prod via the root workspace link')
374+
t.end()
375+
})
376+
346377
t.test('check null target in link', async t => {
347378
const root = new Link({
348379
path: '/some/path',

0 commit comments

Comments
 (0)