Debian Codegraph @

workspaces/arborist/test/isolated-mode.js

raw
1 const tap = require('tap')
2 const fs = require('node:fs')
3 const path = require('node:path')
4 const os = require('node:os')
5
6 const oldMap = Map
7
8 class newMap extends oldMap {
9 get (prop) {
10 const newThis = this.__target || this
11 return oldMap.prototype.get.bind(newThis)(prop)
12 }
13 }
14 Map = newMap
15
16 const getTempDir = () => fs.realpathSync(os.tmpdir())
17
18 const Arborist = require('../lib/arborist')
19 const { getRepo } = require('./fixtures/isolated-nock')
20
21 /**
22 * The testing framework here is work in progress, in particular it does not have nice ergonomics.
23 * The syntactic sugar for this framework will be introduced over time as we add more features.
24 *
25 * The framework has two parts:
26 * - Mocking: The tool generates a test repo based on a declarative list of packages.
27 * - Asserting: Some generic rules are defined which assert a particular contract of a resolved dependency graph.
28 * For each test we declaratively define the expected resolved dependency graph and apply all the rules to it.
29 * This validates that arborist produced the expected dependency graph and respect all the contracts set by the rules.
30 *
31 * The automatic assertions aims to make new tests easy.
32 * A rule needs to be written only once and can be asserted against many graphs cheaply.
33 * The only part that needs to be produced by hand is the conversion from the list of packages to a resolved dependency graph.
34 * Automating this part would mean reimplementing the full resolution algorithm for the tests, this would be error prone.
35 * Manually defining declaratively the input and the output of arborist is what gives us confidence that the tests do what
36 * we want.
37 *
38 **/
39
40 const rule1 = {
41 description: 'Any package (except root package and workspace) should be able to require itself.',
42 apply: (t, dir, resolvedGraph, alreadyAsserted) => {
43 const graph = parseGraph(resolvedGraph)
44 const allPackages = getAllPackages(withRequireChain(graph))
45 // Workspaces are excluded: linked-strategy workspaces have no self-symlink, so self-referencing requires an `exports` field (matching pnpm).
46 allPackages.filter(p => p.chain.length !== 0 && !p.workspace).forEach(p => {
47 const resolveChain = [...p.chain, p.name]
48 const key = p.initialDir + ' => ' + resolveChain.join(' => ')
49 if (alreadyAsserted.has(key)) {
50 return
51 }
52 alreadyAsserted.add(key)
53 t.ok(setupRequire(path.join(dir, p.initialDir))(...resolveChain),
54 `Rule 1: Package "${[p.initialDir.replace('packages/', ''), ...p.chain].join(' => ')}" should have access to itself using its own name.`)
55 })
56 },
57 }
58
59 const rule2 = {
60 description: 'Packages can require their resolved dependencies.',
61 apply: (t, dir, resolvedGraph, alreadyAsserted) => {
62 const graph = parseGraph(resolvedGraph)
63 const allPackages = getAllPackages(withRequireChain(graph))
64 allPackages.forEach(p => {
65 (p.dependencies || []).filter(d => !isLoopToken(d)).map(d => d.name).forEach(n => {
66 const resolveChain = [...p.chain, n]
67 const key = p.initialDir + ' => ' + resolveChain.join(' => ')
68 if (alreadyAsserted.has(key)) {
69 return
70 }
71 alreadyAsserted.add(key)
72 t.ok(path.join(dir, p.initialDir),
73 `Rule 2: ${p.chain.length === 0 && p.initialDir === '.' ? 'The root' : `Package "${[p.initialDir.replace('packages/', ''), ...p.chain].join(' => ')}"`} should have access to "${n}" because it has it as a resolved dependency.`)
74 })
75 })
76 // testing circular deps
77 allPackages.forEach(p => {
78 (p.dependencies || []).filter(d => isLoopToken(d)).forEach(token => {
79 const back = parseLoopToken(token)
80 const n = p.chain.slice(-1 - back)[0] // getting the name of the circular dep by going back in the chain
81 const resolveChain = [...p.chain, n]
82 const key = p.initialDir + ' => ' + resolveChain.join(' => ')
83 if (alreadyAsserted.has(key)) {
84 return
85 }
86 alreadyAsserted.add(key)
87 t.ok(setupRequire(path.join(dir, p.initialDir))(...resolveChain),
88 `Rule 2: ${p.chain.length === 0 && p.initialDir === '.' ? 'The root' : `Package "${[p.initialDir.replace('packages/', ''), ...p.chain].join(' => ')}"`} should have access to "${n}" because it has it as a resolved dependency.`)
89 })
90 })
91 },
92 }
93
94 const rule3 = {
95 description: 'Any package can require a package installed at the root.',
96 apply: (t, dir, resolvedGraph, alreadyAsserted) => {
97 const graph = parseGraph(resolvedGraph)
98 const rootDependencies = graph.root.dependencies.map(o => o.name)
99 const allPackages = getAllPackages(withRequireChain(graph))
100 allPackages.forEach(p => {
101 rootDependencies.forEach(d => {
102 const resolveChain = [...p.chain, d]
103 const key = p.initialDir + ' => ' + resolveChain.join(' => ')
104 if (alreadyAsserted.has(key)) {
105 return
106 }
107 alreadyAsserted.add(key)
108 t.ok(setupRequire(path.join(dir, p.initialDir))(...resolveChain),
109 `Rule 3: ${p.chain.length === 0 && p.initialDir === '.' ? 'The root' : `Package "${[p.initialDir.replace('packages/', ''), ...p.chain].join(' => ')}"`} should have access to "${d}" because it is a root dependency.`)
110 })
111 })
112 },
113 }
114
115 const rule4 = {
116 description: 'Packages cannot require packages that are not in their dependencies, not root dependencies or not themselves.',
117 apply: (t, dir, resolvedGraph, alreadyAsserted) => {
118 const graph = parseGraph(resolvedGraph)
119 const allPackages = getAllPackages(withRequireChain(graph))
120 const allPackageNames = allPackages.filter(p => p.chain.length !== 0 || p.initialDir !== '.').map(o => o.name)
121 const rootDependencyNames = graph.root.dependencies.map(o => o.name)
122 allPackages.forEach(p => {
123 const resolvedDependencyNames = (p.dependencies || [])
124 .filter(d => !isLoopToken(d))
125 .map(d => d.name)
126 .concat((p.dependencies || [])
127 .filter(d => isLoopToken(d))
128 .map(t => {
129 const back = parseLoopToken(t)
130 return p.chain.slice(-1 - back)[0] // getting the name of the circular dep by going back in the chain
131 }))
132 allPackageNames.filter(n => !rootDependencyNames.includes(n))
133 .filter(n => !resolvedDependencyNames.includes(n))
134 .filter(n => n !== p.name)
135 .forEach(n => {
136 const resolveChain = [...p.chain, n]
137 const key = p.initialDir + ' => ' + resolveChain.join(' => ')
138 if (alreadyAsserted.has(key)) {
139 return
140 }
141 alreadyAsserted.add(key)
142 t.notOk(setupRequire(path.join(dir, p.initialDir))(...resolveChain),
143 `Rule 4: ${p.chain.length === 0 && p.initialDir === '.' ? 'The root' : `Package "${[p.initialDir.replace('packages/', ''), ...p.chain].join(' => ')}"`} should not have access to "${n}" because it not a root dependency, not in its resolved dependencies and not itself.`)
144 })
145 })
146 },
147 }
148
149 const rule5 = {
150 description: 'Peer dependencies should be resolved to same instance as parents',
151 apply: (t, dir, resolvedGraph) => {
152 const graph = parseGraph(resolvedGraph)
153 const allPackages = getAllPackages(withRequireChain(graph))
154 allPackages.filter(p => p.peer)
155 .forEach(p => {
156 const chain = p.chain
157 const parentChain = chain.slice(0, -2).concat([p.name])
158 t.same(setupRequire(path.join(dir, p.initialDir))(...parentChain), setupRequire(path.join(dir, p.initialDir))(...chain),
159 `Rule 5: Package "${[p.initialDir.replace('packages/', ''), ...chain.slice(0, -1)].join(' => ')}" should get the same instance of "${p.name}" as its parent`)
160 })
161 },
162 }
163
164 const rule6 = {
165 description: 'Packages with the same name, same version, and same peer deps are installed at the same place on disk',
166 apply: (t, dir, resolvedGraph) => {
167 const graph = parseGraph(resolvedGraph)
168 const allPackages = getAllPackages(withRequireChain(graph))
169 const byNameAndVersion = new Map()
170 allPackages.forEach(p => {
171 const peerDeps = p.dependencies.filter(d => d.peer).map(d => `${d.name}@${d.version}`).sort().join(' - ')
172 const key = `${p.name}@${p.version} - ${peerDeps}`
173 if (!byNameAndVersion.has(key)) {
174 byNameAndVersion.set(key, [])
175 }
176 byNameAndVersion.get(key).push(setupRequire(path.join(dir, p.initialDir))(...p.chain))
177 })
178 byNameAndVersion.forEach((value, key) => {
179 if (value.length === 1) {
180 return
181 }
182 const same = value.every(l => l === value[0])
183 t.ok(same, `Rule 6: Even though it is referenced multiple times, package "${key}" should be installed only once`)
184 })
185 },
186 }
187
188 const rule7 = {
189 description: 'The version of the resolved dependencies is the one we expect',
190 apply: (t, dir, resolvedGraph) => {
191 const graph = parseGraph(resolvedGraph)
192 const allPackages = getAllPackages(withRequireChain(graph))
193 allPackages.forEach(p => {
194 const ppath = setupRequire(path.join(dir, p.initialDir))(...p.chain)
195 p.dependencies.filter(d => !isLoopToken(d)).forEach(d => {
196 const dname = d.name
197 const dversion = JSON.parse(fs.readFileSync(`${resolvePackage(dname, ppath)}/package.json`).toString()).version
198
199 t.ok(dversion === d.version, `Rule 7: The version of ${dname} (${dversion}) provided to ${p.chain.length === 0 && p.initialDir === '.' ? 'the root' : `package "${[p.initialDir.replace('packages/', ''), ...p.chain].join(' => ')}"`} should be "${d.version}"`)
200 })
201 p.dependencies.filter(d => isLoopToken(d)).forEach(token => {
202 const back = parseLoopToken(token)
203 const name = p.chain.slice(-1 - back)[0] // getting the name of the circular dep by going back in the chain
204 const loopStartChain = p.chain.slice(0, -back)
205 const loopEndChain = [...p.chain, name]
206 t.same(setupRequire(path.join(dir, p.initialDir))(...loopStartChain),
207 setupRequire(path.join(dir, p.initialDir))(...loopEndChain),
208 `The two ends of this dependency loop should resolve to the same location: "${[p.initialDir.replace('packages/', ''), ...loopEndChain].join(' => ')}"`)
209 })
210 })
211 },
212 }
213
214 tap.test('most simple happy scenario', async t => {
215 /*
216 *
217 * Dependency graph:
218 *
219 * foo -> which -> isexe
220 *
221 */
222
223 // Input of arborist
224 const graph = {
225 registry: [
226 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
227 { name: 'isexe', version: '1.0.0' },
228 ],
229 root: {
230 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0' },
231 },
232 }
233
234 // expected output
235 const resolved = {
236 '[email protected] (root)': {
237 '[email protected]': {
238 '[email protected]': {},
239 },
240 },
241 }
242
243 const { dir, registry } = await getRepo(graph)
244
245 // Note that we override this cache to prevent interference from other tests
246 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
247 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
248 await arborist.reify({ installStrategy: 'linked' })
249
250 const asserted = new Set()
251 rule1.apply(t, dir, resolved, asserted)
252 rule2.apply(t, dir, resolved, asserted)
253 rule3.apply(t, dir, resolved, asserted)
254 rule4.apply(t, dir, resolved, asserted)
255 rule5.apply(t, dir, resolved, asserted)
256 rule6.apply(t, dir, resolved, asserted)
257 rule7.apply(t, dir, resolved, asserted)
258 })
259
260 tap.test('simple peer dependencies scenarios', async t => {
261 /*
262 * Dependencies:
263 *
264 * foo -> tsutils
265 * tsutils -> typescript (peer dep)
266 * typescript -> baz
267 * foo -> typescript
268 * typescript -> baz
269 *
270 */
271
272 const graph = {
273 registry: [
274 { name: 'tsutils', version: '1.0.0', dependencies: {}, peerDependencies: { typescript: '*' } },
275 { name: 'typescript', version: '1.0.0', dependencies: { baz: '*' } },
276 { name: 'baz', version: '2.0.0' },
277 ],
278 root: {
279 name: 'foo', version: '1.2.3', dependencies: { tsutils: '1.0.0', typescript: '1.0.0' },
280 },
281 }
282
283 const resolved = {
284 '[email protected] (root)': {
285 '[email protected]': {
286 '[email protected] (peer)': {
287 '[email protected]': {},
288 },
289 },
290 '[email protected]': {
291 '[email protected]': {},
292 },
293 },
294 }
295
296 const { dir, registry } = await getRepo(graph)
297
298 // Note that we override this cache to prevent interference from other tests
299 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
300 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
301 await arborist.reify({ installStrategy: 'linked' })
302
303 const asserted = new Set()
304 rule1.apply(t, dir, resolved, asserted)
305 rule2.apply(t, dir, resolved, asserted)
306 rule3.apply(t, dir, resolved, asserted)
307 rule4.apply(t, dir, resolved, asserted)
308 rule5.apply(t, dir, resolved, asserted)
309 rule6.apply(t, dir, resolved, asserted)
310 rule7.apply(t, dir, resolved, asserted)
311 })
312
313 tap.test('peer dependencies with legacyPeerDeps', async t => {
314 /*
315 * With legacyPeerDeps, peer dep edges are not created on the node.
316 * The linked strategy should still place peer deps alongside the
317 * package in the store so require() works from the real path.
318 *
319 * root -> phpegjs
320 * phpegjs -> pegjs (peer dep, no edge with legacyPeerDeps)
321 * root -> pegjs
322 */
323
324 const graph = {
325 registry: [
326 { name: 'phpegjs', version: '1.0.0', peerDependencies: { pegjs: '*', missing: '*' } },
327 { name: 'pegjs', version: '2.0.0' },
328 {
329 name: 'adapter',
330 version: '1.0.0',
331 dependencies: { pegjs: '*' },
332 peerDependencies: { pegjs: '*' },
333 },
334 ],
335 root: {
336 name: 'foo',
337 version: '1.2.3',
338 dependencies: { phpegjs: '1.0.0', pegjs: '2.0.0', adapter: '1.0.0' },
339 },
340 }
341
342 const resolved = {
343 '[email protected] (root)': {
344 '[email protected]': {
345 '[email protected] (peer)': {},
346 },
347 '[email protected]': {},
348 '[email protected]': {
349 '[email protected]': {},
350 },
351 },
352 }
353
354 const { dir, registry } = await getRepo(graph)
355
356 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
357 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache, legacyPeerDeps: true })
358 await arborist.reify({ installStrategy: 'linked' })
359
360 // phpegjs should be able to require its peer dep pegjs
361 t.ok(setupRequire(dir)('phpegjs', 'pegjs'),
362 'phpegjs can require peer dep pegjs with legacyPeerDeps')
363
364 const asserted = new Set()
365 rule1.apply(t, dir, resolved, asserted)
366 rule2.apply(t, dir, resolved, asserted)
367 rule3.apply(t, dir, resolved, asserted)
368 rule5.apply(t, dir, resolved, asserted)
369 rule7.apply(t, dir, resolved, asserted)
370 })
371
372 tap.test('idempotent install with legacyPeerDeps and workspace peer deps', async t => {
373 // Regression: when legacyPeerDeps is enabled and a workspace has a peer dependency on another workspace, node.resolve() returns the Link node (not its target). This caused workspaceProxy to be called with the Link, producing store links under node_modules/<ws>/node_modules/ that race with the workspace symlink at node_modules/<ws>, hitting EEXIST on the second install.
374 // Use many workspaces with cross-peer-deps to increase concurrency and make the race window large enough to trigger reliably.
375 const workspaces = []
376 for (let i = 0; i < 20; i++) {
377 workspaces.push({
378 name: `ws-${i}`,
379 version: '1.0.0',
380 dependencies: { abbrev: '1.0.0' },
381 peerDependencies: { [`ws-${(i + 1) % 20}`]: '*' },
382 })
383 }
384
385 const graph = {
386 registry: [
387 { name: 'abbrev', version: '1.0.0' },
388 ],
389 root: {
390 name: 'myroot', version: '1.0.0',
391 },
392 workspaces,
393 }
394
395 const { dir, registry } = await getRepo(graph)
396
397 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
398 const opts = { path: dir, registry, packumentCache: new Map(), cache, legacyPeerDeps: true }
399
400 // First install
401 const arb1 = new Arborist(opts)
402 await arb1.reify({ installStrategy: 'linked' })
403
404 // Second install must not throw EEXIST
405 const arb2 = new Arborist({ ...opts, packumentCache: new Map() })
406 await arb2.reify({ installStrategy: 'linked' })
407
408 // Workspace peer dep symlinks should still be present after second install
409 for (let i = 0; i < 20; i++) {
410 const peerTarget = `ws-${(i + 1) % 20}`
411 const peerLink = path.join(dir, 'packages', `ws-${i}`, 'node_modules', peerTarget)
412 t.ok(fs.lstatSync(peerLink).isSymbolicLink(),
413 `ws-${i} peer dep on ${peerTarget} is still a symlink after second install`)
414 }
415 })
416
417 tap.test('Lock file is same in hoisted and in isolated mode', async t => {
418 const graph = {
419 registry: [
420 { name: 'which', version: '2.0.2' },
421 ],
422 root: { name: 'foo', version: '1.2.3', dependencies: { which: '2.0.2' } },
423 }
424
425 const { dir: hoistedModeDir, registry } = await getRepo(graph)
426 const { dir: isolatedModeDir } = await getRepo(graph)
427
428 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
429 const arboristHoisted = new Arborist({ path: hoistedModeDir, registry, packumentCache: new Map(), cache })
430 const arboristIsolated = new Arborist({ path: isolatedModeDir, registry, packumentCache: new Map(), cache })
431
432 await Promise.all([
433 arboristHoisted.reify({ strategy: 'hoisted' }),
434 arboristIsolated.reify({ installStrategy: 'linked' }),
435 ])
436
437 const [hoistedModeLockFile, isolatedModeLockFile] = await Promise.all([
438 fs.promises.readFile(path.join(hoistedModeDir, 'package-lock.json'), { encoding: 'utf8' }),
439 fs.promises.readFile(path.join(isolatedModeDir, 'package-lock.json'), { encoding: 'utf8' }),
440 ])
441
442 t.same(hoistedModeLockFile, isolatedModeLockFile, 'hoisted mode and isolated mode produce the same lockfile')
443 })
444
445 tap.test('Basic workspaces setup', async t => {
446 const graph = {
447 registry: [
448 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
449 { name: 'which', version: '2.0.0', dependencies: { isexe: '^1.0.0' } },
450 { name: 'isexe', version: '1.0.0' },
451 ],
452 root: {
453 name: 'dog', version: '1.2.3', dependencies: { bar: '*' },
454 },
455 workspaces: [
456 { name: 'bar', version: '1.0.0', dependencies: { which: '2.0.0' } },
457 { name: 'baz', version: '1.0.0', dependencies: { which: '2.0.0', bar: '*' } },
458 { name: 'cat', version: '1.0.0', dependencies: { which: '1.0.0' } },
459 { name: 'fish', version: '1.0.0', dependencies: { which: '1.0.0', cat: '*' } },
460 { name: 'catfish', version: '1.0.0' },
461 ],
462 }
463
464 const resolved = {
465 '[email protected] (root)': {
466 '[email protected] (workspace)': {
467 '[email protected]': {
468 '[email protected]': {},
469 },
470 },
471 },
472 '[email protected] (workspace)': {
473 '[email protected]': {
474 '[email protected]': {},
475 },
476 },
477 '[email protected] (workspace)': {
478 '[email protected] (workspace)': {
479 '[email protected]': {
480 '[email protected]': {},
481 },
482 },
483 '[email protected]': {
484 '[email protected]': {},
485 },
486 },
487 '[email protected] (workspace)': {
488 '[email protected]': {
489 '[email protected]': {},
490 },
491 },
492 '[email protected] (workspace)': {
493 '[email protected] (workspace)': {
494 '[email protected]': {
495 '[email protected]': {},
496 },
497 },
498 '[email protected]': {
499 '[email protected]': {},
500 },
501 },
502 }
503
504 const { dir, registry } = await getRepo(graph)
505
506 // Note that we override this cache to prevent interference from other tests
507 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
508 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
509 await arborist.reify({ installStrategy: 'linked' })
510
511 const asserted = new Set()
512 rule1.apply(t, dir, resolved, asserted)
513 rule2.apply(t, dir, resolved, asserted)
514 rule3.apply(t, dir, resolved, asserted)
515 rule4.apply(t, dir, resolved, asserted)
516 rule5.apply(t, dir, resolved, asserted)
517 rule6.apply(t, dir, resolved, asserted)
518 rule7.apply(t, dir, resolved, asserted)
519 })
520
521 tap.test('resolved versions are the same on isolated and in hoisted mode', async t => {
522 const graph = {
523 registry: [
524 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
525 { name: 'which', version: '2.0.0', dependencies: { isexe: '^1.0.0' } },
526 { name: 'isexe', version: '1.0.0' },
527 ],
528 root: {
529 name: 'dog', version: '1.2.3', dependencies: { bar: '*' },
530 },
531 workspaces: [
532 { name: 'bar', version: '1.0.0', dependencies: { which: '2.0.0' } },
533 { name: 'baz', version: '1.0.0', dependencies: { which: '2.0.0', bar: '*' } },
534 { name: 'cat', version: '1.0.0', dependencies: { which: '1.0.0' } },
535 { name: 'fish', version: '1.0.0', dependencies: { which: '1.0.0', cat: '*' } },
536 { name: 'catfish', version: '1.0.0' },
537 ],
538 }
539
540 const resolved = {
541 '[email protected] (root)': {
542 '[email protected] (workspace)': {
543 '[email protected]': {
544 '[email protected]': {},
545 },
546 },
547 },
548 '[email protected] (workspace)': {
549 '[email protected]': {
550 '[email protected]': {},
551 },
552 },
553 '[email protected] (workspace)': {
554 '[email protected] (workspace)': {
555 '[email protected]': {
556 '[email protected]': {},
557 },
558 },
559 '[email protected]': {
560 '[email protected]': {},
561 },
562 },
563 '[email protected] (workspace)': {
564 '[email protected]': {
565 '[email protected]': {},
566 },
567 },
568 '[email protected] (workspace)': {
569 '[email protected] (workspace)': {
570 '[email protected]': {
571 '[email protected]': {},
572 },
573 },
574 '[email protected]': {
575 '[email protected]': {},
576 },
577 },
578 '[email protected]': {},
579 }
580
581 let { dir, registry } = await getRepo(graph)
582
583 // Note that we override this cache to prevent interference from other tests
584 let cache = fs.mkdtempSync(`${getTempDir()}/test-`)
585 let arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
586 await arborist.reify({ installStrategy: 'linked' })
587
588 rule7.apply(t, dir, resolved, new Set())
589
590 const mock = await getRepo(graph)
591 dir = mock.dir
592 registry = mock.registry
593
594 // Note that we override this cache to prevent interference from other tests
595 cache = fs.mkdtempSync(`${getTempDir()}/test-`)
596 arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
597 await arborist.reify({ strategy: 'hoisted' })
598
599 // checking that the resolved graph is the same in hoisting and in isolated mode
600 rule7.apply(t, dir, resolved, new Set())
601 })
602
603 tap.test('peer dependency chain', async t => {
604 // Input of arborist
605 const graph = {
606 registry: [
607 { name: 'bar', version: '1.0.0', dependencies: { baz: '^1.0.0' }, peerDependencies: { boat: '*' } },
608 { name: 'baz', version: '1.0.0', peerDependencies: { boat: '*' } },
609 { name: 'boat', version: '3.0.0' },
610 ],
611 root: {
612 name: 'foo', version: '1.2.3', dependencies: { bar: '1.0.0', boat: '^3.0.0' },
613 },
614 }
615
616 // expected output
617 const resolved = {
618 '[email protected] (root)': {
619 '[email protected]': {
620 '[email protected]': {
621 '[email protected] (peer)': {},
622 },
623 '[email protected] (peer)': {},
624 },
625 '[email protected]': {},
626 },
627 }
628
629 const { dir, registry } = await getRepo(graph)
630
631 // Note that we override this cache to prevent interference from other tests
632 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
633 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
634 await arborist.reify({ installStrategy: 'linked' })
635
636 const asserted = new Set()
637 rule1.apply(t, dir, resolved, asserted)
638 rule2.apply(t, dir, resolved, asserted)
639 rule3.apply(t, dir, resolved, asserted)
640 rule4.apply(t, dir, resolved, asserted)
641 rule5.apply(t, dir, resolved, asserted)
642 rule6.apply(t, dir, resolved, asserted)
643 rule7.apply(t, dir, resolved, asserted)
644 })
645
646 tap.test('failing optional deps are not installed', async t => {
647 // Input of arborist
648 const graph = {
649 registry: [
650 { name: 'which', version: '1.0.0', os: ['npmOS'] },
651 ],
652 root: {
653 name: 'foo', version: '1.2.3', optionalDependencies: { which: '1.0.0' },
654 },
655 }
656
657 const { dir, registry } = await getRepo(graph)
658
659 // Note that we override this cache to prevent interference from other tests
660 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
661 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
662 await arborist.reify({ installStrategy: 'linked' })
663
664 t.notOk(setupRequire(dir)('which'), 'Failing optional deps should not be installed')
665
666 t.notOk(fs.existsSync(path.join(dir, 'node_modules', '.bin', 'which')))
667 })
668
669 tap.test('Optional deps are installed when possible', async t => {
670 // Input of arborist
671 const graph = {
672 registry: [
673 { name: 'which', version: '1.0.0' },
674 ],
675 root: {
676 name: 'foo', version: '1.2.3', optionalDependencies: { which: '1.0.0' },
677 },
678 }
679
680 const { dir, registry } = await getRepo(graph)
681
682 // Note that we override this cache to prevent interference from other tests
683 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
684 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
685 await arborist.reify({ installStrategy: 'linked' })
686
687 t.ok(setupRequire(dir)('which'), 'Optional deps should be installed when possible')
688
689 // TODO: make sure that existsSync is not deprecated
690 t.ok(fs.existsSync(path.join(dir, 'node_modules', '.bin', 'which')))
691 })
692
693 tap.test('shrinkwrap', async t => {
694 const shrinkwrap = JSON.stringify({
695 name: 'which',
696 version: '1.0.0',
697 lockfileVersion: 2,
698 requires: true,
699 packages: {
700 '': {
701 name: 'which',
702 version: '1.0.0',
703 dependencies: {
704 isexe: '^1.0.0',
705 },
706 },
707 'node_modules/isexe': {
708 version: '1.0.0',
709 resolved: '##REG##/isexe/1.0.0.tar',
710 },
711 },
712 dependencies: {
713 isexe: {
714 version: '1.0.0',
715 resolved: '##REG##/isexe/1.0.0.tar',
716 },
717 },
718 })
719
720 // Input of arborist
721 const graph = {
722 registry: [
723 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' }, shrinkwrap, _hasShrinkwrap: true },
724 { name: 'isexe', version: '1.1.0' },
725 { name: 'isexe', version: '1.0.0' },
726 ],
727 root: {
728 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0', isexe: '^1.0.0' },
729 },
730 }
731
732 // expected output
733 const resolved = {
734 '[email protected] (root)': {
735 '[email protected]': {
736 '[email protected]': {},
737 },
738 '[email protected]': {},
739 },
740 }
741
742 const { dir, registry } = await getRepo(graph)
743
744 // Note that we override this cache to prevent interference from other tests
745 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
746 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
747 await arborist.reify({ installStrategy: 'linked' })
748
749 const asserted = new Set()
750 rule1.apply(t, dir, resolved, asserted)
751 rule2.apply(t, dir, resolved, asserted)
752 rule3.apply(t, dir, resolved, asserted)
753 rule4.apply(t, dir, resolved, asserted)
754 rule5.apply(t, dir, resolved, asserted)
755 rule6.apply(t, dir, resolved, asserted)
756 rule7.apply(t, dir, resolved, asserted)
757 })
758
759 tap.test('shrinkwrap install dev deps (like hoisting does)', async t => {
760 const shrinkwrap = JSON.stringify({
761 name: 'which',
762 version: '1.0.0',
763 lockfileVersion: 2,
764 requires: true,
765 packages: {
766 '': {
767 name: 'which',
768 version: '1.0.0',
769 devDependencies: {
770 isexe: '^1.0.0',
771 },
772 },
773 'node_modules/isexe': {
774 version: '1.0.0',
775 dev: true,
776 resolved: '##REG##/isexe/1.0.0.tar',
777 },
778 },
779 dependencies: {
780 isexe: {
781 version: '1.0.0',
782 dev: true,
783 resolved: '##REG##/isexe/1.0.0.tar',
784 },
785 },
786 })
787
788 // Input of arborist
789 const graph = {
790 registry: [
791 { name: 'which', version: '1.0.0', devDependencies: { isexe: '^1.0.0' }, shrinkwrap, _hasShrinkwrap: true },
792 { name: 'isexe', version: '1.1.0' },
793 { name: 'isexe', version: '1.0.0' },
794 ],
795 root: {
796 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0', isexe: '^1.0.0' },
797 },
798 }
799
800 // expected output
801 const resolved = {
802 '[email protected] (root)': {
803 '[email protected]': {
804 '[email protected]': {},
805 },
806 '[email protected]': {},
807 },
808 }
809
810 const { dir, registry } = await getRepo(graph)
811
812 // Note that we override this cache to prevent interference from other tests
813 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
814 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
815 await arborist.reify({ installStrategy: 'linked' })
816
817 const asserted = new Set()
818 rule1.apply(t, dir, resolved, asserted)
819 rule2.apply(t, dir, resolved, asserted)
820 rule3.apply(t, dir, resolved, asserted)
821 rule4.apply(t, dir, resolved, asserted)
822 rule5.apply(t, dir, resolved, asserted)
823 rule6.apply(t, dir, resolved, asserted)
824 rule7.apply(t, dir, resolved, asserted)
825 })
826
827 tap.test('shrinkwrap with peer dependencies', async t => {
828 const shrinkwrap = JSON.stringify({
829 name: 'which',
830 version: '1.0.0',
831 lockfileVersion: 2,
832 requires: true,
833 packages: {
834 '': {
835 name: 'which',
836 version: '1.0.0',
837 dependencies: {
838 isexe: '^1.0.0',
839 },
840 devDependencies: {
841 bar: '1.0.0',
842 },
843 peerDependencies: {
844 bar: '*',
845 },
846 },
847 'node_modules/bar': {
848 version: '1.0.0',
849 resolved: '##REG##/bar/1.0.0.tar',
850 dev: true,
851 bin: {
852 bar: 'bin.js',
853 },
854 },
855 'node_modules/isexe': {
856 version: '1.0.0',
857 resolved: '##REG##/isexe/1.0.0.tar',
858 bin: {
859 isexe: 'bin.js',
860 },
861 },
862 },
863 dependencies: {
864 bar: {
865 version: '1.0.0',
866 resolved: '##REG##/bar/1.0.0.tar',
867 dev: true,
868 },
869 isexe: {
870 version: '1.0.0',
871 resolved: '##REG##/isexe/1.0.0.tar',
872 },
873 },
874 })
875 // expected output
876 const resolved = {
877 '[email protected] (root)': {
878 '[email protected]': {
879 '[email protected]': {},
880 '[email protected]': {},
881 },
882 '[email protected]': {},
883 '[email protected]': {},
884 },
885 }
886
887 // Input of arborist
888 const graph = {
889 registry: [
890 {
891 name: 'which',
892 version: '1.0.0',
893 dependencies: { isexe: '^1.0.0' },
894 peerDependencies: { bar: '*' },
895 devDependencies: { bar: '1.0.0' },
896 shrinkwrap,
897 _hasShrinkwrap: true,
898 },
899 { name: 'isexe', version: '1.1.0' },
900 { name: 'isexe', version: '1.0.0' },
901 { name: 'bar', version: '1.1.0' },
902 { name: 'bar', version: '1.0.0' },
903 ],
904 root: {
905 name: 'foo',
906 version: '1.0.0',
907 dependencies: { isexe: '^1.0.0', bar: '^1.0.0', which: '1.0.0' },
908 },
909 }
910
911 const { dir, registry } = await getRepo(graph)
912 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
913 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
914 await arborist.reify({ installStrategy: 'linked' })
915
916 // TODO: create the resolved object
917 const asserted = new Set()
918 rule1.apply(t, dir, resolved, asserted)
919 rule2.apply(t, dir, resolved, asserted)
920 rule3.apply(t, dir, resolved, asserted)
921 rule4.apply(t, dir, resolved, asserted)
922 rule5.apply(t, dir, resolved, asserted)
923 rule6.apply(t, dir, resolved, asserted)
924 rule7.apply(t, dir, resolved, asserted)
925 })
926
927 tap.test('bundled dependencies of external packages', async t => {
928 // Input of arborist
929 const graph = {
930 registry: [
931 { name: 'which',
932 version: '1.0.0',
933 dependencies: { isexe: '^1.0.0' },
934 bundleDependencies: ['isexe'],
935 bundledDeps: [{ name: 'isexe', version: '1.0.0' }],
936 },
937 { name: 'isexe', version: '1.1.0' },
938 ],
939 root: {
940 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0' },
941 },
942 }
943
944 // expected output
945 const resolved = {
946 '[email protected] (root)': {
947 '[email protected]': {
948 '[email protected]': {},
949 },
950 },
951 }
952
953 const { dir, registry } = await getRepo(graph)
954
955 // Note that we override this cache to prevent interference from other tests
956 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
957 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
958 await arborist.reify({ installStrategy: 'linked' })
959
960 const asserted = new Set()
961 rule1.apply(t, dir, resolved, asserted)
962 rule2.apply(t, dir, resolved, asserted)
963 rule3.apply(t, dir, resolved, asserted)
964 rule4.apply(t, dir, resolved, asserted)
965 rule5.apply(t, dir, resolved, asserted)
966 rule6.apply(t, dir, resolved, asserted)
967 rule7.apply(t, dir, resolved, asserted)
968 })
969
970 tap.test('bundled dependencies of internal packages', async t => {
971 // Input of arborist
972 const graph = {
973 registry: [
974 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' },
975 },
976 { name: 'isexe', version: '1.1.0' },
977 ],
978 root: {
979 name: 'foo',
980 version: '1.2.3',
981 dependencies: { which: '1.0.0', isexe: '^1.0.0' },
982 bundleDependencies: ['isexe'],
983 },
984 }
985
986 // expected output
987 const resolved = {
988 '[email protected] (root)': {
989 '[email protected]': {
990 '[email protected]': {},
991 },
992 '[email protected]': {},
993 },
994 }
995
996 const { dir, registry } = await getRepo(graph)
997
998 // Note that we override this cache to prevent interference from other tests
999 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1000 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1001 await arborist.reify({ installStrategy: 'linked' })
1002
1003 const asserted = new Set()
1004 rule1.apply(t, dir, resolved, asserted)
1005 rule2.apply(t, dir, resolved, asserted)
1006 rule3.apply(t, dir, resolved, asserted)
1007 rule4.apply(t, dir, resolved, asserted)
1008 rule5.apply(t, dir, resolved, asserted)
1009 // I think that duplicated versions are okay in the case of bundled deps
1010 // rule6.apply(t, dir, resolved, asserted)
1011 rule7.apply(t, dir, resolved, asserted)
1012
1013 const isexePath = path.join(dir, 'node_modules', 'isexe')
1014 t.equal(isexePath, fs.realpathSync(isexePath))
1015 })
1016
1017 tap.test('nested bundled dependencies of internal packages', async t => {
1018 // Input of arborist
1019 const graph = {
1020 registry: [
1021 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' },
1022 },
1023 { name: 'isexe', version: '1.1.0', dependencies: { bar: '*' } },
1024 { name: 'bar', version: '3.0.0' },
1025 ],
1026 root: {
1027 name: 'foo',
1028 version: '1.2.3',
1029 dependencies: { which: '1.0.0', isexe: '^1.0.0' },
1030 bundleDependencies: ['isexe'],
1031 },
1032 }
1033
1034 // expected output
1035 const resolved = {
1036 '[email protected] (root)': {
1037 '[email protected]': {
1038 '[email protected]': {
1039 '[email protected]': {},
1040 },
1041 },
1042 '[email protected]': {},
1043 '[email protected]': {}, // The bundled bar is hoisted
1044 },
1045 }
1046
1047 const { dir, registry } = await getRepo(graph)
1048
1049 // Note that we override this cache to prevent interference from other tests
1050 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1051
1052 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1053 await arborist.reify({ installStrategy: 'linked' })
1054
1055 const asserted = new Set()
1056 rule1.apply(t, dir, resolved, asserted)
1057 rule2.apply(t, dir, resolved, asserted)
1058 rule3.apply(t, dir, resolved, asserted)
1059 rule4.apply(t, dir, resolved, asserted)
1060 rule5.apply(t, dir, resolved, asserted)
1061 // I think that duplicated versions are okay in the case of bundled deps
1062 // rule6.apply(t, dir, resolved, asserted)
1063 rule7.apply(t, dir, resolved, asserted)
1064
1065 const isexePath = path.join(dir, 'node_modules', 'isexe')
1066 t.equal(isexePath, fs.realpathSync(isexePath))
1067 })
1068
1069 tap.test('nested bundled dependencies of workspaces', async t => {
1070 const graph = {
1071 registry: [
1072 { name: 'which', version: '2.0.0', dependencies: { isexe: '^1.0.0' } },
1073 { name: 'isexe', version: '1.0.0' },
1074 ],
1075 root: {
1076 name: 'dog', version: '1.2.3',
1077 },
1078 workspaces: [
1079 { name: 'bar', version: '1.0.0', dependencies: { which: '2.0.0' }, bundleDependencies: ['which'] },
1080 ],
1081 }
1082
1083 const resolved = {
1084 '[email protected] (root)': {
1085 '[email protected]': {},
1086 '[email protected]': {},
1087 },
1088 '[email protected] (workspace)': {
1089 '[email protected]': {},
1090 '[email protected]': {},
1091 },
1092 }
1093
1094 const { dir, registry } = await getRepo(graph)
1095
1096 // Note that we override this cache to prevent interference from other tests
1097 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1098
1099 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1100 await arborist.reify({ installStrategy: 'linked' })
1101
1102 const asserted = new Set()
1103 rule1.apply(t, dir, resolved, asserted)
1104 rule2.apply(t, dir, resolved, asserted)
1105 rule3.apply(t, dir, resolved, asserted)
1106 // Workspace link at packages/bar/node_modules/bar and bundled deps (which, isexe) are co-located in the same node_modules/, making them mutually accessible regardless of declared dependencies.
1107 // rule4.apply(t, dir, resolved, asserted)
1108 rule5.apply(t, dir, resolved, asserted)
1109 // I think that duplicated versions are okay in the case of bundled deps
1110 // rule6.apply(t, dir, resolved, asserted)
1111 rule7.apply(t, dir, resolved, asserted)
1112
1113 // Bundled deps are hoisted to root node_modules as real directories
1114 const isexePath = path.join(dir, 'node_modules', 'isexe')
1115 t.equal(isexePath, fs.realpathSync(isexePath))
1116 const whichPath = path.join(dir, 'node_modules', 'which')
1117 t.equal(whichPath, fs.realpathSync(whichPath))
1118 })
1119
1120 tap.test('nested bundled dependencies of workspaces with conflicting isolated dep', async t => {
1121 const graph = {
1122 registry: [
1123 { name: 'which', version: '3.0.0', dependencies: { isexe: '^2.0.0' } },
1124 { name: 'isexe', version: '2.0.0' },
1125 { name: 'which', version: '2.0.0', dependencies: { isexe: '^1.0.0' } },
1126 { name: 'isexe', version: '1.0.0' },
1127 ],
1128 root: {
1129 name: 'dog', version: '1.2.3', dependencies: { which: '3.0.0' },
1130 },
1131 workspaces: [
1132 { name: 'bar', version: '1.0.0', dependencies: { which: '2.0.0' }, bundleDependencies: ['which'] },
1133 ],
1134 }
1135
1136 // the isexe that is bundled is hoisted
1137 // the 'which' that is bundled is not hoisted due to a conflict
1138 const resolved = {
1139 '[email protected] (root)': {
1140 '[email protected]': {
1141 '[email protected]': {},
1142 },
1143 },
1144 '[email protected] (workspace)': {
1145 '[email protected]': {
1146 '[email protected]': {},
1147 },
1148 '[email protected]': {},
1149 },
1150 }
1151
1152 const { dir, registry } = await getRepo(graph)
1153
1154 // Note that we override this cache to prevent interference from other tests
1155 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1156
1157 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1158 await arborist.reify({ installStrategy: 'linked' })
1159
1160 const asserted = new Set()
1161 rule1.apply(t, dir, resolved, asserted)
1162 rule2.apply(t, dir, resolved, asserted)
1163 rule3.apply(t, dir, resolved, asserted)
1164 // Workspace link at packages/bar/node_modules/bar and bundled deps (which, isexe) share the same node_modules/, making them mutually accessible regardless of declared dependencies.
1165 // rule4.apply(t, dir, resolved, asserted)
1166 rule5.apply(t, dir, resolved, asserted)
1167 // I think that duplicated versions are okay in the case of bundled deps
1168 // rule6.apply(t, dir, resolved, asserted)
1169 rule7.apply(t, dir, resolved, asserted)
1170
1171 const isexePath = path.join(dir, 'packages', 'bar', 'node_modules', 'isexe')
1172 t.equal(isexePath, fs.realpathSync(isexePath))
1173 const whichPath = path.join(dir, 'packages', 'bar', 'node_modules', 'which')
1174 t.equal(whichPath, fs.realpathSync(whichPath))
1175 })
1176
1177 tap.test('adding a dependency', async t => {
1178 // Input of arborist
1179 const graph = {
1180 registry: [
1181 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
1182 { name: 'isexe', version: '1.0.0' },
1183 { name: 'bar', version: '2.2.0' },
1184 ],
1185 root: {
1186 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0' },
1187 },
1188 }
1189
1190 // expected output
1191 const resolved = {
1192 '[email protected] (root)': {
1193 '[email protected]': {
1194 '[email protected]': {},
1195 },
1196 '[email protected]': {},
1197 },
1198 }
1199
1200 const { dir, registry } = await getRepo(graph)
1201
1202 // Note that we override this cache to prevent interference from other tests
1203 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1204 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1205 await arborist.reify({ installStrategy: 'linked' })
1206
1207 // Add a new dependency
1208 const cache2 = fs.mkdtempSync(`${getTempDir()}/test-`)
1209 const arborist2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache: cache2, add: ['bar@^2.0.0'] })
1210 await arborist2.reify({ installStrategy: 'linked' })
1211
1212 // Note that the 'resolved' dependency graph contains 'bar'
1213 const asserted = new Set()
1214 rule1.apply(t, dir, resolved, asserted)
1215 rule2.apply(t, dir, resolved, asserted)
1216 rule3.apply(t, dir, resolved, asserted)
1217 rule4.apply(t, dir, resolved, asserted)
1218 rule5.apply(t, dir, resolved, asserted)
1219 rule6.apply(t, dir, resolved, asserted)
1220 rule7.apply(t, dir, resolved, asserted)
1221 })
1222
1223 tap.test('removing a dependency', async t => {
1224 // Input of arborist
1225 const graph = {
1226 registry: [
1227 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
1228 { name: 'isexe', version: '1.0.0' },
1229 { name: 'bar', version: '2.2.0' },
1230 ],
1231 root: {
1232 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0', bar: '^2.0.0' },
1233 },
1234 }
1235
1236 const { dir, registry } = await getRepo(graph)
1237
1238 // Note that we override this cache to prevent interference from other tests
1239 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1240 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1241 await arborist.reify({ installStrategy: 'linked' })
1242
1243 // checking that bar is installed
1244 t.ok(setupRequire(dir)('bar'), 'bar should be installed initially')
1245
1246 // Add a new dependency
1247 const cache2 = fs.mkdtempSync(`${getTempDir()}/test-`)
1248 const arborist2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache: cache2 })
1249 await arborist2.buildIdealTree({ rm: ['bar'] })
1250 await arborist2.reify({ installStrategy: 'linked' })
1251
1252 t.notOk(setupRequire(dir)('bar'), 'bar should not be installed anymore')
1253 })
1254
1255 tap.test('circular dependencies', async t => {
1256 // Input of arborist
1257 const graph = {
1258 registry: [
1259 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
1260 { name: 'isexe', version: '1.0.0', dependencies: { which: '1.0.0' } },
1261 { name: 'bar', version: '1.2.6' },
1262 ],
1263 root: {
1264 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0', bar: '1.2.6' },
1265 },
1266 }
1267
1268 // expected output
1269 const resolved = {
1270 '[email protected] (root)': {
1271 '[email protected]': {
1272 '[email protected]': '(back 1)',
1273 },
1274 '[email protected]': {},
1275 },
1276 }
1277
1278 const { dir, registry } = await getRepo(graph)
1279
1280 // Note that we override this cache to prevent interference from other tests
1281 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1282 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1283 await arborist.reify({ installStrategy: 'linked' })
1284
1285 const asserted = new Set()
1286 rule1.apply(t, dir, resolved, asserted)
1287 rule2.apply(t, dir, resolved, asserted)
1288 rule3.apply(t, dir, resolved, asserted)
1289 rule4.apply(t, dir, resolved, asserted)
1290 rule5.apply(t, dir, resolved, asserted)
1291 rule6.apply(t, dir, resolved, asserted)
1292 rule7.apply(t, dir, resolved, asserted)
1293 })
1294
1295 tap.test('circular peer dependencies', async t => {
1296 // Input of arborist
1297 const graph = {
1298 registry: [
1299 { name: 'cat', version: '1.0.0', peerDependencies: { bar: '*' } },
1300 { name: 'bar', version: '1.0.0', peerDependencies: { cat: '*' } },
1301 ],
1302 root: {
1303 name: 'foo', version: '1.2.3', dependencies: { cat: '1.0.0', bar: '1.0.0' },
1304 },
1305 }
1306
1307 // expected output
1308 const resolved = {
1309 '[email protected] (root)': {
1310 '[email protected]': {
1311 '[email protected]': '(back 1)',
1312 },
1313 '[email protected]': {
1314 '[email protected]': '(back 1)',
1315 },
1316 },
1317 }
1318
1319 const { dir, registry } = await getRepo(graph)
1320
1321 // Note that we override this cache to prevent interference from other tests
1322 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1323 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1324 await arborist.reify({ installStrategy: 'linked' })
1325
1326 const asserted = new Set()
1327 rule1.apply(t, dir, resolved, asserted)
1328 rule2.apply(t, dir, resolved, asserted)
1329 rule3.apply(t, dir, resolved, asserted)
1330 rule4.apply(t, dir, resolved, asserted)
1331 rule5.apply(t, dir, resolved, asserted)
1332 rule6.apply(t, dir, resolved, asserted)
1333 rule7.apply(t, dir, resolved, asserted)
1334 })
1335
1336 tap.test('peer dependency on parent', async t => {
1337 // Input of arborist
1338 const graph = {
1339 registry: [
1340 { name: 'cat', version: '1.0.0', dependencies: { bar: '*' } },
1341 { name: 'bar', version: '1.0.0', peerDependencies: { cat: '*' } },
1342 ],
1343 root: {
1344 name: 'foo', version: '1.2.3', dependencies: { cat: '1.0.0' },
1345 },
1346 }
1347
1348 // expected output
1349 const resolved = {
1350 '[email protected] (root)': {
1351 '[email protected]': {
1352 '[email protected]': '(back 1)',
1353 },
1354 },
1355 }
1356
1357 const { dir, registry } = await getRepo(graph)
1358
1359 // Note that we override this cache to prevent interference from other tests
1360 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1361 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1362 await arborist.reify({ installStrategy: 'linked' })
1363
1364 const asserted = new Set()
1365 rule1.apply(t, dir, resolved, asserted)
1366 rule2.apply(t, dir, resolved, asserted)
1367 rule3.apply(t, dir, resolved, asserted)
1368 rule4.apply(t, dir, resolved, asserted)
1369 rule5.apply(t, dir, resolved, asserted)
1370 rule6.apply(t, dir, resolved, asserted)
1371 rule7.apply(t, dir, resolved, asserted)
1372 })
1373
1374 tap.test('scoped package', async t => {
1375 /*
1376 *
1377 * Dependency graph:
1378 *
1379 * foo -> which -> isexe
1380 *
1381 */
1382
1383 // Input of arborist
1384 const graph = {
1385 registry: [
1386 { name: 'which', version: '1.0.0', dependencies: { '@foo/isexe': '^1.0.0' } },
1387 { name: '@foo/isexe', version: '1.0.0' },
1388 ],
1389 root: {
1390 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0' },
1391 },
1392 }
1393
1394 // expected output
1395 const resolved = {
1396 '[email protected] (root)': {
1397 '[email protected]': {
1398 '@foo/[email protected]': {},
1399 },
1400 },
1401 }
1402
1403 const { dir, registry } = await getRepo(graph)
1404
1405 // Note that we override this cache to prevent interference from other tests
1406 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1407 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1408 await arborist.reify({ installStrategy: 'linked' })
1409
1410 const asserted = new Set()
1411 rule1.apply(t, dir, resolved, asserted)
1412 rule2.apply(t, dir, resolved, asserted)
1413 rule3.apply(t, dir, resolved, asserted)
1414 rule4.apply(t, dir, resolved, asserted)
1415 rule7.apply(t, dir, resolved, asserted)
1416 })
1417
1418 tap.test('scoped workspace packages', async t => {
1419 /*
1420 * Dependency graph:
1421 *
1422 * root -> @scope/package-b (workspace)
1423 * @scope/package-b -> @scope/package-a (workspace)
1424 * root -> @scope/package-a (workspace)
1425 */
1426
1427 const graph = {
1428 registry: [
1429 { name: 'which', version: '1.0.0' },
1430 ],
1431 root: {
1432 name: 'myproject', version: '1.0.0', dependencies: { '@scope/package-a': '*', '@scope/package-b': '*' },
1433 },
1434 workspaces: [
1435 { name: '@scope/package-a', version: '1.0.0', dependencies: { which: '1.0.0' } },
1436 { name: '@scope/package-b', version: '1.0.0', dependencies: { '@scope/package-a': '*' } },
1437 ],
1438 }
1439
1440 const resolved = {
1441 '[email protected] (root)': {
1442 '@scope/[email protected] (workspace)': {
1443 '[email protected]': {},
1444 },
1445 '@scope/[email protected] (workspace)': {
1446 '@scope/[email protected] (workspace)': {
1447 '[email protected]': {},
1448 },
1449 },
1450 },
1451 }
1452
1453 const { dir, registry } = await getRepo(graph)
1454
1455 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1456 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1457 await arborist.reify({ installStrategy: 'linked' })
1458
1459 const asserted = new Set()
1460 rule1.apply(t, dir, resolved, asserted)
1461 rule2.apply(t, dir, resolved, asserted)
1462 rule3.apply(t, dir, resolved, asserted)
1463 rule4.apply(t, dir, resolved, asserted)
1464 rule7.apply(t, dir, resolved, asserted)
1465 })
1466
1467 tap.test('aliased packages in workspace', async t => {
1468 /*
1469 * Dependency graph:
1470 *
1471 * root -> prettier (alias for npm:[email protected])
1472 * custom-prettier -> isexe
1473 * root -> my-pkg (workspace)
1474 * my-pkg -> prettier (alias for npm:[email protected])
1475 */
1476
1477 const graph = {
1478 registry: [
1479 { name: 'custom-prettier', version: '3.0.3', dependencies: { isexe: '1.0.0' } },
1480 { name: 'isexe', version: '1.0.0' },
1481 ],
1482 root: {
1483 name: 'myproject',
1484 version: '1.0.0',
1485 dependencies: { prettier: 'npm:[email protected]' },
1486 },
1487 workspaces: [
1488 { name: 'my-pkg', version: '1.0.0', dependencies: { prettier: 'npm:[email protected]' } },
1489 ],
1490 }
1491
1492 const resolved = {
1493 '[email protected] (root)': {
1494 '[email protected]': {
1495 '[email protected]': {},
1496 },
1497 },
1498 '[email protected] (workspace)': {
1499 '[email protected]': {
1500 '[email protected]': {},
1501 },
1502 },
1503 }
1504
1505 const { dir, registry } = await getRepo(graph)
1506
1507 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1508 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1509 await arborist.reify({ installStrategy: 'linked' })
1510
1511 // Verify symlink uses alias name, not real package name
1512 t.ok(setupRequire(dir)('prettier'), 'root can require via alias "prettier"')
1513 t.notOk(
1514 pathExists(path.join(dir, 'node_modules', 'custom-prettier')),
1515 'no custom-prettier symlink at root node_modules'
1516 )
1517
1518 const asserted = new Set()
1519 rule1.apply(t, dir, resolved, asserted)
1520 rule2.apply(t, dir, resolved, asserted)
1521 rule3.apply(t, dir, resolved, asserted)
1522 rule7.apply(t, dir, resolved, asserted)
1523 })
1524
1525 tap.test('failing optional peer deps are not installed', async t => {
1526 // Input of arborist
1527 const graph = {
1528 registry: [
1529 { name: 'which', version: '1.0.0', os: ['npmOS'] },
1530 { name: 'bar', version: '1.0.0', peerDependencies: { which: '*' }, peerDependenciesMeta: { which: { optional: true } } },
1531 ],
1532 root: {
1533 name: 'foo', version: '1.2.3', optionalDependencies: { which: '1.0.0', bar: '*' },
1534 },
1535 }
1536
1537 const { dir, registry } = await getRepo(graph)
1538
1539 // Note that we override this cache to prevent interference from other tests
1540 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1541 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1542 await arborist.reify({ installStrategy: 'linked' })
1543
1544 t.notOk(setupRequire(dir)('bar', 'which'), 'Failing optional peer deps should not be installed')
1545 })
1546
1547 // Virtual packages are 2 packages that have the same version but are
1548 // duplicated on disk to solve peer-dependency conflict.
1549 tap.test('virtual packages', async t => {
1550 // Input of arborist
1551 const graph = {
1552 registry: [
1553 { name: 'foo', version: '1.0.0' },
1554 { name: 'foo', version: '2.0.0', peerDependencies: { cat: '*' } },
1555 { name: 'cat', version: '1.0.0' },
1556 { name: 'cat', version: '2.0.0' },
1557 { name: 'bar', version: '1.0.0', dependencies: { foo: '2.0.0', cat: '2.0.0' } },
1558 { name: 'baz', version: '1.0.0', dependencies: { foo: '2.0.0', cat: '1.0.0' } },
1559 ],
1560 root: {
1561 name: 'toor', version: '1.2.3', dependencies: { foo: '1.0.0', bar: '*', baz: '*', cat: '1.0.0' },
1562 },
1563 }
1564
1565 // expected output
1566 const resolved = {
1567 '[email protected] (root)': {
1568 '[email protected]': {},
1569 '[email protected]': {
1570 '[email protected]': {
1571 '[email protected] (peer)': {},
1572 },
1573 '[email protected]': {},
1574 },
1575 '[email protected]': {
1576 '[email protected]': {
1577 '[email protected] (peer)': {},
1578 },
1579 '[email protected]': {},
1580 },
1581 '[email protected]': {},
1582 },
1583 }
1584
1585 const { dir, registry } = await getRepo(graph)
1586
1587 // Note that we override this cache to prevent interference from other tests
1588 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1589 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1590 await arborist.reify({ installStrategy: 'linked' })
1591
1592 const asserted = new Set()
1593 rule1.apply(t, dir, resolved, asserted)
1594 rule2.apply(t, dir, resolved, asserted)
1595 rule3.apply(t, dir, resolved, asserted)
1596 rule4.apply(t, dir, resolved, asserted)
1597 rule5.apply(t, dir, resolved, asserted)
1598 rule6.apply(t, dir, resolved, asserted)
1599 rule7.apply(t, dir, resolved, asserted)
1600 })
1601
1602 tap.test('postinstall scripts are run', async t => {
1603 // Input of arborist
1604 const graph = {
1605 registry: [
1606 { name: 'which', version: '1.0.0', scripts: { postinstall: `node -e "fs.openSync('postInstallRanWhich', 'w')"` } },
1607 ],
1608 root: {
1609 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0' },
1610 },
1611 workspaces: [
1612 { name: 'bar', version: '1.0.0', scripts: { postinstall: `node -e "fs.openSync('postInstallRanBar', 'w')"` } },
1613 ],
1614 }
1615
1616 const { dir, registry } = await getRepo(graph)
1617
1618 // Note that we override this cache to prevent interference from other tests
1619 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1620 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1621 await arborist.reify({ installStrategy: 'linked' })
1622
1623 const postInstallRanWhich = pathExists(`${setupRequire(dir)('which')}/postInstallRanWhich`)
1624 t.ok(postInstallRanWhich)
1625
1626 const postInstallRanBar = pathExists(`${path.join(dir, 'packages', 'bar')}/postInstallRanBar`)
1627 t.ok(postInstallRanBar)
1628 })
1629
1630 tap.test('postinstall scripts run once for store packages', async t => {
1631 // Regression test: store links should not cause scripts to run twice.
1632 // The store entry and its symlink both end up in the build queue, but
1633 // only the store entry should run scripts.
1634 const graph = {
1635 registry: [
1636 {
1637 name: 'which',
1638 version: '1.0.0',
1639 scripts: {
1640 postinstall: 'node -e "var c=0;try{c=+fs.readFileSync(\'postinstall-count\',\'utf8\')}catch(e){};fs.writeFileSync(\'postinstall-count\',String(c+1))"',
1641 },
1642 },
1643 ],
1644 root: {
1645 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0' },
1646 },
1647 }
1648
1649 const { dir, registry } = await getRepo(graph)
1650
1651 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1652 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1653 await arborist.reify({ installStrategy: 'linked' })
1654
1655 const whichDir = setupRequire(dir)('which')
1656 const count = Number(fs.readFileSync(`${whichDir}/postinstall-count`, 'utf8'))
1657 t.equal(count, 1, 'postinstall ran exactly once')
1658 })
1659
1660 tap.test('workspace-filtered install with linked strategy', async t => {
1661 // Two workspaces sharing the same dependency must not crash when installing with --workspace + --install-strategy=linked.
1662 const graph = {
1663 registry: [
1664 { name: 'abbrev', version: '2.0.0' },
1665 ],
1666 root: {
1667 name: 'myroot', version: '1.0.0',
1668 },
1669 workspaces: [
1670 { name: 'ws-a', version: '1.0.0', dependencies: { abbrev: '2.0.0' } },
1671 { name: 'ws-b', version: '1.0.0', dependencies: { abbrev: '2.0.0' } },
1672 ],
1673 }
1674
1675 const { dir, registry } = await getRepo(graph)
1676
1677 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1678 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1679
1680 // Full install first
1681 await arborist.reify({ installStrategy: 'linked' })
1682
1683 // Verify store entry exists
1684 const storeDir = path.join(dir, 'node_modules', '.store')
1685 const storeEntries = fs.readdirSync(storeDir)
1686 t.ok(storeEntries.some(e => e.startsWith('abbrev@')), 'store has abbrev entry after full install')
1687
1688 // Workspace-filtered install must not crash
1689 const arborist2 = new Arborist({
1690 path: dir,
1691 registry,
1692 packumentCache: new Map(),
1693 cache,
1694 workspaces: ['ws-a'],
1695 })
1696 await arborist2.reify({
1697 installStrategy: 'linked',
1698 workspaces: ['ws-a'],
1699 })
1700
1701 // Verify workspace filtering was actually applied (not silently skipped)
1702 t.ok(arborist2.diff.filterSet.size > 0, 'workspace filter was applied to diff')
1703
1704 // Store entries still intact
1705 const storeEntries2 = fs.readdirSync(storeDir)
1706 t.ok(storeEntries2.some(e => e.startsWith('abbrev@')), 'store entries preserved after ws install')
1707
1708 // Workspace symlinks preserved
1709 const wsALink = fs.readlinkSync(path.join(dir, 'packages', 'ws-a', 'node_modules', 'abbrev'))
1710 t.ok(wsALink.includes('.store'), 'workspace a abbrev symlink points to store')
1711
1712 const wsBLink = fs.readlinkSync(path.join(dir, 'packages', 'ws-b', 'node_modules', 'abbrev'))
1713 t.ok(wsBLink.includes('.store'), 'workspace b abbrev symlink preserved')
1714 })
1715
1716 tap.test('bins are installed', async t => {
1717 // Input of arborist
1718 const graph = {
1719 registry: [
1720 { name: 'which', version: '1.0.0', bin: './bin.js' },
1721 ],
1722 root: {
1723 name: 'foo', version: '1.2.3', dependencies: { which: '1.0.0' },
1724 },
1725 workspaces: [
1726 { name: 'bar', version: '1.0.0', dependencies: { which: '1.0.0' }, bin: './bin.js' },
1727 ],
1728 }
1729
1730 const { dir, registry } = await getRepo(graph)
1731
1732 // Note that we override this cache to prevent interference from other tests
1733 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1734 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1735 await arborist.reify({ installStrategy: 'linked' })
1736
1737 // TODO: make the test not assume folder structure
1738 // TODO should the bin also be in the store?
1739 // const binFromWhichToWhich = pathExists(fs.realpathSync(`${setupRequire(dir)('which')}/../.bin/which`))
1740 // t.ok(binFromWhichToWhich)
1741
1742 const binFromRootToWhich = pathExists(`${dir}/node_modules/.bin/which`)
1743 t.ok(binFromRootToWhich)
1744
1745 // bar is not a root dep, so its bin should be in the workspace's own node_modules
1746 const binFromBarToWhich = pathExists(`${path.join(dir, 'packages', 'bar')}/node_modules/.bin/which`)
1747 t.ok(binFromBarToWhich)
1748 })
1749
1750 tap.test('file: dependency with linked strategy', async t => {
1751 /*
1752 * Regression test for https://github.com/npm/cli/issues/7549
1753 *
1754 * A relative file: dependency (file:./project2) was incorrectly resolved as file:../project2, causing ENOENT errors because the path was resolved one level above the project root.
1755 */
1756 const graph = {
1757 registry: [],
1758 root: {
1759 name: 'project1',
1760 version: '1.0.0',
1761 dependencies: { project2: 'file:./project2' },
1762 },
1763 }
1764
1765 const { dir, registry } = await getRepo(graph)
1766
1767 // Create the local file: dependency on disk
1768 const depDir = path.join(dir, 'project2')
1769 fs.mkdirSync(depDir, { recursive: true })
1770 fs.writeFileSync(path.join(depDir, 'package.json'), JSON.stringify({
1771 name: 'project2',
1772 version: '1.0.0',
1773 }))
1774 fs.writeFileSync(path.join(depDir, 'index.js'), "module.exports = 'project2'")
1775
1776 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1777 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1778 await arborist.reify({ installStrategy: 'linked' })
1779
1780 // The file dep should be symlinked in node_modules
1781 const linkPath = path.join(dir, 'node_modules', 'project2')
1782 const stat = fs.lstatSync(linkPath)
1783 t.ok(stat.isSymbolicLink(), 'project2 is a symlink in node_modules')
1784
1785 // The symlink should resolve to the actual local directory
1786 const realpath = fs.realpathSync(linkPath)
1787 t.equal(realpath, depDir, 'symlink points to the correct local directory')
1788
1789 // The package should be requireable
1790 t.ok(setupRequire(dir)('project2'), 'project2 can be required from root')
1791 })
1792
1793 tap.test('npm link (external file: dep) with linked strategy', async t => {
1794 // Regression test: `npm link` creates a file: dependency pointing outside the project root.
1795 // The linked strategy should symlink it directly instead of trying to extract it into .store/.
1796 const graph = {
1797 registry: [
1798 { name: 'abbrev', version: '2.0.0' },
1799 ],
1800 root: {
1801 name: 'my-app',
1802 version: '1.0.0',
1803 dependencies: { abbrev: '2.0.0' },
1804 },
1805 }
1806
1807 const { dir, registry } = await getRepo(graph)
1808
1809 // Create an external package OUTSIDE the project root (simulates npm link target)
1810 const externalPkgDir = path.join(path.dirname(dir), 'external-pkg')
1811 fs.mkdirSync(externalPkgDir, { recursive: true })
1812 fs.writeFileSync(path.join(externalPkgDir, 'package.json'), JSON.stringify({
1813 name: 'external-pkg',
1814 version: '1.0.0',
1815 }))
1816 fs.writeFileSync(path.join(externalPkgDir, 'index.js'), "module.exports = 'external'")
1817
1818 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1819
1820 // First install without the linked package
1821 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1822 await arb1.reify({ installStrategy: 'linked' })
1823
1824 // Now simulate `npm link external-pkg` by adding a file: dep and reifying
1825 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1826 await arb2.reify({ installStrategy: 'linked', add: [`file:${externalPkgDir}`] })
1827
1828 // The external package should be symlinked in node_modules
1829 const linkPath = path.join(dir, 'node_modules', 'external-pkg')
1830 const stat = fs.lstatSync(linkPath)
1831 t.ok(stat.isSymbolicLink(), 'external-pkg is a symlink in node_modules')
1832
1833 // The symlink should resolve to the actual external directory
1834 const realpath = fs.realpathSync(linkPath)
1835 t.equal(realpath, externalPkgDir, 'symlink points to the correct external directory')
1836
1837 // The existing store packages should still be intact
1838 const storePath = path.join(dir, 'node_modules', '.store')
1839 const storeEntries = fs.readdirSync(storePath)
1840 t.ok(storeEntries.some(e => e.startsWith('abbrev@')), 'abbrev is still in the store')
1841 t.notOk(storeEntries.some(e => e.startsWith('external-pkg@')), 'external-pkg is NOT in the store')
1842 })
1843
1844 tap.test('subsequent linked install is a no-op', async t => {
1845 const graph = {
1846 registry: [
1847 { name: 'which', version: '1.0.0', bin: './bin.js', dependencies: { isexe: '^1.0.0' } },
1848 { name: 'isexe', version: '1.0.0' },
1849 ],
1850 root: {
1851 name: 'myproject',
1852 version: '1.0.0',
1853 dependencies: { which: '1.0.0' },
1854 },
1855 }
1856 const { dir, registry } = await getRepo(graph)
1857 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1858
1859 // First install
1860 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1861 await arb1.reify({ installStrategy: 'linked' })
1862
1863 // Verify packages are installed
1864 t.ok(fs.lstatSync(path.join(dir, 'node_modules', 'which')).isSymbolicLink(),
1865 'which is a symlink after first install')
1866
1867 // Second install — should detect everything is up-to-date
1868 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1869 await arb2.reify({ installStrategy: 'linked' })
1870
1871 // Verify the diff has zero actionable leaves
1872 const leaves = arb2.diff?.leaves || []
1873 const actions = leaves.filter(l => l.action)
1874 t.equal(actions.length, 0, 'second install should have no diff actions')
1875
1876 // Verify unchanged nodes were detected
1877 t.ok(arb2.diff.unchanged.length > 0, 'second install should have unchanged nodes')
1878
1879 // Verify packages are still correctly installed
1880 t.ok(fs.lstatSync(path.join(dir, 'node_modules', 'which')).isSymbolicLink(),
1881 'which is still a symlink after second install')
1882 t.ok(setupRequire(dir)('which'), 'which is requireable after second install')
1883 })
1884
1885 tap.test('workspace links are not affected by store resolved fix', async t => {
1886 const graph = {
1887 registry: [
1888 { name: 'abbrev', version: '1.0.0' },
1889 ],
1890 root: {
1891 name: 'myproject',
1892 version: '1.0.0',
1893 dependencies: { abbrev: '1.0.0' },
1894 },
1895 workspaces: [
1896 { name: 'mypkg', version: '1.0.0', dependencies: { abbrev: '1.0.0' } },
1897 ],
1898 }
1899 const { dir, registry } = await getRepo(graph)
1900 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1901
1902 // First install
1903 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1904 await arb1.reify({ installStrategy: 'linked' })
1905
1906 // Second install
1907 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1908 await arb2.reify({ installStrategy: 'linked' })
1909
1910 // Verify the workspace's own deps still resolve from inside the workspace after the second install
1911 t.ok(setupRequire(path.join(dir, 'packages', 'mypkg'))('abbrev'), 'workspace dep is requireable from inside workspace after second install')
1912 t.ok(setupRequire(dir)('abbrev'), 'registry dep is requireable after second install')
1913
1914 // Verify the diff has unchanged nodes (store entries are correctly matched)
1915 t.ok(arb2.diff.unchanged.length > 0, 'second install should have unchanged nodes')
1916 })
1917
1918 tap.test('idempotent install with cross-workspace deps (diamond pattern)', async t => {
1919 // Regression: when workspace-x depends on workspace-y (and both share a registry dep), the second install would report "changed N packages" because (1) workspace link resolved values didn't match between ideal and actual trees, and (2) actual fsChildren overwrote synthetic store entries in the diff proxy.
1920 const graph = {
1921 registry: [
1922 { name: 'abbrev', version: '2.0.0' },
1923 ],
1924 root: {
1925 name: 'myproject',
1926 version: '1.0.0',
1927 dependencies: { 'workspace-x': '*', 'workspace-y': '*' },
1928 },
1929 workspaces: [
1930 { name: 'workspace-x', version: '1.0.0', dependencies: { abbrev: '2.0.0', 'workspace-y': '*' } },
1931 { name: 'workspace-y', version: '1.0.0', dependencies: { abbrev: '2.0.0' } },
1932 ],
1933 }
1934 const { dir, registry } = await getRepo(graph)
1935 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1936
1937 // First install
1938 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1939 await arb1.reify({ installStrategy: 'linked' })
1940
1941 // Second install — should detect everything is up-to-date
1942 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1943 await arb2.reify({ installStrategy: 'linked' })
1944
1945 const leaves = arb2.diff?.leaves || []
1946 const actions = leaves.filter(l => l.action)
1947 t.equal(actions.length, 0, 'second install should have no diff actions')
1948 t.ok(arb2.diff.unchanged.length > 0, 'second install should have unchanged nodes')
1949
1950 // Verify packages are still correctly installed (abbrev is a workspace dep, not root)
1951 t.ok(setupRequire(path.join(dir, 'packages', 'workspace-y'))('abbrev'),
1952 'abbrev is requireable from workspace after second install')
1953 })
1954
1955 tap.test('orphaned store entries are cleaned up on dependency update', async t => {
1956 const graph = {
1957 registry: [
1958 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
1959 { name: 'which', version: '2.0.0', dependencies: { isexe: '^1.0.0' } },
1960 { name: 'isexe', version: '1.0.0' },
1961 ],
1962 root: {
1963 name: 'myproject',
1964 version: '1.0.0',
1965 dependencies: { which: '1.0.0' },
1966 },
1967 }
1968 const { dir, registry } = await getRepo(graph)
1969 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1970 const storeDir = path.join(dir, 'node_modules', '.store')
1971
1972 // First install — [email protected]
1973 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1974 await arb1.reify({ installStrategy: 'linked' })
1975
1976 const entriesAfterV1 = fs.readdirSync(storeDir)
1977 t.ok(entriesAfterV1.some(e => e.startsWith('[email protected]')),
1978 'store has [email protected] entry after first install')
1979
1980 // Update package.json to depend on [email protected]
1981 const pkgPath = path.join(dir, 'package.json')
1982 const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
1983 pkg.dependencies.which = '2.0.0'
1984 fs.writeFileSync(pkgPath, JSON.stringify(pkg))
1985
1986 // Second install — [email protected]
1987 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1988 await arb2.reify({ installStrategy: 'linked' })
1989
1990 const entriesAfterV2 = fs.readdirSync(storeDir)
1991 t.ok(entriesAfterV2.some(e => e.startsWith('[email protected]')),
1992 'store has [email protected] entry after update')
1993 t.notOk(entriesAfterV2.some(e => e.startsWith('[email protected]')),
1994 'old [email protected] store entry is removed after update')
1995 })
1996
1997 tap.test('orphaned store entries are cleaned up on dependency removal', async t => {
1998 const graph = {
1999 registry: [
2000 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
2001 { name: 'isexe', version: '1.0.0' },
2002 ],
2003 root: {
2004 name: 'myproject',
2005 version: '1.0.0',
2006 dependencies: { which: '1.0.0' },
2007 },
2008 }
2009 const { dir, registry } = await getRepo(graph)
2010 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2011 const storeDir = path.join(dir, 'node_modules', '.store')
2012
2013 // First install
2014 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2015 await arb1.reify({ installStrategy: 'linked' })
2016
2017 t.ok(fs.readdirSync(storeDir).length > 0, 'store has entries after install')
2018
2019 // Remove the dependency
2020 const pkgPath = path.join(dir, 'package.json')
2021 const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
2022 delete pkg.dependencies
2023 fs.writeFileSync(pkgPath, JSON.stringify(pkg))
2024
2025 // Reinstall
2026 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2027 await arb2.reify({ installStrategy: 'linked' })
2028
2029 const entriesAfterRemoval = fs.readdirSync(storeDir)
2030 t.equal(entriesAfterRemoval.length, 0,
2031 'all store entries are removed when dependencies are removed')
2032
2033 // https://github.com/npm/cli/issues/9308 — the top-level node_modules symlink for the removed dep was left behind, dangling into the just-cleaned store.
2034 t.notOk(fs.existsSync(path.join(dir, 'node_modules', 'which')),
2035 'top-level symlink for removed dependency is also cleaned up')
2036 })
2037
2038 tap.test('orphaned link inside workspace node_modules is cleaned up on dependency removal', async t => {
2039 const graph = {
2040 registry: [
2041 { name: 'abbrev', version: '4.0.0' },
2042 ],
2043 root: {
2044 name: 'root',
2045 version: '1.0.0',
2046 },
2047 workspaces: [
2048 { name: 'a', version: '1.0.0', dependencies: { abbrev: '^4.0.0' } },
2049 ],
2050 }
2051 const { dir, registry } = await getRepo(graph)
2052 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2053
2054 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2055 await arb1.reify({ installStrategy: 'linked' })
2056
2057 const wsLink = path.join(dir, 'packages', 'a', 'node_modules', 'abbrev')
2058 t.ok(fs.existsSync(wsLink), 'abbrev is linked into workspace node_modules')
2059
2060 // Drop abbrev from the workspace package.json
2061 const wsPkgPath = path.join(dir, 'packages', 'a', 'package.json')
2062 const wsPkg = JSON.parse(fs.readFileSync(wsPkgPath, 'utf8'))
2063 delete wsPkg.dependencies
2064 fs.writeFileSync(wsPkgPath, JSON.stringify(wsPkg))
2065
2066 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2067 await arb2.reify({ installStrategy: 'linked' })
2068
2069 t.notOk(fs.existsSync(wsLink), 'abbrev symlink target no longer resolves')
2070 t.notOk(fs.lstatSync(wsLink, { throwIfNoEntry: false }),
2071 'abbrev symlink itself is removed from workspace node_modules')
2072 })
2073
2074 tap.test('orphaned link in declared-workspace node_modules is cleaned up when last dep is removed', async t => {
2075 // Reproduces the case where the workspace is also a root dependency, so its self-link sits at the ROOT node_modules and the workspace's own node_modules has no surviving links after removing its only dep.
2076 // Without explicitly seeding each workspace's node_modules into the sweep map, that directory would never be visited and the orphan symlink would remain.
2077 const graph = {
2078 registry: [
2079 { name: 'abbrev', version: '4.0.0' },
2080 ],
2081 root: {
2082 name: 'root',
2083 version: '1.0.0',
2084 dependencies: { a: '*' },
2085 },
2086 workspaces: [
2087 { name: 'a', version: '1.0.0', dependencies: { abbrev: '^4.0.0' } },
2088 ],
2089 }
2090 const { dir, registry } = await getRepo(graph)
2091 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2092
2093 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2094 await arb1.reify({ installStrategy: 'linked' })
2095
2096 const wsLink = path.join(dir, 'packages', 'a', 'node_modules', 'abbrev')
2097 t.ok(fs.lstatSync(wsLink, { throwIfNoEntry: false }), 'abbrev is linked into workspace node_modules')
2098
2099 const wsPkgPath = path.join(dir, 'packages', 'a', 'package.json')
2100 const wsPkg = JSON.parse(fs.readFileSync(wsPkgPath, 'utf8'))
2101 delete wsPkg.dependencies
2102 fs.writeFileSync(wsPkgPath, JSON.stringify(wsPkg))
2103
2104 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2105 await arb2.reify({ installStrategy: 'linked' })
2106
2107 t.notOk(fs.lstatSync(wsLink, { throwIfNoEntry: false }),
2108 'abbrev symlink is removed even though the workspace itself is the only declared root dep')
2109 })
2110
2111 tap.test('orphan sweep is skipped on dryRun and packageLockOnly linked installs', async t => {
2112 // The sweep mutates node_modules; dry-run and package-lock-only installs must not touch the filesystem.
2113 const graph = {
2114 registry: [
2115 { name: 'abbrev', version: '4.0.0' },
2116 ],
2117 root: {
2118 name: 'myproject',
2119 version: '1.0.0',
2120 dependencies: { abbrev: '^4.0.0' },
2121 },
2122 }
2123 const { dir, registry } = await getRepo(graph)
2124 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2125
2126 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2127 await arb1.reify({ installStrategy: 'linked' })
2128 const linkPath = path.join(dir, 'node_modules', 'abbrev')
2129 t.ok(fs.lstatSync(linkPath, { throwIfNoEntry: false }), 'abbrev link present after first install')
2130
2131 // Drop the dep, then run dryRun and packageLockOnly — neither should remove the orphan
2132 const pkgPath = path.join(dir, 'package.json')
2133 const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
2134 delete pkg.dependencies
2135 fs.writeFileSync(pkgPath, JSON.stringify(pkg))
2136
2137 const arbDry = new Arborist({ path: dir, registry, packumentCache: new Map(), cache, dryRun: true })
2138 await arbDry.reify({ installStrategy: 'linked', dryRun: true })
2139 t.ok(fs.lstatSync(linkPath, { throwIfNoEntry: false }),
2140 'dryRun does not remove orphan symlink')
2141
2142 const arbLockOnly = new Arborist({ path: dir, registry, packumentCache: new Map(), cache, packageLockOnly: true })
2143 await arbLockOnly.reify({ installStrategy: 'linked', packageLockOnly: true })
2144 t.ok(fs.lstatSync(linkPath, { throwIfNoEntry: false }),
2145 'packageLockOnly does not remove orphan symlink')
2146
2147 // A real install does perform the cleanup
2148 const arbReal = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2149 await arbReal.reify({ installStrategy: 'linked' })
2150 t.notOk(fs.lstatSync(linkPath, { throwIfNoEntry: false }),
2151 'real install cleans up the orphan symlink')
2152 })
2153
2154 tap.test('orphan sweep is scoped to in-filter workspaces during workspace-filtered linked install', async t => {
2155 // Filtered installs should clean up dependencies removed from the targeted workspace, but leave out-of-scope workspaces alone.
2156 // ws-a is the in-filter workspace: it keeps one dep (which) and drops one (abbrev) so both the surviving-link and orphan-sweep paths are exercised inside an in-scope workspace.
2157 // ws-c is also in the filter but starts with one dep (abbrev) and drops it entirely, exercising the case where the in-filter workspace's node_modules dir is not populated by any surviving links.
2158 // ws-b is out of filter and gets a stale link planted to verify it is preserved.
2159 const graph = {
2160 registry: [
2161 { name: 'abbrev', version: '4.0.0' },
2162 { name: 'which', version: '4.0.0' },
2163 ],
2164 root: {
2165 name: 'myroot',
2166 version: '1.0.0',
2167 // ws-c is declared as a root dep so its self-link lives at root node_modules — that means ws-c's own node_modules has no self-link, and dropping its only dep leaves the dir empty.
2168 dependencies: { 'ws-c': '*' },
2169 },
2170 workspaces: [
2171 { name: 'ws-a', version: '1.0.0', dependencies: { abbrev: '4.0.0', which: '4.0.0' } },
2172 { name: 'ws-b', version: '1.0.0', dependencies: { abbrev: '4.0.0' } },
2173 { name: 'ws-c', version: '1.0.0', dependencies: { abbrev: '4.0.0' } },
2174 ],
2175 }
2176
2177 const { dir, registry } = await getRepo(graph)
2178 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2179
2180 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2181 await arb1.reify({ installStrategy: 'linked' })
2182
2183 // Plant a stale orphan link inside ws-b (out-of-filter): the sweep must NOT touch it.
2184 const stalePath = path.join(dir, 'packages', 'ws-b', 'node_modules', 'stale-pkg')
2185 fs.symlinkSync('../../../node_modules/.store/nonexistent/node_modules/stale-pkg', stalePath)
2186
2187 // Drop abbrev from ws-a's package.json so that ws-a/node_modules/abbrev becomes orphan; which stays as a surviving link.
2188 const wsAPkgPath = path.join(dir, 'packages', 'ws-a', 'package.json')
2189 const wsAPkg = JSON.parse(fs.readFileSync(wsAPkgPath, 'utf8'))
2190 wsAPkg.dependencies = { which: '4.0.0' }
2191 fs.writeFileSync(wsAPkgPath, JSON.stringify(wsAPkg))
2192
2193 // Drop all deps from ws-c so its node_modules has no surviving links — exercises the in-filter empty-dir seeding path.
2194 const wsCPkgPath = path.join(dir, 'packages', 'ws-c', 'package.json')
2195 const wsCPkg = JSON.parse(fs.readFileSync(wsCPkgPath, 'utf8'))
2196 delete wsCPkg.dependencies
2197 fs.writeFileSync(wsCPkgPath, JSON.stringify(wsCPkg))
2198
2199 const arb2 = new Arborist({
2200 path: dir,
2201 registry,
2202 packumentCache: new Map(),
2203 cache,
2204 workspaces: ['ws-a', 'ws-c'],
2205 })
2206 await arb2.reify({
2207 installStrategy: 'linked',
2208 workspaces: ['ws-a', 'ws-c'],
2209 })
2210
2211 t.ok(arb2.diff.filterSet.size > 0, 'filterSet is populated for filtered install')
2212 t.notOk(fs.lstatSync(path.join(dir, 'packages', 'ws-a', 'node_modules', 'abbrev'), { throwIfNoEntry: false }),
2213 'orphan link in in-filter workspace with surviving deps is removed')
2214 t.ok(fs.lstatSync(path.join(dir, 'packages', 'ws-a', 'node_modules', 'which'), { throwIfNoEntry: false }),
2215 'surviving link in in-filter workspace is preserved')
2216 t.notOk(fs.lstatSync(path.join(dir, 'packages', 'ws-c', 'node_modules', 'abbrev'), { throwIfNoEntry: false }),
2217 'orphan link in in-filter workspace with no surviving deps is removed')
2218 t.ok(fs.lstatSync(stalePath, { throwIfNoEntry: false }),
2219 'stale link in out-of-filter workspace is preserved')
2220 })
2221
2222 tap.test('orphan sweep includes root node_modules when --include-workspace-root is set', async t => {
2223 // With --include-workspace-root, the filter scope pulls root deps in too, so dropped root deps must be cleaned up alongside the in-filter workspaces.
2224 const graph = {
2225 registry: [
2226 { name: 'abbrev', version: '4.0.0' },
2227 { name: 'which', version: '4.0.0' },
2228 ],
2229 root: {
2230 name: 'myroot',
2231 version: '1.0.0',
2232 dependencies: { which: '4.0.0' },
2233 },
2234 workspaces: [
2235 { name: 'ws-a', version: '1.0.0', dependencies: { abbrev: '4.0.0' } },
2236 ],
2237 }
2238
2239 const { dir, registry } = await getRepo(graph)
2240 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2241
2242 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2243 await arb1.reify({ installStrategy: 'linked' })
2244 t.ok(fs.lstatSync(path.join(dir, 'node_modules', 'which'), { throwIfNoEntry: false }), 'which is installed at root')
2245
2246 // Drop the root dep so node_modules/which becomes orphan.
2247 const pkgPath = path.join(dir, 'package.json')
2248 const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
2249 delete pkg.dependencies
2250 fs.writeFileSync(pkgPath, JSON.stringify(pkg))
2251
2252 const arb2 = new Arborist({
2253 path: dir,
2254 registry,
2255 packumentCache: new Map(),
2256 cache,
2257 workspaces: ['ws-a'],
2258 includeWorkspaceRoot: true,
2259 })
2260 await arb2.reify({
2261 installStrategy: 'linked',
2262 workspaces: ['ws-a'],
2263 includeWorkspaceRoot: true,
2264 })
2265
2266 t.notOk(fs.lstatSync(path.join(dir, 'node_modules', 'which'), { throwIfNoEntry: false }),
2267 'orphan root dep is removed when --include-workspace-root scope covers it')
2268 })
2269
2270 tap.test('hand-made symlink inside the project root is intentionally swept by linked install', async t => {
2271 // Documents an explicit trade-off: a hand-made symlink whose target lives inside the project (e.g. node_modules/local-tool -> ../tools/local-tool) is indistinguishable from a workspace self-link or store link by target alone.
2272 // The linked sweep treats it as orphaned and removes it on the next reify, matching how the default install strategy already behaves with intra-project symlinks.
2273 // External targets (outside the project root) remain preserved — see the sibling test 'unmanaged symlinks (e.g. npm link) in node_modules are preserved across reify'.
2274 const graph = {
2275 registry: [
2276 { name: 'abbrev', version: '4.0.0' },
2277 ],
2278 root: {
2279 name: 'myproject',
2280 version: '1.0.0',
2281 dependencies: { abbrev: '^4.0.0' },
2282 },
2283 }
2284 const { dir, registry } = await getRepo(graph)
2285 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2286
2287 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2288 await arb1.reify({ installStrategy: 'linked' })
2289
2290 // Create a target folder inside the project root and link to it from node_modules.
2291 const localToolDir = path.join(dir, 'tools', 'local-tool')
2292 fs.mkdirSync(localToolDir, { recursive: true })
2293 fs.writeFileSync(path.join(localToolDir, 'package.json'),
2294 JSON.stringify({ name: 'local-tool', version: '0.0.0' }))
2295 const intraLink = path.join(dir, 'node_modules', 'local-tool')
2296 fs.symlinkSync(path.join('..', 'tools', 'local-tool'), intraLink)
2297
2298 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2299 await arb2.reify({ installStrategy: 'linked' })
2300
2301 t.notOk(fs.lstatSync(intraLink, { throwIfNoEntry: false }),
2302 'intra-project hand-made symlink is removed by the sweep (intentional trade-off)')
2303 t.ok(fs.existsSync(localToolDir),
2304 'the target directory itself is left intact — only the symlink is removed')
2305 })
2306
2307 tap.test('orphaned workspace self-link in root node_modules is cleaned up when workspace is undeclared', async t => {
2308 // When root declares a workspace as a dependency, the workspace gets a self-link at root node_modules (e.g. node_modules/a -> ../packages/a).
2309 // If the workspace is later removed from root's dependencies, that self-link must be cleaned up.
2310 // It is a symlink npm itself created, but its target lives outside .store/, so the sweep must accept any orphan whose target resolves inside the project root.
2311 const graph = {
2312 registry: [],
2313 root: {
2314 name: 'root',
2315 version: '1.0.0',
2316 dependencies: { a: '*', b: '*' },
2317 },
2318 workspaces: [
2319 { name: 'a', version: '1.0.0' },
2320 { name: 'b', version: '1.0.0' },
2321 ],
2322 }
2323 const { dir, registry } = await getRepo(graph)
2324 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2325
2326 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2327 await arb1.reify({ installStrategy: 'linked' })
2328
2329 t.ok(fs.lstatSync(path.join(dir, 'node_modules', 'a'), { throwIfNoEntry: false }), 'a self-link present')
2330 t.ok(fs.lstatSync(path.join(dir, 'node_modules', 'b'), { throwIfNoEntry: false }), 'b self-link present')
2331
2332 // Drop workspace a from both root deps and the workspaces glob
2333 const pkgPath = path.join(dir, 'package.json')
2334 const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
2335 pkg.workspaces = ['packages/b']
2336 pkg.dependencies = { b: '*' }
2337 fs.writeFileSync(pkgPath, JSON.stringify(pkg))
2338
2339 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2340 await arb2.reify({ installStrategy: 'linked' })
2341
2342 t.notOk(fs.lstatSync(path.join(dir, 'node_modules', 'a'), { throwIfNoEntry: false }),
2343 'orphan workspace self-link is removed')
2344 t.ok(fs.lstatSync(path.join(dir, 'node_modules', 'b'), { throwIfNoEntry: false }),
2345 'still-declared workspace self-link is preserved')
2346 })
2347
2348 tap.test('unmanaged symlinks (e.g. npm link) in node_modules are preserved across reify', async t => {
2349 // The orphan sweep should only touch links the linked strategy itself created (those resolving into the project's node_modules/.store/).
2350 // A symlink pointing outside .store/ — e.g. one created by `npm link foo` without --save or by hand — must be left alone.
2351 const graph = {
2352 registry: [
2353 { name: 'abbrev', version: '4.0.0' },
2354 ],
2355 root: {
2356 name: 'myproject',
2357 version: '1.0.0',
2358 dependencies: { abbrev: '^4.0.0' },
2359 },
2360 }
2361 const { dir, registry } = await getRepo(graph)
2362 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2363
2364 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2365 await arb1.reify({ installStrategy: 'linked' })
2366
2367 // Drop a hand-made symlink to a target outside the project's .store/
2368 const externalDir = fs.mkdtempSync(`${getTempDir()}/external-`)
2369 fs.writeFileSync(path.join(externalDir, 'package.json'),
2370 JSON.stringify({ name: 'external-pkg', version: '1.0.0' }))
2371 const externalLink = path.join(dir, 'node_modules', 'external-pkg')
2372 fs.symlinkSync(externalDir, externalLink)
2373
2374 // Remove abbrev so the sweep runs and would otherwise consider external-pkg orphaned
2375 const pkgPath = path.join(dir, 'package.json')
2376 const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
2377 delete pkg.dependencies
2378 fs.writeFileSync(pkgPath, JSON.stringify(pkg))
2379
2380 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2381 await arb2.reify({ installStrategy: 'linked' })
2382
2383 t.notOk(fs.existsSync(path.join(dir, 'node_modules', 'abbrev')),
2384 'orphan link into our .store/ is removed')
2385 t.ok(fs.lstatSync(externalLink, { throwIfNoEntry: false }),
2386 'unmanaged symlink pointing outside .store/ is preserved')
2387 })
2388
2389 tap.test('orphaned scoped top-level link is cleaned up when only one of two scoped deps is removed', async t => {
2390 const graph = {
2391 registry: [
2392 { name: '@scope/a', version: '1.0.0' },
2393 { name: '@scope/b', version: '1.0.0' },
2394 ],
2395 root: {
2396 name: 'myproject',
2397 version: '1.0.0',
2398 dependencies: { '@scope/a': '1.0.0', '@scope/b': '1.0.0' },
2399 },
2400 }
2401 const { dir, registry } = await getRepo(graph)
2402 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2403
2404 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2405 await arb1.reify({ installStrategy: 'linked' })
2406
2407 t.ok(fs.existsSync(path.join(dir, 'node_modules', '@scope', 'a')), '@scope/a installed')
2408 t.ok(fs.existsSync(path.join(dir, 'node_modules', '@scope', 'b')), '@scope/b installed')
2409
2410 // Drop @scope/a from package.json
2411 const pkgPath = path.join(dir, 'package.json')
2412 const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
2413 delete pkg.dependencies['@scope/a']
2414 fs.writeFileSync(pkgPath, JSON.stringify(pkg))
2415
2416 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2417 await arb2.reify({ installStrategy: 'linked' })
2418
2419 t.notOk(fs.existsSync(path.join(dir, 'node_modules', '@scope', 'a')),
2420 '@scope/a top-level symlink is removed')
2421 t.ok(fs.existsSync(path.join(dir, 'node_modules', '@scope', 'b')),
2422 '@scope/b top-level symlink is preserved')
2423 })
2424
2425 tap.test('store symlinks are updated when hash changes after adding a dep', async t => {
2426 const graph = {
2427 registry: [
2428 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
2429 { name: 'isexe', version: '1.0.0' },
2430 { name: 'abbrev', version: '2.0.0' },
2431 ],
2432 root: {
2433 name: 'myproject',
2434 version: '1.0.0',
2435 dependencies: { which: '1.0.0' },
2436 },
2437 }
2438 const { dir, registry } = await getRepo(graph)
2439 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2440
2441 // First install — only which
2442 const arb1 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2443 await arb1.reify({ installStrategy: 'linked' })
2444
2445 const whichLink = path.join(dir, 'node_modules', 'which')
2446 t.ok(fs.lstatSync(whichLink).isSymbolicLink(), 'which is a symlink after first install')
2447 const hashBefore = fs.readlinkSync(whichLink)
2448
2449 // Add abbrev — changes the dep graph, causing store hash recalculation
2450 const pkgPath = path.join(dir, 'package.json')
2451 const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
2452 pkg.dependencies.abbrev = '2.0.0'
2453 fs.writeFileSync(pkgPath, JSON.stringify(pkg))
2454
2455 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2456 await arb2.reify({ installStrategy: 'linked' })
2457
2458 // The symlink target should still be valid (not dangling)
2459 t.ok(fs.existsSync(whichLink), 'which symlink target exists after adding abbrev')
2460 t.ok(setupRequire(dir)('which'), 'which is requireable after adding abbrev')
2461
2462 // Verify the symlink was updated if the hash changed
2463 const hashAfter = fs.readlinkSync(whichLink)
2464 if (hashBefore !== hashAfter) {
2465 const storeDir = path.join(dir, 'node_modules', '.store')
2466 const storeEntries = fs.readdirSync(storeDir)
2467 const oldKey = hashBefore.split('/')[0].replace('.store/', '')
2468 t.notOk(storeEntries.includes(oldKey), 'old store entry was cleaned up')
2469 }
2470 })
2471
2472 function setupRequire (cwd) {
2473 return function requireChain (...chain) {
2474 return chain.reduce((path, name) => {
2475 if (path === undefined) {
2476 return undefined
2477 }
2478 return resolvePackage(name, path)
2479 }, cwd)
2480 }
2481 }
2482
2483 function pathExists (path) {
2484 try {
2485 fs.statSync(path)
2486 return true
2487 } catch (_) {
2488 return false
2489 }
2490 }
2491
2492 /**
2493 * We reimplement a lightweight version of require.resolve because the
2494 * one that is implemented in nodejs memoizes the resolution which
2495 * asserts interfering with each others
2496 **/
2497 function resolvePackage (name, from) {
2498 try {
2499 const loc = `${from}/node_modules/${name}`
2500 fs.statSync(loc)
2501 return fs.realpathSync(loc)
2502 } catch (_) {
2503 const next = path.dirname(from)
2504 if (next === from) {
2505 return undefined
2506 } else {
2507 return resolvePackage(name, next)
2508 }
2509 }
2510 }
2511
2512 function getAllPackages (resolvedGraph) {
2513 return [...getAllPackagesRecursive(resolvedGraph.root),
2514 ...(resolvedGraph.workspaces?.map(w => getAllPackagesRecursive(w)) || []).reduce((a, n) => ([...a, ...n]), [])]
2515 }
2516
2517 function getAllPackagesRecursive (resolvedGraph) {
2518 return [
2519 resolvedGraph,
2520 ...(resolvedGraph.dependencies
2521 ?.filter(d => !isLoopToken(d))
2522 .map(d => getAllPackagesRecursive(d)) || [])
2523 .reduce((a, n) => ([...a, ...n]), []),
2524 ]
2525 }
2526
2527 function withRequireChain (resolvedGraph) {
2528 return {
2529 root: {
2530 ...resolvedGraph.root,
2531 chain: [],
2532 initialDir: '.',
2533 dependencies: resolvedGraph.root.dependencies?.map(d =>
2534 withRequireChainRecursive(d, [], '.')),
2535 },
2536 workspaces: resolvedGraph.workspaces?.map(w => {
2537 const initialDir = `packages/${w.name}`
2538 return {
2539 ...w,
2540 chain: [],
2541 initialDir,
2542 dependencies: w.dependencies?.map(d => withRequireChainRecursive(d, [], initialDir)),
2543 }
2544 }),
2545 }
2546 }
2547
2548 function withRequireChainRecursive (resolvedGraph, chain, initialDir) {
2549 if (isLoopToken(resolvedGraph)) {
2550 return resolvedGraph
2551 }
2552
2553 const newChain = [...chain, resolvedGraph.name]
2554 return {
2555 ...resolvedGraph,
2556 chain: newChain,
2557 initialDir,
2558 dependencies: resolvedGraph.dependencies?.map(d =>
2559 withRequireChainRecursive(d, newChain, initialDir)),
2560 }
2561 }
2562
2563 function parseGraph (graph) {
2564 const root = Object.entries(graph).find(([key]) => key.includes('(root)'))
2565 const result = { root: parseGraphRecursive(...root), workspaces: [] }
2566
2567 Object.entries(graph).filter(([key]) => key.includes('(workspace)'))
2568 .forEach(([key, value]) => {
2569 result.workspaces.push(parseGraphRecursive(key, value))
2570 })
2571 return result
2572 }
2573
2574 function isLoopToken (obj) {
2575 return typeof obj === 'string' && /^\(back \d+\)$/.test(obj)
2576 }
2577
2578 function parseLoopToken (t) {
2579 return parseInt(/\d+/.exec(t)[0])
2580 }
2581
2582 function parseGraphRecursive (key, deps) {
2583 if (isLoopToken(key)) {
2584 return key
2585 }
2586 const name = /^(.[^@]*)@/.exec(key)[1]
2587 const version = /^.[^@]*@([^ ]*)/.exec(key)[1]
2588 const workspace = / \(workspace\)/.test(key)
2589 const peer = / \(peer\)/.test(key)
2590 const normalizedDeps = typeof deps === 'string' ? { [deps]: {} } : deps
2591 const dependencies = Object.entries(normalizedDeps).map(([key, value]) => parseGraphRecursive(key, value))
2592 return { name, version, workspace, peer, dependencies }
2593 }
2594
2595 tap.test('undeclared workspaces are not hoisted to root node_modules', async t => {
2596 // Regression test: all workspace packages were unconditionally symlinked into root node_modules/.
2597 // Only workspaces that root explicitly depends on should appear at root node_modules/.
2598 const graph = {
2599 registry: [
2600 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
2601 { name: 'isexe', version: '1.0.0' },
2602 ],
2603 root: {
2604 name: 'myapp',
2605 version: '1.0.0',
2606 dependencies: { 'ws-a': '*' },
2607 },
2608 workspaces: [
2609 { name: 'ws-a', version: '1.0.0', dependencies: { 'ws-b': '*', which: '1.0.0' } },
2610 { name: 'ws-b', version: '1.0.0' },
2611 { name: 'ws-c', version: '1.0.0' },
2612 ],
2613 }
2614
2615 const { dir, registry } = await getRepo(graph)
2616 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2617 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2618 await arborist.reify({ installStrategy: 'linked' })
2619
2620 // ws-a is declared as a root dependency — should be at root node_modules
2621 t.ok(pathExists(path.join(dir, 'node_modules', 'ws-a')),
2622 'declared workspace ws-a is symlinked at root node_modules')
2623 t.ok(fs.lstatSync(path.join(dir, 'node_modules', 'ws-a')).isSymbolicLink(),
2624 'ws-a at root is a symlink')
2625
2626 // ws-b is NOT a root dependency — should NOT be at root node_modules
2627 t.notOk(pathExists(path.join(dir, 'node_modules', 'ws-b')),
2628 'undeclared workspace ws-b is NOT at root node_modules')
2629
2630 // ws-c is NOT a root dependency — should NOT be at root node_modules
2631 t.notOk(pathExists(path.join(dir, 'node_modules', 'ws-c')),
2632 'undeclared workspace ws-c is NOT at root node_modules')
2633
2634 // ws-b should be resolvable from ws-a (ws-a depends on ws-b)
2635 t.ok(pathExists(path.join(dir, 'packages', 'ws-a', 'node_modules', 'ws-b')),
2636 'ws-b is linked in ws-a/node_modules (declared dep)')
2637
2638 // ws-c has no dependencies and is not depended on — should not be able to access ws-b
2639 t.notOk(pathExists(path.join(dir, 'packages', 'ws-c', 'node_modules', 'ws-b')),
2640 'ws-c cannot access ws-b (no dependency declared)')
2641 })
2642
2643 tap.test('undeclared workspaces do not get a self-link in their own node_modules', async t => {
2644 // Undeclared workspaces used to be self-symlinked into their own node_modules/.
2645 // Cross-workspace dep links remain unaffected, and stale self-links from older npm versions are swept on the next install.
2646 const graph = {
2647 registry: [
2648 { name: 'abbrev', version: '1.0.0' },
2649 ],
2650 root: {
2651 name: 'myapp',
2652 version: '1.0.0',
2653 dependencies: { '@scope/a': '*' },
2654 },
2655 workspaces: [
2656 { name: '@scope/a', version: '1.0.0', dependencies: { '@scope/test': '*', abbrev: '1.0.0' } },
2657 { name: '@scope/test', version: '1.0.0' },
2658 ],
2659 }
2660
2661 const { dir, registry } = await getRepo(graph)
2662 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2663 const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2664 await arborist.reify({ installStrategy: 'linked' })
2665
2666 // No self-link inside the undeclared workspace's own node_modules
2667 t.notOk(fs.lstatSync(path.join(dir, 'packages', '@scope', 'test', 'node_modules', '@scope', 'test'), { throwIfNoEntry: false }),
2668 'undeclared workspace has no self-link in its own node_modules')
2669
2670 // Cross-workspace dep link still works: @scope/a depends on @scope/test
2671 t.ok(fs.lstatSync(path.join(dir, 'packages', '@scope', 'a', 'node_modules', '@scope', 'test'), { throwIfNoEntry: false })?.isSymbolicLink(),
2672 '@scope/a still has a symlink to @scope/test in its node_modules')
2673
2674 // Stale self-link from an older npm version must be swept on a subsequent install
2675 fs.mkdirSync(path.join(dir, 'packages', '@scope', 'test', 'node_modules', '@scope'), { recursive: true })
2676 fs.symlinkSync('../../..', path.join(dir, 'packages', '@scope', 'test', 'node_modules', '@scope', 'test'))
2677 t.ok(fs.lstatSync(path.join(dir, 'packages', '@scope', 'test', 'node_modules', '@scope', 'test'), { throwIfNoEntry: false }),
2678 'stale self-link planted')
2679
2680 const arb2 = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
2681 await arb2.reify({ installStrategy: 'linked' })
2682
2683 t.notOk(fs.lstatSync(path.join(dir, 'packages', '@scope', 'test', 'node_modules', '@scope', 'test'), { throwIfNoEntry: false }),
2684 'stale self-link is removed by the orphan sweep on the next install')
2685 })
2686
2687 tap.test('omit dev dependencies with linked strategy', async t => {
2688 const graph = {
2689 registry: [
2690 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
2691 { name: 'isexe', version: '1.0.0' },
2692 { name: 'eslint', version: '1.0.0' },
2693 ],
2694 root: {
2695 name: 'myapp',
2696 version: '1.0.0',
2697 dependencies: { which: '1.0.0' },
2698 devDependencies: { eslint: '1.0.0' },
2699 },
2700 workspaces: [
2701 {
2702 name: 'mylib',
2703 version: '1.0.0',
2704 dependencies: { isexe: '1.0.0' },
2705 devDependencies: { eslint: '1.0.0' },
2706 },
2707 ],
2708 }
2709
2710 const { dir, registry } = await getRepo(graph)
2711 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2712 const arborist = new Arborist({
2713 path: dir,
2714 registry,
2715 packumentCache: new Map(),
2716 cache,
2717 omit: ['dev'],
2718 })
2719 await arborist.reify({ installStrategy: 'linked' })
2720
2721 const storeDir = path.join(dir, 'node_modules', '.store')
2722 const storeEntries = fs.readdirSync(storeDir)
2723
2724 t.ok(storeEntries.some(e => e.startsWith('which@')), 'prod dep which is in store')
2725 t.ok(storeEntries.some(e => e.startsWith('isexe@')), 'prod dep isexe is in store')
2726 t.notOk(storeEntries.some(e => e.startsWith('eslint@')), 'dev dep eslint is not in store')
2727 })
2728
2729 tap.test('omit dev deps from root even when shared with workspace prod deps', async t => {
2730 // In a monorepo, a root devDependency may also be a workspace prod dependency.
2731 // With --omit=dev, root should NOT link to it, but the workspace still should.
2732 // Also covers the case where a workspace itself is a root devDependency.
2733 const graph = {
2734 registry: [
2735 { name: 'typescript', version: '5.0.0' },
2736 { name: 'which', version: '1.0.0', dependencies: { isexe: '^1.0.0' } },
2737 { name: 'isexe', version: '1.0.0' },
2738 ],
2739 root: {
2740 name: 'myapp',
2741 version: '1.0.0',
2742 dependencies: { which: '1.0.0', mylib: '1.0.0' },
2743 devDependencies: { typescript: '5.0.0', devtool: '1.0.0' },
2744 },
2745 workspaces: [
2746 {
2747 name: 'mylib',
2748 version: '1.0.0',
2749 dependencies: { typescript: '5.0.0' },
2750 },
2751 {
2752 name: 'devtool',
2753 version: '1.0.0',
2754 },
2755 ],
2756 }
2757
2758 const { dir, registry } = await getRepo(graph)
2759 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2760 const arborist = new Arborist({
2761 path: dir,
2762 registry,
2763 packumentCache: new Map(),
2764 cache,
2765 omit: ['dev'],
2766 })
2767 await arborist.reify({ installStrategy: 'linked' })
2768
2769 const storeDir = path.join(dir, 'node_modules', '.store')
2770 const storeEntries = fs.readdirSync(storeDir)
2771
2772 // typescript should still be in the store because mylib needs it as a prod dep
2773 t.ok(storeEntries.some(e => e.startsWith('typescript@')), 'typescript is in store (workspace prod dep)')
2774 t.ok(storeEntries.some(e => e.startsWith('which@')), 'which is in store')
2775
2776 // root should NOT have a symlink to typescript (it's a dev dep of root)
2777 const rootNmEntries = fs.readdirSync(path.join(dir, 'node_modules'))
2778 t.ok(rootNmEntries.includes('which'), 'root has symlink to prod dep which')
2779 t.ok(rootNmEntries.includes('mylib'), 'root has symlink to prod workspace mylib')
2780 t.notOk(rootNmEntries.includes('typescript'), 'root does not have symlink to dev dep typescript')
2781 t.notOk(rootNmEntries.includes('devtool'), 'root does not have symlink to dev workspace devtool')
2782
2783 // workspace should have a symlink to typescript (it's a prod dep of mylib)
2784 const wsNmEntries = fs.readdirSync(path.join(dir, 'packages', 'mylib', 'node_modules'))
2785 t.ok(wsNmEntries.includes('typescript'), 'workspace has symlink to prod dep typescript')
2786 })
2787
2788 tap.test('omit optional dependencies with linked strategy', async t => {
2789 const graph = {
2790 registry: [
2791 { name: 'which', version: '1.0.0' },
2792 { name: 'fsevents', version: '1.0.0' },
2793 ],
2794 root: {
2795 name: 'myapp',
2796 version: '1.0.0',
2797 dependencies: { which: '1.0.0' },
2798 optionalDependencies: { fsevents: '1.0.0' },
2799 },
2800 workspaces: [
2801 {
2802 name: 'mylib',
2803 version: '1.0.0',
2804 dependencies: { fsevents: '1.0.0' },
2805 },
2806 ],
2807 }
2808
2809 const { dir, registry } = await getRepo(graph)
2810 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2811 const arborist = new Arborist({
2812 path: dir,
2813 registry,
2814 packumentCache: new Map(),
2815 cache,
2816 omit: ['optional'],
2817 })
2818 await arborist.reify({ installStrategy: 'linked' })
2819
2820 const rootNmEntries = fs.readdirSync(path.join(dir, 'node_modules'))
2821 t.ok(rootNmEntries.includes('which'), 'root has prod dep which')
2822 t.notOk(rootNmEntries.includes('fsevents'), 'root does not have optional dep fsevents')
2823 })
2824
2825 tap.test('omit peer dependencies with linked strategy', async t => {
2826 const graph = {
2827 registry: [
2828 { name: 'which', version: '1.0.0' },
2829 { name: 'react', version: '18.0.0' },
2830 ],
2831 root: {
2832 name: 'myapp',
2833 version: '1.0.0',
2834 dependencies: { which: '1.0.0' },
2835 peerDependencies: { react: '18.0.0' },
2836 },
2837 workspaces: [
2838 {
2839 name: 'mylib',
2840 version: '1.0.0',
2841 dependencies: { react: '18.0.0' },
2842 },
2843 ],
2844 }
2845
2846 const { dir, registry } = await getRepo(graph)
2847 const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
2848 const arborist = new Arborist({
2849 path: dir,
2850 registry,
2851 packumentCache: new Map(),
2852 cache,
2853 omit: ['peer'],
2854 })
2855 await arborist.reify({ installStrategy: 'linked' })
2856
2857 const rootNmEntries = fs.readdirSync(path.join(dir, 'node_modules'))
2858 t.ok(rootNmEntries.includes('which'), 'root has prod dep which')
2859 t.notOk(rootNmEntries.includes('react'), 'root does not have peer dep react')
2860 })
2861
2862 /*
2863 * TO TEST:
2864 * --------------------------------------
2865 * - rollbacks
2866 * - scoped installs
2867 * - overrides?
2868 * - changing repo from isolated to hoisted and from hoisted to isolated
2869 */