Repository navigation
Commit f9e3a80
authored
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#91001 parent 6a5bf26 commit f9e3a80
4 files changed
Lines changed: 116 additions & 6 deletions
File tree
- workspaces/arborist
- lib
- arborist
- test
- arborist
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
| 97 | + | |
96 | 98 | | |
97 | 99 | | |
98 | 100 | | |
| |||
173 | 175 | | |
174 | 176 | | |
175 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
176 | 192 | | |
177 | 193 | | |
178 | 194 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
44 | 64 | | |
45 | 65 | | |
46 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
303 | 303 | | |
304 | 304 | | |
305 | 305 | | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
306 | 349 | | |
307 | 350 | | |
308 | 351 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
343 | 343 | | |
344 | 344 | | |
345 | 345 | | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
346 | 377 | | |
347 | 378 | | |
348 | 379 | | |
| |||
0 commit comments