-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
build-ideal-tree.js
1523 lines (1356 loc) · 53.1 KB
/
build-ideal-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// mixin implementing the buildIdealTree method
const localeCompare = require('@isaacs/string-locale-compare')('en')
const rpj = require('read-package-json-fast')
const npa = require('npm-package-arg')
const pacote = require('pacote')
const cacache = require('cacache')
const { callLimit: promiseCallLimit } = require('promise-call-limit')
const realpath = require('../../lib/realpath.js')
const { resolve, dirname } = require('node:path')
const treeCheck = require('../tree-check.js')
const { readdirScoped } = require('@npmcli/fs')
const { lstat, readlink } = require('node:fs/promises')
const { depth } = require('treeverse')
const { log, time } = require('proc-log')
const { redact } = require('@npmcli/redact')
const {
OK,
REPLACE,
CONFLICT,
} = require('../can-place-dep.js')
const PlaceDep = require('../place-dep.js')
const debug = require('../debug.js')
const fromPath = require('../from-path.js')
const calcDepFlags = require('../calc-dep-flags.js')
const Shrinkwrap = require('../shrinkwrap.js')
const { defaultLockfileVersion } = Shrinkwrap
const Node = require('../node.js')
const Link = require('../link.js')
const addRmPkgDeps = require('../add-rm-pkg-deps.js')
const optionalSet = require('../optional-set.js')
const { checkEngine, checkPlatform } = require('npm-install-checks')
const relpath = require('../relpath.js')
const resetDepFlags = require('../reset-dep-flags.js')
// note: some of these symbols are shared so we can hit
// them with unit tests and reuse them across mixins
const _updateAll = Symbol.for('updateAll')
const _flagsSuspect = Symbol.for('flagsSuspect')
const _setWorkspaces = Symbol.for('setWorkspaces')
const _updateNames = Symbol.for('updateNames')
const _resolvedAdd = Symbol.for('resolvedAdd')
const _usePackageLock = Symbol.for('usePackageLock')
const _rpcache = Symbol.for('realpathCache')
const _stcache = Symbol.for('statCache')
// used by Reify mixin
const _addNodeToTrashList = Symbol.for('addNodeToTrashList')
// Push items in, pop them sorted by depth and then path
// Sorts physically shallower deps up to the front of the queue, because
// they'll affect things deeper in, then alphabetical for consistency between
// installs
class DepsQueue {
#deps = []
#sorted = true
get length () {
return this.#deps.length
}
push (item) {
if (!this.#deps.includes(item)) {
this.#sorted = false
this.#deps.push(item)
}
}
pop () {
if (!this.#sorted) {
this.#deps.sort((a, b) => (a.depth - b.depth) || localeCompare(a.path, b.path))
this.#sorted = true
}
return this.#deps.shift()
}
}
module.exports = cls => class IdealTreeBuilder extends cls {
#complete
#currentDep = null
#depsQueue = new DepsQueue()
#depsSeen = new Set()
#explicitRequests = new Set()
#follow
#installStrategy
#linkNodes = new Set()
#loadFailures = new Set()
#manifests = new Map()
#mutateTree = false
// a map of each module in a peer set to the thing that depended on
// that set of peers in the first place. Use a WeakMap so that we
// don't hold onto references for nodes that are garbage collected.
#peerSetSource = new WeakMap()
#preferDedupe = false
#prune
#strictPeerDeps
#virtualRoots = new Map()
constructor (options) {
super(options)
// normalize trailing slash
const registry = options.registry || 'https://registry.npmjs.org'
options.registry = this.registry = registry.replace(/\/+$/, '') + '/'
const {
follow = false,
installStrategy = 'hoisted',
idealTree = null,
installLinks = false,
legacyPeerDeps = false,
packageLock = true,
strictPeerDeps = false,
workspaces,
global,
} = options
this.#strictPeerDeps = !!strictPeerDeps
this.idealTree = idealTree
this.installLinks = installLinks
this.legacyPeerDeps = legacyPeerDeps
this[_usePackageLock] = packageLock
this.#installStrategy = global ? 'shallow' : installStrategy
this.#follow = !!follow
if (workspaces?.length && global) {
throw new Error('Cannot operate on workspaces in global mode')
}
this[_updateAll] = false
this[_updateNames] = []
this[_resolvedAdd] = []
}
get explicitRequests () {
return new Set(this.#explicitRequests)
}
// public method
async buildIdealTree (options = {}) {
if (this.idealTree) {
return this.idealTree
}
// allow the user to set reify options on the ctor as well.
// XXX: deprecate separate reify() options object.
options = { ...this.options, ...options }
// an empty array or any falsey value is the same as null
if (!options.add || options.add.length === 0) {
options.add = null
}
if (!options.rm || options.rm.length === 0) {
options.rm = null
}
const timeEnd = time.start('idealTree')
if (!options.add && !options.rm && !options.update && this.options.global) {
throw new Error('global requires add, rm, or update option')
}
// first get the virtual tree, if possible. If there's a lockfile, then
// that defines the ideal tree, unless the root package.json is not
// satisfied by what the ideal tree provides.
// from there, we start adding nodes to it to satisfy the deps requested
// by the package.json in the root.
this.#parseSettings(options)
// start tracker block
this.addTracker('idealTree')
try {
await this.#initTree()
await this.#inflateAncientLockfile()
await this.#applyUserRequests(options)
await this.#buildDeps()
await this.#fixDepFlags()
await this.#pruneFailedOptional()
await this.#checkEngineAndPlatform()
} finally {
timeEnd()
this.finishTracker('idealTree')
}
return treeCheck(this.idealTree)
}
async #checkEngineAndPlatform () {
const { engineStrict, npmVersion, nodeVersion } = this.options
for (const node of this.idealTree.inventory.values()) {
if (!node.optional) {
try {
// if devEngines is present in the root node we ignore the engines check
if (!(node.isRoot && node.package.devEngines)) {
checkEngine(node.package, npmVersion, nodeVersion, this.options.force)
}
} catch (err) {
if (engineStrict) {
throw err
}
log.warn(err.code, err.message, {
package: err.pkgid,
required: err.required,
current: err.current,
})
}
checkPlatform(node.package, this.options.force)
}
}
}
#parseSettings (options) {
const update = options.update === true ? { all: true }
: Array.isArray(options.update) ? { names: options.update }
: options.update || {}
if (update.all || !Array.isArray(update.names)) {
update.names = []
}
this.#complete = !!options.complete
this.#preferDedupe = !!options.preferDedupe
// validates list of update names, they must
// be dep names only, no semver ranges are supported
for (const name of update.names) {
const spec = npa(name)
const validationError =
new TypeError(`Update arguments must only contain package names, eg:
npm update ${spec.name}`)
validationError.code = 'EUPDATEARGS'
// If they gave us anything other than a bare package name
if (spec.raw !== spec.name) {
throw validationError
}
}
this[_updateNames] = update.names
this[_updateAll] = update.all
// we prune by default unless explicitly set to boolean false
this.#prune = options.prune !== false
// set if we add anything, but also set here if we know we'll make
// changes and thus have to maybe prune later.
this.#mutateTree = !!(
options.add ||
options.rm ||
update.all ||
update.names.length
)
}
// load the initial tree, either the virtualTree from a shrinkwrap,
// or just the root node from a package.json
async #initTree () {
const timeEnd = time.start('idealTree:init')
let root
if (this.options.global) {
root = await this.#globalRootNode()
} else {
try {
const pkg = await rpj(this.path + '/package.json')
root = await this.#rootNodeFromPackage(pkg)
} catch (err) {
if (err.code === 'EJSONPARSE') {
throw err
}
root = await this.#rootNodeFromPackage({})
}
}
return this[_setWorkspaces](root)
// ok to not have a virtual tree. probably initial install.
// When updating all, we load the shrinkwrap, but don't bother
// to build out the full virtual tree from it, since we'll be
// reconstructing it anyway.
.then(root => this.options.global ? root
: !this[_usePackageLock] || this[_updateAll]
? Shrinkwrap.reset({
path: this.path,
lockfileVersion: this.options.lockfileVersion,
resolveOptions: this.options,
}).then(meta => Object.assign(root, { meta }))
: this.loadVirtual({ root }))
// if we don't have a lockfile to go from, then start with the
// actual tree, so we only make the minimum required changes.
// don't do this for global installs or updates, because in those
// cases we don't use a lockfile anyway.
// Load on a new Arborist object, so the Nodes aren't the same,
// or else it'll get super confusing when we change them!
.then(async root => {
if ((!this[_updateAll] && !this.options.global && !root.meta.loadedFromDisk) || (this.options.global && this[_updateNames].length)) {
await new this.constructor(this.options).loadActual({ root })
const tree = root.target
// even though we didn't load it from a package-lock.json FILE,
// we still loaded it "from disk", meaning we have to reset
// dep flags before assuming that any mutations were reflected.
if (tree.children.size) {
root.meta.loadedFromDisk = true
// set these so that we don't try to ancient lockfile reload it
root.meta.originalLockfileVersion = root.meta.lockfileVersion = this.options.lockfileVersion || defaultLockfileVersion
}
}
root.meta.inferFormattingOptions(root.package)
return root
})
.then(tree => {
// search the virtual tree for invalid edges, if any are found add their source to
// the depsQueue so that we'll fix it later
depth({
tree,
getChildren: (node) => {
const children = []
for (const edge of node.edgesOut.values()) {
children.push(edge.to)
}
return children
},
filter: node => node,
visit: node => {
for (const edge of node.edgesOut.values()) {
if (!edge.valid) {
this.#depsQueue.push(node)
break // no need to continue the loop after the first hit
}
}
},
})
// null the virtual tree, because we're about to hack away at it
// if you want another one, load another copy.
this.idealTree = tree
this.virtualTree = null
timeEnd()
return tree
})
}
async #globalRootNode () {
const root = await this.#rootNodeFromPackage({ dependencies: {} })
// this is a gross kludge to handle the fact that we don't save
// metadata on the root node in global installs, because the "root"
// node is something like /usr/local/lib.
const meta = new Shrinkwrap({
path: this.path,
lockfileVersion: this.options.lockfileVersion,
resolveOptions: this.options,
})
meta.reset()
root.meta = meta
return root
}
async #rootNodeFromPackage (pkg) {
// if the path doesn't exist, then we explode at this point. Note that
// this is not a problem for reify(), since it creates the root path
// before ever loading trees.
// TODO: make buildIdealTree() and loadActual handle a missing root path,
// or a symlink to a missing target, and let reify() create it as needed.
const real = await realpath(this.path, this[_rpcache], this[_stcache])
const Cls = real === this.path ? Node : Link
const root = new Cls({
path: this.path,
realpath: real,
pkg,
extraneous: false,
dev: false,
devOptional: false,
peer: false,
optional: false,
global: this.options.global,
installLinks: this.installLinks,
legacyPeerDeps: this.legacyPeerDeps,
loadOverrides: true,
})
if (root.isLink) {
root.target = new Node({
path: real,
realpath: real,
pkg,
extraneous: false,
dev: false,
devOptional: false,
peer: false,
optional: false,
global: this.options.global,
installLinks: this.installLinks,
legacyPeerDeps: this.legacyPeerDeps,
root,
})
}
return root
}
// process the add/rm requests by modifying the root node, and the
// update.names request by queueing nodes dependent on those named.
async #applyUserRequests (options) {
const timeEnd = time.start('idealTree:userRequests')
const tree = this.idealTree.target
if (!this.options.workspaces.length) {
await this.#applyUserRequestsToNode(tree, options)
} else {
const nodes = this.workspaceNodes(tree, this.options.workspaces)
if (this.options.includeWorkspaceRoot) {
nodes.push(tree)
}
const appliedRequests = nodes.map(
node => this.#applyUserRequestsToNode(node, options)
)
await Promise.all(appliedRequests)
}
timeEnd()
}
async #applyUserRequestsToNode (tree, options) {
// If we have a list of package names to update, and we know it's
// going to update them wherever they are, add any paths into those
// named nodes to the buildIdealTree queue.
if (!this.options.global && this[_updateNames].length) {
this.#queueNamedUpdates()
}
// global updates only update the globalTop nodes, but we need to know
// that they're there, and not reinstall the world unnecessarily.
const globalExplicitUpdateNames = []
if (this.options.global && (this[_updateAll] || this[_updateNames].length)) {
const nm = resolve(this.path, 'node_modules')
const paths = await readdirScoped(nm).catch(() => [])
for (const p of paths) {
const name = p.replace(/\\/g, '/')
tree.package.dependencies = tree.package.dependencies || {}
const updateName = this[_updateNames].includes(name)
if (this[_updateAll] || updateName) {
if (updateName) {
globalExplicitUpdateNames.push(name)
}
const dir = resolve(nm, name)
const st = await lstat(dir)
.catch(/* istanbul ignore next */ () => null)
if (st && st.isSymbolicLink()) {
const target = await readlink(dir)
const real = resolve(dirname(dir), target).replace(/#/g, '%23')
tree.package.dependencies[name] = `file:${real}`
} else {
tree.package.dependencies[name] = '*'
}
}
}
}
if (this.auditReport && this.auditReport.size > 0) {
await this.#queueVulnDependents(options)
}
const { add, rm } = options
if (rm && rm.length) {
addRmPkgDeps.rm(tree.package, rm)
for (const name of rm) {
this.#explicitRequests.add({ from: tree, name, action: 'DELETE' })
}
}
if (add && add.length) {
await this.#add(tree, options)
}
// triggers a refresh of all edgesOut. this has to be done BEFORE
// adding the edges to explicitRequests, because the package setter
// resets all edgesOut.
if (add && add.length || rm && rm.length || this.options.global) {
tree.package = tree.package
}
for (const spec of this[_resolvedAdd]) {
if (spec.tree === tree) {
this.#explicitRequests.add(tree.edgesOut.get(spec.name))
}
}
for (const name of globalExplicitUpdateNames) {
this.#explicitRequests.add(tree.edgesOut.get(name))
}
this.#depsQueue.push(tree)
}
// This returns a promise because we might not have the name yet, and need to
// call pacote.manifest to find the name.
async #add (tree, { add, saveType = null, saveBundle = false }) {
// If we have a link it will need to be added relative to the target's path
const path = tree.target.path
// get the name for each of the specs in the list.
// ie, doing `foo@bar` we just return foo but if it's a url or git, we
// don't know the name until we fetch it and look in its manifest.
await Promise.all(add.map(async rawSpec => {
// We do NOT provide the path to npa here, because user-additions need to
// be resolved relative to the tree being added to.
let spec = npa(rawSpec)
// if it's just @'' then we reload whatever's there, or get latest
// if it's an explicit tag, we need to install that specific tag version
const isTag = spec.rawSpec && spec.type === 'tag'
// look up the names of file/directory/git specs
if (!spec.name || isTag) {
const mani = await pacote.manifest(spec, { ...this.options })
if (isTag) {
// translate tag to a version
spec = npa(`${mani.name}@${mani.version}`)
}
spec.name = mani.name
}
const { name } = spec
if (spec.type === 'file') {
spec = npa(`file:${relpath(path, spec.fetchSpec).replace(/#/g, '%23')}`, path)
spec.name = name
} else if (spec.type === 'directory') {
try {
const real = await realpath(spec.fetchSpec, this[_rpcache], this[_stcache])
spec = npa(`file:${relpath(path, real).replace(/#/g, '%23')}`, path)
spec.name = name
} catch {
// TODO: create synthetic test case to simulate realpath failure
}
}
spec.tree = tree
this[_resolvedAdd].push(spec)
}))
// now this._resolvedAdd is a list of spec objects with names.
// find a home for each of them!
addRmPkgDeps.add({
pkg: tree.package,
add: this[_resolvedAdd],
saveBundle,
saveType,
})
}
// TODO: provide a way to fix bundled deps by exposing metadata about
// what's in the bundle at each published manifest. Without that, we
// can't possibly fix bundled deps without breaking a ton of other stuff,
// and leaving the user subject to getting it overwritten later anyway.
async #queueVulnDependents (options) {
for (const vuln of this.auditReport.values()) {
for (const node of vuln.nodes) {
const bundler = node.getBundler()
// XXX this belongs in the audit report itself, not here.
// We shouldn't even get these things here, and they shouldn't
// be printed by npm-audit-report as if they can be fixed, because
// they can't.
if (bundler) {
log.warn(`audit fix ${node.name}@${node.version}`,
`${node.location}\nis a bundled dependency of\n${
bundler.name}@${bundler.version} at ${bundler.location}\n` +
'It cannot be fixed automatically.\n' +
`Check for updates to the ${bundler.name} package.`)
continue
}
for (const edge of node.edgesIn) {
this.addTracker('idealTree', edge.from.name, edge.from.location)
this.#depsQueue.push(edge.from)
}
}
}
// note any that can't be fixed at the root level without --force
// if there's a fix, we use that. otherwise, the user has to remove it,
// find a different thing, fix the upstream, etc.
//
// XXX: how to handle top nodes that aren't the root? Maybe the report
// just tells the user to cd into that directory and fix it?
if (this.options.force && this.auditReport && this.auditReport.topVulns.size) {
options.add = options.add || []
options.rm = options.rm || []
const nodesTouched = new Set()
for (const [name, topVuln] of this.auditReport.topVulns.entries()) {
const {
simpleRange,
topNodes,
fixAvailable,
} = topVuln
for (const node of topNodes) {
if (!node.isProjectRoot && !node.isWorkspace) {
// not something we're going to fix, sorry. have to cd into
// that directory and fix it yourself.
log.warn('audit', 'Manual fix required in linked project ' +
`at ./${node.location} for ${name}@${simpleRange}.\n` +
`'cd ./${node.location}' and run 'npm audit' for details.`)
continue
}
if (!fixAvailable) {
log.warn('audit', `No fix available for ${name}@${simpleRange}`)
continue
}
// name may be different if parent fixes the dep
// see Vuln fixAvailable setter
const { isSemVerMajor, version, name: fixName } = fixAvailable
const breakingMessage = isSemVerMajor
? 'a SemVer major change'
: 'outside your stated dependency range'
log.warn('audit', `Updating ${fixName} to ${version}, ` +
`which is ${breakingMessage}.`)
await this.#add(node, { add: [`${fixName}@${version}`] })
nodesTouched.add(node)
}
}
for (const node of nodesTouched) {
node.package = node.package
}
}
}
#avoidRange (name) {
if (!this.auditReport) {
return null
}
const vuln = this.auditReport.get(name)
if (!vuln) {
return null
}
return vuln.range
}
#queueNamedUpdates () {
// ignore top nodes, since they are not loaded the same way, and
// probably have their own project associated with them.
// for every node with one of the names on the list, we add its
// dependents to the queue to be evaluated. in buildDepStep,
// anything on the update names list will get refreshed, even if
// it isn't a problem.
// XXX this could be faster by doing a series of inventory.query('name')
// calls rather than walking over everything in the tree.
for (const node of this.idealTree.inventory.values()) {
// XXX add any invalid edgesOut to the queue
if (this[_updateNames].includes(node.name) &&
!node.isTop && !node.inDepBundle && !node.inShrinkwrap) {
for (const edge of node.edgesIn) {
this.addTracker('idealTree', edge.from.name, edge.from.location)
this.#depsQueue.push(edge.from)
}
}
}
}
async #inflateAncientLockfile () {
const { meta, inventory } = this.idealTree
const ancient = meta.ancientLockfile
const old = meta.loadedFromDisk && !(meta.originalLockfileVersion >= 2)
if (inventory.size === 0 || !ancient && !old) {
return
}
// if the lockfile is from node v5 or earlier, then we'll have to reload
// all the manifests of everything we encounter. this is costly, but at
// least it's just a one-time hit.
const timeEnd = time.start('idealTree:inflate')
// don't warn if we're not gonna actually write it back anyway.
const heading = ancient ? 'ancient lockfile' : 'old lockfile'
if (ancient || !this.options.lockfileVersion ||
this.options.lockfileVersion >= defaultLockfileVersion) {
log.warn(heading,
`
The ${meta.type} file was created with an old version of npm,
so supplemental metadata must be fetched from the registry.
This is a one-time fix-up, please be patient...
`)
}
this.addTracker('idealTree:inflate')
const queue = []
for (const node of inventory.values()) {
if (node.isProjectRoot) {
continue
}
// if the node's location isn't within node_modules then this is actually
// a link target, so skip it. the link node itself will be queued later.
if (!node.location.startsWith('node_modules')) {
continue
}
queue.push(async () => {
log.silly('inflate', node.location)
const { resolved, version, path, name, location, integrity } = node
// don't try to hit the registry for linked deps
const useResolved = resolved && (
!version || resolved.startsWith('file:')
)
const id = useResolved ? resolved : version
const spec = npa.resolve(name, id, dirname(path))
const t = `idealTree:inflate:${location}`
this.addTracker(t)
try {
const mani = await pacote.manifest(spec, {
...this.options,
resolved: resolved,
integrity: integrity,
fullMetadata: false,
})
node.package = { ...mani, _id: `${mani.name}@${mani.version}` }
} catch (er) {
const warning = `Could not fetch metadata for ${name}@${id}`
log.warn(heading, warning, er)
}
this.finishTracker(t)
})
}
await promiseCallLimit(queue)
// have to re-calc dep flags, because the nodes don't have edges
// until their packages get assigned, so everything looks extraneous
calcDepFlags(this.idealTree)
// yes, yes, this isn't the "original" version, but now that it's been
// upgraded, we need to make sure we don't do the work to upgrade it
// again, since it's now as new as can be.
if (!this.options.lockfileVersion && !meta.hiddenLockfile) {
meta.originalLockfileVersion = defaultLockfileVersion
}
this.finishTracker('idealTree:inflate')
timeEnd()
}
// at this point we have a virtual tree with the actual root node's
// package deps, which may be partly or entirely incomplete, invalid
// or extraneous.
#buildDeps () {
const timeEnd = time.start('idealTree:buildDeps')
const tree = this.idealTree.target
tree.assertRootOverrides()
this.#depsQueue.push(tree)
// XXX also push anything that depends on a node with a name
// in the override list
log.silly('idealTree', 'buildDeps')
this.addTracker('idealTree', tree.name, '')
return this.#buildDepStep().then(timeEnd)
}
async #buildDepStep () {
// removes tracker of previous dependency in the queue
if (this.#currentDep) {
const { location, name } = this.#currentDep
time.end(`idealTree:${location || '#root'}`)
this.finishTracker('idealTree', name, location)
this.#currentDep = null
}
if (!this.#depsQueue.length) {
return this.#resolveLinks()
}
const node = this.#depsQueue.pop()
const bd = node.package.bundleDependencies
const hasBundle = bd && Array.isArray(bd) && bd.length
const { hasShrinkwrap } = node
// if the node was already visited, or has since been removed from the
// tree, skip over it and process the rest of the queue. If a node has
// a shrinkwrap, also skip it, because it's going to get its deps
// satisfied by whatever's in that file anyway.
if (this.#depsSeen.has(node) ||
node.root !== this.idealTree ||
hasShrinkwrap && !this.#complete) {
return this.#buildDepStep()
}
this.#depsSeen.add(node)
this.#currentDep = node
time.start(`idealTree:${node.location || '#root'}`)
// if we're loading a _complete_ ideal tree, for a --package-lock-only
// installation for example, we have to crack open the tarball and
// look inside if it has bundle deps or shrinkwraps. note that this is
// not necessary during a reification, because we just update the
// ideal tree by reading bundles/shrinkwraps in place.
// Don't bother if the node is from the actual tree and hasn't
// been resolved, because we can't fetch it anyway, could be anything!
const crackOpen = this.#complete &&
node !== this.idealTree &&
node.resolved &&
(hasBundle || hasShrinkwrap)
if (crackOpen) {
const Arborist = this.constructor
const opt = { ...this.options }
await cacache.tmp.withTmp(this.cache, opt, async path => {
await pacote.extract(node.resolved, path, {
...opt,
Arborist,
resolved: node.resolved,
integrity: node.integrity,
})
if (hasShrinkwrap) {
await new Arborist({ ...this.options, path })
.loadVirtual({ root: node })
}
if (hasBundle) {
await new Arborist({ ...this.options, path })
.loadActual({ root: node, ignoreMissing: true })
}
})
}
// if any deps are missing or invalid, then we fetch the manifest for
// the thing we want, and build a new dep node from that.
// Then, find the ideal placement for that node. The ideal placement
// searches from the node's deps (or parent deps in the case of non-root
// peer deps), and walks up the tree until it finds the highest spot
// where it doesn't cause any conflicts.
//
// A conflict can be:
// - A node by that name already exists at that location.
// - The parent has a peer dep on that name
// - One of the node's peer deps conflicts at that location, unless the
// peer dep is met by a node at that location, which is fine.
//
// If we create a new node, then build its ideal deps as well.
//
// Note: this is the same "maximally naive" deduping tree-building
// algorithm that npm has used since v3. In a case like this:
//
// root -> (a@1, b@1||2)
// a -> (b@1)
//
// You'll end up with a tree like this:
//
// root
// +-- a@1
// | +-- b@1
// +-- b@2
//
// rather than this, more deduped, but just as correct tree:
//
// root
// +-- a@1
// +-- b@1
//
// Another way to look at it is that this algorithm favors getting higher
// version deps at higher levels in the tree, even if that reduces
// potential deduplication.
//
// Set `preferDedupe: true` in the options to replace the shallower
// dep if allowed.
const tasks = []
const peerSource = this.#peerSetSource.get(node) || node
for (const edge of this.#problemEdges(node)) {
if (edge.peerConflicted) {
continue
}
// peerSetSource is only relevant when we have a peerEntryEdge
// otherwise we're setting regular non-peer deps as if they have
// a virtual root of whatever brought in THIS node.
// so we VR the node itself if the edge is not a peer
const source = edge.peer ? peerSource : node
const virtualRoot = this.#virtualRoot(source, true)
// reuse virtual root if we already have one, but don't
// try to do the override ahead of time, since we MAY be able
// to create a more correct tree than the virtual root could.
const vrEdge = virtualRoot && virtualRoot.edgesOut.get(edge.name)
const vrDep = vrEdge && vrEdge.valid && vrEdge.to
// only re-use the virtualRoot if it's a peer edge we're placing.
// otherwise, we end up in situations where we override peer deps that
// we could have otherwise found homes for. Eg:
// xy -> (x, y)
// x -> PEER(z@1)
// y -> PEER(z@2)
// If xy is a dependency, we can resolve this like:
// project
// +-- xy
// | +-- y
// | +-- z@2
// +-- x
// +-- z@1
// But if x and y are loaded in the same virtual root, then they will
// be forced to agree on a version of z.
const required = new Set([edge.from])
const parent = edge.peer ? virtualRoot : null
const dep = vrDep && vrDep.satisfies(edge) ? vrDep
: await this.#nodeFromEdge(edge, parent, null, required)
/* istanbul ignore next */
debug(() => {
if (!dep) {
throw new Error('no dep??')
}
})
tasks.push({ edge, dep })
}
const placeDeps = tasks.sort((a, b) => localeCompare(a.edge.name, b.edge.name))
const promises = []
for (const { edge, dep } of placeDeps) {
const pd = new PlaceDep({
edge,
dep,
auditReport: this.auditReport,
explicitRequest: this.#explicitRequests.has(edge),
force: this.options.force,
installLinks: this.installLinks,
installStrategy: this.#installStrategy,
legacyPeerDeps: this.legacyPeerDeps,
preferDedupe: this.#preferDedupe,
strictPeerDeps: this.#strictPeerDeps,
updateNames: this[_updateNames],
})
// placing a dep is actually a tree of placing the dep itself
// and all of its peer group that aren't already met by the tree
depth({
tree: pd,
getChildren: pd => pd.children,
visit: pd => {
const { placed, edge, canPlace: cpd } = pd
// if we didn't place anything, nothing to do here
if (!placed) {
return
}
// we placed something, that means we changed the tree
if (placed.errors.length) {
this.#loadFailures.add(placed)
}
this.#mutateTree = true
if (cpd.canPlaceSelf === OK) {
for (const edgeIn of placed.edgesIn) {
if (edgeIn === edge) {
continue
}
const { from, valid, peerConflicted } = edgeIn
if (!peerConflicted && !valid && !this.#depsSeen.has(from)) {
this.addTracker('idealTree', from.name, from.location)
this.#depsQueue.push(edgeIn.from)
}
}
} else {
/* istanbul ignore else - should be only OK or REPLACE here */
if (cpd.canPlaceSelf === REPLACE) {
// this may also create some invalid edges, for example if we're
// intentionally causing something to get nested which was
// previously placed in this location.
for (const edgeIn of placed.edgesIn) {
if (edgeIn === edge) {
continue
}
const { valid, peerConflicted } = edgeIn
if (!valid && !peerConflicted) {
// if it's already been visited, we have to re-visit
// otherwise, just enqueue normally.
this.#depsSeen.delete(edgeIn.from)
this.#depsQueue.push(edgeIn.from)
}
}
}
}
/* istanbul ignore if - should be impossible */
if (cpd.canPlaceSelf === CONFLICT) {
debug(() => {
const er = new Error('placed with canPlaceSelf=CONFLICT')
throw Object.assign(er, { placeDep: pd })
})
return
}
// lastly, also check for the missing deps of the node we placed,
// and any holes created by pruning out conflicted peer sets.
this.#depsQueue.push(placed)
for (const dep of pd.needEvaluation) {
this.#depsSeen.delete(dep)
this.#depsQueue.push(dep)
}