/
index.ts
1173 lines (1101 loc) · 41.7 KB
/
index.ts
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
import crypto from 'crypto'
import path from 'path'
import buildModules, { DepsStateCache, linkBinsOfDependencies } from '@pnpm/build-modules'
import {
LAYOUT_VERSION,
LOCKFILE_VERSION,
WANTED_LOCKFILE,
} from '@pnpm/constants'
import {
stageLogger,
summaryLogger,
} from '@pnpm/core-loggers'
import { createBase32HashFromFile } from '@pnpm/crypto.base32-hash'
import PnpmError from '@pnpm/error'
import getContext, { PnpmContext, ProjectOptions } from '@pnpm/get-context'
import headless, { Project } from '@pnpm/headless'
import runLifecycleHook, {
makeNodeRequireOption,
runLifecycleHooksConcurrently,
RunLifecycleHooksConcurrentlyOptions,
} from '@pnpm/lifecycle'
import linkBins, { linkBinsOfPackages } from '@pnpm/link-bins'
import {
ProjectSnapshot,
Lockfile,
writeCurrentLockfile,
writeLockfiles,
writeWantedLockfile,
cleanGitBranchLockfiles,
PatchFile,
} from '@pnpm/lockfile-file'
import { writePnpFile } from '@pnpm/lockfile-to-pnp'
import { extendProjectsWithTargetDirs } from '@pnpm/lockfile-utils'
import logger, { globalInfo, streamParser } from '@pnpm/logger'
import { getAllDependenciesFromManifest } from '@pnpm/manifest-utils'
import { write as writeModulesYaml } from '@pnpm/modules-yaml'
import readModulesDirs from '@pnpm/read-modules-dir'
import { safeReadProjectManifestOnly } from '@pnpm/read-project-manifest'
import { removeBin } from '@pnpm/remove-bins'
import resolveDependencies, {
getWantedDependencies,
DependenciesGraph,
DependenciesGraphNode,
PinnedVersion,
WantedDependency,
} from '@pnpm/resolve-dependencies'
import {
PreferredVersions,
} from '@pnpm/resolver-base'
import {
DependenciesField,
DependencyManifest,
PeerDependencyIssues,
ProjectManifest,
ReadPackageHook,
} from '@pnpm/types'
import rimraf from '@zkochan/rimraf'
import isInnerLink from 'is-inner-link'
import pFilter from 'p-filter'
import pLimit from 'p-limit'
import flatten from 'ramda/src/flatten'
import fromPairs from 'ramda/src/fromPairs'
import equals from 'ramda/src/equals'
import isEmpty from 'ramda/src/isEmpty'
import pipeWith from 'ramda/src/pipeWith'
import props from 'ramda/src/props'
import unnest from 'ramda/src/unnest'
import parseWantedDependencies from '../parseWantedDependencies'
import removeDeps from '../uninstall/removeDeps'
import allProjectsAreUpToDate from './allProjectsAreUpToDate'
import extendOptions, {
InstallOptions,
ProcessedInstallOptions as StrictInstallOptions,
} from './extendInstallOptions'
import { getPreferredVersionsFromLockfile, getAllUniqueSpecs } from './getPreferredVersions'
import linkPackages from './link'
import reportPeerDependencyIssues from './reportPeerDependencyIssues'
const BROKEN_LOCKFILE_INTEGRITY_ERRORS = new Set([
'ERR_PNPM_UNEXPECTED_PKG_CONTENT_IN_STORE',
'ERR_PNPM_TARBALL_INTEGRITY',
])
const DEV_PREINSTALL = 'pnpm:devPreinstall'
export type DependenciesMutation = (
{
buildIndex: number
mutation: 'install'
pruneDirectDependencies?: boolean
} | {
allowNew?: boolean
dependencySelectors: string[]
mutation: 'installSome'
peer?: boolean
pruneDirectDependencies?: boolean
pinnedVersion?: PinnedVersion
targetDependenciesField?: DependenciesField
} | {
mutation: 'uninstallSome'
dependencyNames: string[]
targetDependenciesField?: DependenciesField
} | {
mutation: 'unlink'
} | {
mutation: 'unlinkSome'
dependencyNames: string[]
}
) & (
{
manifest: ProjectManifest
}
)
export async function install (
manifest: ProjectManifest,
opts: InstallOptions & {
preferredVersions?: PreferredVersions
pruneDirectDependencies?: boolean
}
) {
const projects = await mutateModules(
[
{
buildIndex: 0,
manifest,
mutation: 'install',
pruneDirectDependencies: opts.pruneDirectDependencies,
rootDir: opts.dir ?? process.cwd(),
},
],
opts
)
return projects[0].manifest
}
interface ProjectToBeInstalled {
id: string
buildIndex: number
manifest: ProjectManifest
modulesDir: string
rootDir: string
}
export type MutatedProject = ProjectOptions & DependenciesMutation
export type MutateModulesOptions = InstallOptions & {
preferredVersions?: PreferredVersions
hooks?: {
readPackage?: ReadPackageHook[] | ReadPackageHook
} | InstallOptions['hooks']
}
export async function mutateModules (
projects: MutatedProject[],
maybeOpts: MutateModulesOptions
): Promise<UpdatedProject[]> {
const reporter = maybeOpts?.reporter
if ((reporter != null) && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
const opts = await extendOptions(maybeOpts)
if (!opts.include.dependencies && opts.include.optionalDependencies) {
throw new PnpmError('OPTIONAL_DEPS_REQUIRE_PROD_DEPS', 'Optional dependencies cannot be installed without production dependencies')
}
const installsOnly = projects.every((project) => project.mutation === 'install')
if (!installsOnly) opts.strictPeerDependencies = false
opts['forceNewModules'] = installsOnly
const rootProjectManifest = projects.find(({ rootDir }) => rootDir === opts.lockfileDir)?.manifest ??
// When running install/update on a subset of projects, the root project might not be included,
// so reading its manifest explicitly here.
await safeReadProjectManifestOnly(opts.lockfileDir)
const ctx = await getContext(projects, opts)
if (opts.hooks.preResolution) {
await opts.hooks.preResolution({
currentLockfile: ctx.currentLockfile,
wantedLockfile: ctx.wantedLockfile,
existsCurrentLockfile: ctx.existsCurrentLockfile,
existsWantedLockfile: ctx.existsWantedLockfile,
lockfileDir: ctx.lockfileDir,
storeDir: ctx.storeDir,
})
}
const pruneVirtualStore = ctx.modulesFile?.prunedAt && opts.modulesCacheMaxAge > 0
? cacheExpired(ctx.modulesFile.prunedAt, opts.modulesCacheMaxAge)
: true
if (!maybeOpts.ignorePackageManifest) {
for (const { manifest, rootDir } of ctx.projects) {
if (!manifest) {
throw new Error(`No package.json found in "${rootDir}"`)
}
}
}
const result = await _install()
if (global['verifiedFileIntegrity'] > 1000) {
globalInfo(`The integrity of ${global['verifiedFileIntegrity']} files was checked. This might have caused installation to take longer.`) // eslint-disable-line
}
if ((reporter != null) && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}
if (opts.mergeGitBranchLockfiles) {
await cleanGitBranchLockfiles(ctx.lockfileDir)
}
return result
async function _install (): Promise<UpdatedProject[]> {
const scriptsOpts: RunLifecycleHooksConcurrentlyOptions = {
extraBinPaths: opts.extraBinPaths,
extraEnv: opts.extraEnv,
rawConfig: opts.rawConfig,
scriptsPrependNodePath: opts.scriptsPrependNodePath,
scriptShell: opts.scriptShell,
shellEmulator: opts.shellEmulator,
stdio: opts.ownLifecycleHooksStdio,
storeController: opts.storeController,
unsafePerm: opts.unsafePerm || false,
}
if (!opts.ignoreScripts && !opts.ignorePackageManifest && rootProjectManifest?.scripts?.[DEV_PREINSTALL]) {
await runLifecycleHook(
DEV_PREINSTALL,
rootProjectManifest,
{
...scriptsOpts,
depPath: opts.lockfileDir,
pkgRoot: opts.lockfileDir,
rootModulesDir: ctx.rootModulesDir,
}
)
}
const packageExtensionsChecksum = isEmpty(opts.packageExtensions ?? {}) ? undefined : createObjectChecksum(opts.packageExtensions!)
const patchedDependencies = opts.ignorePackageManifest
? ctx.wantedLockfile.patchedDependencies
: (opts.patchedDependencies ? await calcPatchHashes(opts.patchedDependencies, opts.lockfileDir) : {})
const patchedDependenciesWithResolvedPath = patchedDependencies
? fromPairs(Object.entries(patchedDependencies).map(([key, patchFile]) => [key, {
hash: patchFile.hash,
path: path.join(opts.lockfileDir, patchFile.path),
}]))
: undefined
let needsFullResolution = !maybeOpts.ignorePackageManifest &&
lockfileIsUpToDate(ctx.wantedLockfile, {
overrides: opts.overrides,
neverBuiltDependencies: opts.neverBuiltDependencies,
onlyBuiltDependencies: opts.onlyBuiltDependencies,
packageExtensionsChecksum,
patchedDependencies,
}) ||
opts.fixLockfile
if (needsFullResolution) {
ctx.wantedLockfile.overrides = opts.overrides
ctx.wantedLockfile.neverBuiltDependencies = opts.neverBuiltDependencies
ctx.wantedLockfile.onlyBuiltDependencies = opts.onlyBuiltDependencies
ctx.wantedLockfile.packageExtensionsChecksum = packageExtensionsChecksum
ctx.wantedLockfile.patchedDependencies = patchedDependencies
}
const frozenLockfile = opts.frozenLockfile ||
opts.frozenLockfileIfExists && ctx.existsWantedLockfile
if (
!ctx.lockfileHadConflicts &&
!opts.update &&
!opts.fixLockfile &&
installsOnly &&
(
frozenLockfile && !opts.lockfileOnly ||
opts.ignorePackageManifest ||
!needsFullResolution &&
opts.preferFrozenLockfile &&
(!opts.pruneLockfileImporters || Object.keys(ctx.wantedLockfile.importers).length === ctx.projects.length) &&
ctx.existsWantedLockfile &&
ctx.wantedLockfile.lockfileVersion === LOCKFILE_VERSION &&
await allProjectsAreUpToDate(ctx.projects, {
autoInstallPeers: opts.autoInstallPeers,
linkWorkspacePackages: opts.linkWorkspacePackagesDepth >= 0,
wantedLockfile: ctx.wantedLockfile,
workspacePackages: opts.workspacePackages,
})
)
) {
if (needsFullResolution) {
throw new PnpmError('FROZEN_LOCKFILE_WITH_OUTDATED_LOCKFILE', 'Cannot perform a frozen installation because the lockfile needs updates', {
hint: 'Note that in CI environments this setting is true by default. If you still need to run install in such cases, use "pnpm install --no-frozen-lockfile"',
})
}
if (opts.lockfileOnly) {
return projects
}
if (!ctx.existsWantedLockfile) {
if (ctx.projects.some((project) => pkgHasDependencies(project.manifest))) {
throw new Error(`Headless installation requires a ${WANTED_LOCKFILE} file`)
}
} else {
if (maybeOpts.ignorePackageManifest) {
logger.info({ message: 'Importing packages to virtual store', prefix: opts.lockfileDir })
} else {
logger.info({ message: 'Lockfile is up to date, resolution step is skipped', prefix: opts.lockfileDir })
}
try {
await headless({
...ctx,
...opts,
currentEngine: {
nodeVersion: opts.nodeVersion,
pnpmVersion: opts.packageManager.name === 'pnpm' ? opts.packageManager.version : '',
},
patchedDependencies: patchedDependenciesWithResolvedPath,
projects: ctx.projects as Project[],
prunedAt: ctx.modulesFile?.prunedAt,
pruneVirtualStore,
wantedLockfile: maybeOpts.ignorePackageManifest ? undefined : ctx.wantedLockfile,
})
if (opts.useLockfile && opts.saveLockfile && opts.mergeGitBranchLockfiles) {
await writeLockfiles({
currentLockfile: ctx.currentLockfile,
currentLockfileDir: ctx.virtualStoreDir,
wantedLockfile: ctx.wantedLockfile,
wantedLockfileDir: ctx.lockfileDir,
forceSharedFormat: opts.forceSharedLockfile,
useInlineSpecifiersFormat: opts.useInlineSpecifiersLockfileFormat,
useGitBranchLockfile: opts.useGitBranchLockfile,
mergeGitBranchLockfiles: opts.mergeGitBranchLockfiles,
})
}
return projects
} catch (error: any) { // eslint-disable-line
if (
frozenLockfile ||
error.code !== 'ERR_PNPM_LOCKFILE_MISSING_DEPENDENCY' && !BROKEN_LOCKFILE_INTEGRITY_ERRORS.has(error.code)
) throw error
if (BROKEN_LOCKFILE_INTEGRITY_ERRORS.has(error.code)) {
needsFullResolution = true
// Ideally, we would not update but currently there is no other way to redownload the integrity of the package
opts.update = true
}
// A broken lockfile may be caused by a badly resolved Git conflict
logger.warn({
error,
message: error.message,
prefix: ctx.lockfileDir,
})
logger.error(new PnpmError(error.code, 'The lockfile is broken! Resolution step will be performed to fix it.'))
}
}
}
const projectsToInstall = [] as ImporterToUpdate[]
let preferredSpecs: Record<string, string> | null = null
// TODO: make it concurrent
for (const project of ctx.projects) {
switch (project.mutation) {
case 'uninstallSome':
projectsToInstall.push({
pruneDirectDependencies: false,
...project,
removePackages: project.dependencyNames,
updatePackageManifest: true,
wantedDependencies: [],
})
break
case 'install': {
await installCase({
...project,
updatePackageManifest: opts.updatePackageManifest ?? opts.update,
})
break
}
case 'installSome': {
await installSome({
...project,
updatePackageManifest: opts.updatePackageManifest !== false,
})
break
}
case 'unlink': {
const packageDirs = await readModulesDirs(project.modulesDir)
const externalPackages = await pFilter(
packageDirs!,
async (packageDir: string) => isExternalLink(ctx.storeDir, project.modulesDir, packageDir)
)
const allDeps = getAllDependenciesFromManifest(project.manifest)
const packagesToInstall: string[] = []
for (const pkgName of externalPackages) {
await rimraf(path.join(project.modulesDir, pkgName))
if (allDeps[pkgName]) {
packagesToInstall.push(pkgName)
}
}
if (packagesToInstall.length === 0) return projects
// TODO: install only those that were unlinked
// but don't update their version specs in package.json
await installCase({ ...project, mutation: 'install' })
break
}
case 'unlinkSome': {
if (project.manifest?.name && opts.globalBin) {
await removeBin(path.join(opts.globalBin, project.manifest?.name))
}
const packagesToInstall: string[] = []
const allDeps = getAllDependenciesFromManifest(project.manifest)
for (const depName of project.dependencyNames) {
try {
if (!await isExternalLink(ctx.storeDir, project.modulesDir, depName)) {
logger.warn({
message: `${depName} is not an external link`,
prefix: project.rootDir,
})
continue
}
} catch (err: any) { // eslint-disable-line
if (err['code'] !== 'ENOENT') throw err
}
await rimraf(path.join(project.modulesDir, depName))
if (allDeps[depName]) {
packagesToInstall.push(depName)
}
}
if (packagesToInstall.length === 0) return projects
// TODO: install only those that were unlinked
// but don't update their version specs in package.json
await installSome({
...project,
dependencySelectors: packagesToInstall,
mutation: 'installSome',
updatePackageManifest: false,
})
break
}
}
}
async function installCase (project: any) { // eslint-disable-line
const wantedDependencies = getWantedDependencies(project.manifest, {
autoInstallPeers: opts.autoInstallPeers,
includeDirect: opts.includeDirect,
updateWorkspaceDependencies: opts.update,
nodeExecPath: opts.nodeExecPath,
})
.map((wantedDependency) => ({ ...wantedDependency, updateSpec: true }))
if (ctx.wantedLockfile?.importers) {
forgetResolutionsOfPrevWantedDeps(ctx.wantedLockfile.importers[project.id], wantedDependencies)
}
if (opts.ignoreScripts && project.manifest?.scripts &&
(project.manifest.scripts.preinstall ||
project.manifest.scripts.install ||
project.manifest.scripts.postinstall ||
project.manifest.scripts.prepare)
) {
ctx.pendingBuilds.push(project.id)
}
projectsToInstall.push({
pruneDirectDependencies: false,
...project,
wantedDependencies,
})
}
async function installSome (project: any) { // eslint-disable-line
const currentPrefs = opts.ignoreCurrentPrefs ? {} : getAllDependenciesFromManifest(project.manifest)
const optionalDependencies = project.targetDependenciesField ? {} : project.manifest.optionalDependencies || {}
const devDependencies = project.targetDependenciesField ? {} : project.manifest.devDependencies || {}
if (preferredSpecs == null) {
preferredSpecs = getAllUniqueSpecs(flatten(Object.values(opts.workspacePackages).map(obj => Object.values(obj))).map(({ manifest }) => manifest))
}
const wantedDeps = parseWantedDependencies(project.dependencySelectors, {
allowNew: project.allowNew !== false,
currentPrefs,
defaultTag: opts.tag,
dev: project.targetDependenciesField === 'devDependencies',
devDependencies,
optional: project.targetDependenciesField === 'optionalDependencies',
optionalDependencies,
updateWorkspaceDependencies: opts.update,
preferredSpecs,
overrides: opts.overrides,
})
projectsToInstall.push({
pruneDirectDependencies: false,
...project,
wantedDependencies: wantedDeps.map(wantedDep => ({ ...wantedDep, isNew: true, updateSpec: true, nodeExecPath: opts.nodeExecPath })),
})
}
// Unfortunately, the private lockfile may differ from the public one.
// A user might run named installations on a project that has a pnpm-lock.yaml file before running a noop install
const makePartialCurrentLockfile = !installsOnly && (
ctx.existsWantedLockfile && !ctx.existsCurrentLockfile ||
!ctx.currentLockfileIsUpToDate
)
const result = await installInContext(projectsToInstall, ctx, {
...opts,
currentLockfileIsUpToDate: !ctx.existsWantedLockfile || ctx.currentLockfileIsUpToDate,
makePartialCurrentLockfile,
needsFullResolution,
pruneVirtualStore,
scriptsOpts,
updateLockfileMinorVersion: true,
patchedDependencies: patchedDependenciesWithResolvedPath,
})
return result.projects
}
}
async function calcPatchHashes (patches: Record<string, string>, lockfileDir: string) {
return fromPairs(await Promise.all(
Object.entries(patches).map(async ([key, patchFileRelativePath]) => {
const patchFilePath = path.join(lockfileDir, patchFileRelativePath)
return [
key,
{
hash: await createBase32HashFromFile(patchFilePath),
path: patchFileRelativePath,
},
]
})
))
}
function lockfileIsUpToDate (
lockfile: Lockfile,
{
neverBuiltDependencies,
onlyBuiltDependencies,
overrides,
packageExtensionsChecksum,
patchedDependencies,
}: {
neverBuiltDependencies?: string[]
onlyBuiltDependencies?: string[]
overrides?: Record<string, string>
packageExtensionsChecksum?: string
patchedDependencies?: Record<string, PatchFile>
}) {
return !equals(lockfile.overrides ?? {}, overrides ?? {}) ||
!equals((lockfile.neverBuiltDependencies ?? []).sort(), (neverBuiltDependencies ?? []).sort()) ||
!equals(onlyBuiltDependencies?.sort(), lockfile.onlyBuiltDependencies) ||
lockfile.packageExtensionsChecksum !== packageExtensionsChecksum ||
!equals(lockfile.patchedDependencies ?? {}, patchedDependencies ?? {})
}
export function createObjectChecksum (obj: Object) {
const s = JSON.stringify(obj)
return crypto.createHash('md5').update(s).digest('hex')
}
function cacheExpired (prunedAt: string, maxAgeInMinutes: number) {
return ((Date.now() - new Date(prunedAt).valueOf()) / (1000 * 60)) > maxAgeInMinutes
}
async function isExternalLink (storeDir: string, modules: string, pkgName: string) {
const link = await isInnerLink(modules, pkgName)
return !link.isInner
}
function pkgHasDependencies (manifest: ProjectManifest) {
return Boolean(
(Object.keys(manifest.dependencies ?? {}).length > 0) ||
Object.keys(manifest.devDependencies ?? {}).length ||
Object.keys(manifest.optionalDependencies ?? {}).length
)
}
// If the specifier is new, the old resolution probably does not satisfy it anymore.
// By removing these resolutions we ensure that they are resolved again using the new specs.
function forgetResolutionsOfPrevWantedDeps (importer: ProjectSnapshot, wantedDeps: WantedDependency[]) {
if (!importer.specifiers) return
importer.dependencies = importer.dependencies ?? {}
importer.devDependencies = importer.devDependencies ?? {}
importer.optionalDependencies = importer.optionalDependencies ?? {}
for (const { alias, pref } of wantedDeps) {
if (alias && importer.specifiers[alias] !== pref) {
if (!importer.dependencies[alias]?.startsWith('link:')) {
delete importer.dependencies[alias]
}
delete importer.devDependencies[alias]
delete importer.optionalDependencies[alias]
}
}
}
export async function addDependenciesToPackage (
manifest: ProjectManifest,
dependencySelectors: string[],
opts: InstallOptions & {
bin?: string
allowNew?: boolean
peer?: boolean
pinnedVersion?: 'major' | 'minor' | 'patch'
targetDependenciesField?: DependenciesField
}
) {
const projects = await mutateModules(
[
{
binsDir: opts.bin,
allowNew: opts.allowNew,
dependencySelectors,
manifest,
mutation: 'installSome',
peer: opts.peer,
pinnedVersion: opts.pinnedVersion,
rootDir: opts.dir ?? process.cwd(),
targetDependenciesField: opts.targetDependenciesField,
},
],
{
...opts,
lockfileDir: opts.lockfileDir ?? opts.dir,
})
return projects[0].manifest
}
export type ImporterToUpdate = {
binsDir: string
id: string
manifest: ProjectManifest
originalManifest?: ProjectManifest
modulesDir: string
rootDir: string
pruneDirectDependencies: boolean
removePackages?: string[]
updatePackageManifest: boolean
wantedDependencies: Array<WantedDependency & { isNew?: boolean, updateSpec?: boolean }>
} & DependenciesMutation
export interface UpdatedProject {
manifest: ProjectManifest
peerDependencyIssues?: PeerDependencyIssues
rootDir: string
}
interface InstallFunctionResult {
newLockfile: Lockfile
projects: UpdatedProject[]
}
type InstallFunction = (
projects: ImporterToUpdate[],
ctx: PnpmContext<DependenciesMutation>,
opts: Omit<StrictInstallOptions, 'patchedDependencies'> & {
patchedDependencies?: Record<string, PatchFile>
makePartialCurrentLockfile: boolean
needsFullResolution: boolean
neverBuiltDependencies?: string[]
onlyBuiltDependencies?: string[]
overrides?: Record<string, string>
updateLockfileMinorVersion: boolean
preferredVersions?: PreferredVersions
pruneVirtualStore: boolean
scriptsOpts: RunLifecycleHooksConcurrentlyOptions
currentLockfileIsUpToDate: boolean
}
) => Promise<InstallFunctionResult>
const _installInContext: InstallFunction = async (projects, ctx, opts) => {
if (opts.lockfileOnly && ctx.existsCurrentLockfile) {
logger.warn({
message: '`node_modules` is present. Lockfile only installation will make it out-of-date',
prefix: ctx.lockfileDir,
})
}
ctx.wantedLockfile.importers = ctx.wantedLockfile.importers || {}
for (const { id } of projects) {
if (!ctx.wantedLockfile.importers[id]) {
ctx.wantedLockfile.importers[id] = { specifiers: {} }
}
}
if (opts.pruneLockfileImporters) {
const projectIds = new Set(projects.map(({ id }) => id))
for (const wantedImporter of Object.keys(ctx.wantedLockfile.importers)) {
if (!projectIds.has(wantedImporter)) {
delete ctx.wantedLockfile.importers[wantedImporter]
}
}
}
await Promise.all(
projects
.map(async (project) => {
if (project.mutation !== 'uninstallSome') return
const _removeDeps = async (manifest: ProjectManifest) => removeDeps(manifest, project.dependencyNames, { prefix: project.rootDir, saveType: project.targetDependenciesField })
project.manifest = await _removeDeps(project.manifest)
if (project.originalManifest != null) {
project.originalManifest = await _removeDeps(project.originalManifest)
}
})
)
stageLogger.debug({
prefix: ctx.lockfileDir,
stage: 'resolution_started',
})
const preferredVersions = opts.preferredVersions ?? (
(
!opts.update &&
(ctx.wantedLockfile.packages != null) &&
!isEmpty(ctx.wantedLockfile.packages)
)
? getPreferredVersionsFromLockfile(ctx.wantedLockfile.packages)
: undefined
)
const forceFullResolution = ctx.wantedLockfile.lockfileVersion !== LOCKFILE_VERSION ||
!opts.currentLockfileIsUpToDate ||
opts.force ||
opts.needsFullResolution ||
ctx.lockfileHadConflicts
// Ignore some fields when fixing lockfile, so these fields can be regenerated
// and make sure it's up to date
if (
opts.fixLockfile &&
(ctx.wantedLockfile.packages != null) &&
!isEmpty(ctx.wantedLockfile.packages)
) {
ctx.wantedLockfile.packages = Object.entries(ctx.wantedLockfile.packages).reduce((pre, [depPath, snapshot]) => ({
...pre,
[depPath]: {
// These fields are needed to avoid losing information of the locked dependencies if these fields are not broken
// If these fields are broken, they will also be regenerated
dependencies: snapshot.dependencies,
optionalDependencies: snapshot.optionalDependencies,
resolution: snapshot.resolution,
},
}), {})
}
let {
dependenciesGraph,
dependenciesByProjectId,
finishLockfileUpdates,
linkedDependenciesByProjectId,
newLockfile,
outdatedDependencies,
peerDependencyIssuesByProjects,
wantedToBeSkippedPackageIds,
waitTillAllFetchingsFinish,
} = await resolveDependencies(
projects,
{
allowBuild: createAllowBuildFunction(opts),
allowedDeprecatedVersions: opts.allowedDeprecatedVersions,
allowNonAppliedPatches: opts.allowNonAppliedPatches,
autoInstallPeers: opts.autoInstallPeers,
currentLockfile: ctx.currentLockfile,
defaultUpdateDepth: (opts.update || (opts.updateMatching != null)) ? opts.depth : -1,
dryRun: opts.lockfileOnly,
engineStrict: opts.engineStrict,
force: opts.force,
forceFullResolution,
hooks: {
readPackage: opts.readPackageHook,
},
linkWorkspacePackagesDepth: opts.linkWorkspacePackagesDepth ?? (opts.saveWorkspaceProtocol ? 0 : -1),
lockfileDir: opts.lockfileDir,
nodeVersion: opts.nodeVersion,
pnpmVersion: opts.packageManager.name === 'pnpm' ? opts.packageManager.version : '',
preferWorkspacePackages: opts.preferWorkspacePackages,
preferredVersions,
preserveWorkspaceProtocol: opts.preserveWorkspaceProtocol,
registries: ctx.registries,
resolutionMode: opts.resolutionMode,
saveWorkspaceProtocol: opts.saveWorkspaceProtocol,
storeController: opts.storeController,
tag: opts.tag,
updateMatching: opts.updateMatching,
virtualStoreDir: ctx.virtualStoreDir,
wantedLockfile: ctx.wantedLockfile,
workspacePackages: opts.workspacePackages,
patchedDependencies: opts.patchedDependencies,
lockfileIncludeTarballUrl: opts.lockfileIncludeTarballUrl,
}
)
if (!opts.include.optionalDependencies || !opts.include.devDependencies || !opts.include.dependencies) {
for (const projectId of Object.keys(linkedDependenciesByProjectId ?? {})) {
linkedDependenciesByProjectId[projectId] = linkedDependenciesByProjectId[projectId].filter((linkedDep) =>
!(
linkedDep.dev && !opts.include.devDependencies ||
linkedDep.optional && !opts.include.optionalDependencies ||
!linkedDep.dev && !linkedDep.optional && !opts.include.dependencies
)
)
}
for (const { id, manifest } of projects) {
dependenciesByProjectId[id] = fromPairs(
Object.entries(dependenciesByProjectId[id])
.filter(([, depPath]) => {
const dep = dependenciesGraph[depPath]
if (!dep) return false
const isDev = Boolean(manifest.devDependencies?.[dep.name])
const isOptional = Boolean(manifest.optionalDependencies?.[dep.name])
return !(
isDev && !opts.include.devDependencies ||
isOptional && !opts.include.optionalDependencies ||
!isDev && !isOptional && !opts.include.dependencies
)
})
)
}
}
stageLogger.debug({
prefix: ctx.lockfileDir,
stage: 'resolution_done',
})
newLockfile = ((opts.hooks?.afterAllResolved) != null)
? await pipeWith(async (f, res) => f(await res), opts.hooks.afterAllResolved as any)(newLockfile) as Lockfile // eslint-disable-line
: newLockfile
if (opts.updateLockfileMinorVersion) {
newLockfile.lockfileVersion = LOCKFILE_VERSION
}
const depsStateCache: DepsStateCache = {}
const lockfileOpts = {
forceSharedFormat: opts.forceSharedLockfile,
useInlineSpecifiersFormat: opts.useInlineSpecifiersLockfileFormat,
useGitBranchLockfile: opts.useGitBranchLockfile,
mergeGitBranchLockfiles: opts.mergeGitBranchLockfiles,
}
if (!opts.lockfileOnly && opts.enableModulesDir) {
const result = await linkPackages(
projects,
dependenciesGraph,
{
currentLockfile: ctx.currentLockfile,
dependenciesByProjectId,
depsStateCache,
extraNodePaths: ctx.extraNodePaths,
force: opts.force,
hoistedDependencies: ctx.hoistedDependencies,
hoistedModulesDir: ctx.hoistedModulesDir,
hoistPattern: ctx.hoistPattern,
ignoreScripts: opts.ignoreScripts,
include: opts.include,
linkedDependenciesByProjectId,
lockfileDir: opts.lockfileDir,
makePartialCurrentLockfile: opts.makePartialCurrentLockfile,
outdatedDependencies,
pruneStore: opts.pruneStore,
pruneVirtualStore: opts.pruneVirtualStore,
publicHoistPattern: ctx.publicHoistPattern,
registries: ctx.registries,
rootModulesDir: ctx.rootModulesDir,
sideEffectsCacheRead: opts.sideEffectsCacheRead,
symlink: opts.symlink,
skipped: ctx.skipped,
storeController: opts.storeController,
virtualStoreDir: ctx.virtualStoreDir,
wantedLockfile: newLockfile,
wantedToBeSkippedPackageIds,
}
)
await finishLockfileUpdates()
if (opts.enablePnp) {
const importerNames = fromPairs(
projects.map(({ manifest, id }) => [id, manifest.name ?? id])
)
await writePnpFile(result.currentLockfile, {
importerNames,
lockfileDir: ctx.lockfileDir,
virtualStoreDir: ctx.virtualStoreDir,
registries: ctx.registries,
})
}
ctx.pendingBuilds = ctx.pendingBuilds
.filter((relDepPath) => !result.removedDepPaths.has(relDepPath))
if (result.newDepPaths?.length) {
if (opts.ignoreScripts) {
// we can use concat here because we always only append new packages, which are guaranteed to not be there by definition
ctx.pendingBuilds = ctx.pendingBuilds
.concat(
await pFilter(result.newDepPaths,
(depPath) => {
const requiresBuild = dependenciesGraph[depPath].requiresBuild
if (typeof requiresBuild === 'function') return requiresBuild()
return requiresBuild
}
)
)
}
if (!opts.ignoreScripts || Object.keys(opts.patchedDependencies ?? {}).length > 0) {
// postinstall hooks
const depPaths = Object.keys(dependenciesGraph)
const rootNodes = depPaths.filter((depPath) => dependenciesGraph[depPath].depth === 0)
let extraEnv: Record<string, string> | undefined = opts.scriptsOpts.extraEnv
if (opts.enablePnp) {
extraEnv = {
...extraEnv,
...makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs')),
}
}
await buildModules(dependenciesGraph, rootNodes, {
childConcurrency: opts.childConcurrency,
depsStateCache,
depsToBuild: new Set(result.newDepPaths),
extraBinPaths: ctx.extraBinPaths,
extraNodePaths: ctx.extraNodePaths,
extraEnv,
ignoreScripts: opts.ignoreScripts || opts.ignoreDepScripts,
lockfileDir: ctx.lockfileDir,
optional: opts.include.optionalDependencies,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
rawConfig: opts.rawConfig,
rootModulesDir: ctx.virtualStoreDir,
scriptsPrependNodePath: opts.scriptsPrependNodePath,
scriptShell: opts.scriptShell,
shellEmulator: opts.shellEmulator,
sideEffectsCacheWrite: opts.sideEffectsCacheWrite,
storeController: opts.storeController,
unsafePerm: opts.unsafePerm,
userAgent: opts.userAgent,
})
}
}
const binWarn = (prefix: string, message: string) => logger.info({ message, prefix })
if (result.newDepPaths?.length) {
const newPkgs = props<string, DependenciesGraphNode>(result.newDepPaths, dependenciesGraph)
await linkAllBins(newPkgs, dependenciesGraph, {
extraNodePaths: ctx.extraNodePaths,
optional: opts.include.optionalDependencies,
warn: binWarn.bind(null, opts.lockfileDir),
})
}
await Promise.all(projects.map(async (project, index) => {
let linkedPackages!: string[]
if (ctx.publicHoistPattern?.length && path.relative(project.rootDir, opts.lockfileDir) === '') {
const nodeExecPathByAlias = Object.entries(project.manifest.dependenciesMeta ?? {})
.reduce((prev, [alias, { node }]) => {
if (node) {
prev[alias] = node
}
return prev
}, {})
linkedPackages = await linkBins(project.modulesDir, project.binsDir, {
allowExoticManifests: true,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,
projectManifest: project.manifest,
nodeExecPathByAlias,
extraNodePaths: ctx.extraNodePaths,
warn: binWarn.bind(null, project.rootDir),
})
} else {
const directPkgs = [
...props<string, DependenciesGraphNode>(
Object.values(dependenciesByProjectId[project.id]).filter((depPath) => !ctx.skipped.has(depPath)),
dependenciesGraph
),
...linkedDependenciesByProjectId[project.id].map(({ pkgId }) => ({
dir: path.join(project.rootDir, pkgId.substring(5)),
fetchingBundledManifest: undefined,
})),
]
linkedPackages = await linkBinsOfPackages(
(
await Promise.all(
directPkgs.map(async (dep) => {
const manifest = await dep.fetchingBundledManifest?.() ?? await safeReadProjectManifestOnly(dep.dir)
let nodeExecPath: string | undefined
if (manifest?.name) {
nodeExecPath = project.manifest.dependenciesMeta?.[manifest.name]?.node
}
return {
location: dep.dir,
manifest,
nodeExecPath,
}
})
)
)
.filter(({ manifest }) => manifest != null) as Array<{ location: string, manifest: DependencyManifest }>,
project.binsDir,
{
extraNodePaths: ctx.extraNodePaths,
preferSymlinkedExecutables: opts.preferSymlinkedExecutables,