-
Notifications
You must be signed in to change notification settings - Fork 604
/
InstallManager.ts
1498 lines (1301 loc) · 60.4 KB
/
InstallManager.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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as glob from 'glob';
import * as colors from 'colors';
import * as fetch from 'node-fetch';
import * as http from 'http';
import HttpsProxyAgent = require('https-proxy-agent');
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as tar from 'tar';
import * as globEscape from 'glob-escape';
import {
JsonFile,
Text,
IPackageJson,
MapExtensions,
FileSystem,
FileConstants,
Sort,
PosixModeBits,
JsonObject,
NewlineKind
} from '@rushstack/node-core-library';
import { ApprovedPackagesChecker } from '../logic/ApprovedPackagesChecker';
import { AsyncRecycler } from '../utilities/AsyncRecycler';
import { BaseLinkManager } from '../logic/base/BaseLinkManager';
import { BaseShrinkwrapFile } from '../logic/base/BaseShrinkwrapFile';
import { PolicyValidator } from '../logic/policy/PolicyValidator';
import { IRushTempPackageJson } from '../logic/base/BasePackage';
import { Git } from '../logic/Git';
import { LastInstallFlag } from '../api/LastInstallFlag';
import { LinkManagerFactory } from '../logic/LinkManagerFactory';
import { PurgeManager } from './PurgeManager';
import { RushConfiguration, ICurrentVariantJson, IConfigurationEnvironment } from '../api/RushConfiguration';
import { RushConfigurationProject } from '../api/RushConfigurationProject';
import { RushConstants } from '../logic/RushConstants';
import { ShrinkwrapFileFactory } from '../logic/ShrinkwrapFileFactory';
import { Stopwatch } from '../utilities/Stopwatch';
import { Utilities } from '../utilities/Utilities';
import { Rush } from '../api/Rush';
import { PackageJsonEditor, DependencyType, PackageJsonDependency } from '../api/PackageJsonEditor';
import { AlreadyReportedError } from '../utilities/AlreadyReportedError';
import { CommonVersionsConfiguration } from '../api/CommonVersionsConfiguration';
import { RushGlobalFolder } from '../api/RushGlobalFolder';
import { PnpmPackageManager } from '../api/packageManager/PnpmPackageManager';
import { DependencySpecifier } from './DependencySpecifier';
import { EnvironmentConfiguration } from '../api/EnvironmentConfiguration';
import { InstallHelpers } from './InstallHelpers';
// The PosixModeBits are intended to be used with bitwise operations.
/* eslint-disable no-bitwise */
/**
* The "noMtime" flag is new in [email protected] and not available yet for \@types/tar.
* As a temporary workaround, augment the type.
*/
declare module 'tar' {
// eslint-disable-next-line @typescript-eslint/naming-convention
export interface CreateOptions {
/**
* "Set to true to omit writing mtime values for entries. Note that this prevents using other
* mtime-based features like tar.update or the keepNewer option with the resulting tar archive."
*/
noMtime?: boolean;
}
}
export interface IInstallManagerOptions {
/**
* Whether the global "--debug" flag was specified.
*/
debug: boolean;
/**
* Whether or not Rush will automatically update the shrinkwrap file.
* True for "rush update", false for "rush install".
*/
allowShrinkwrapUpdates: boolean;
/**
* Whether to skip policy checks.
*/
bypassPolicy: boolean;
/**
* Whether to skip linking, i.e. require "rush link" to be done manually later.
*/
noLink: boolean;
/**
* Whether to delete the shrinkwrap file before installation, i.e. so that all dependencies
* will be upgraded to the latest SemVer-compatible version.
*/
fullUpgrade: boolean;
/**
* Whether to force an update to the shrinkwrap file even if it appears to be unnecessary.
* Normally Rush uses heuristics to determine when "pnpm install" can be skipped,
* but sometimes the heuristics can be inaccurate due to external influences
* (pnpmfile.js script logic, registry changes, etc).
*/
recheckShrinkwrap: boolean;
/**
* The value of the "--network-concurrency" command-line parameter, which
* is a diagnostic option used to troubleshoot network failures.
*
* Currently only supported for PNPM.
*/
networkConcurrency: number | undefined;
/**
* Whether or not to collect verbose logs from the package manager.
* If specified when using PNPM, the logs will be in /common/temp/pnpm.log
*/
collectLogFile: boolean;
/**
* The variant to consider when performing installations and validating shrinkwrap updates.
*/
variant?: string | undefined;
/**
* Retry the install the specified number of times
*/
maxInstallAttempts: number;
}
/**
* This class implements common logic between "rush install" and "rush update".
*/
export class InstallManager {
private _rushConfiguration: RushConfiguration;
private _rushGlobalFolder: RushGlobalFolder;
private _commonNodeModulesMarker: LastInstallFlag;
private _commonTempFolderRecycler: AsyncRecycler;
private _options: IInstallManagerOptions;
public constructor(
rushConfiguration: RushConfiguration,
rushGlobalFolder: RushGlobalFolder,
purgeManager: PurgeManager,
options: IInstallManagerOptions
) {
this._rushConfiguration = rushConfiguration;
this._rushGlobalFolder = rushGlobalFolder;
this._commonTempFolderRecycler = purgeManager.commonTempFolderRecycler;
this._options = options;
const lastInstallState: JsonObject = {
node: process.versions.node,
packageManager: rushConfiguration.packageManager,
packageManagerVersion: rushConfiguration.packageManagerToolVersion
};
if (lastInstallState.packageManager === 'pnpm') {
lastInstallState.storePath = rushConfiguration.pnpmOptions.pnpmStorePath;
}
this._commonNodeModulesMarker = new LastInstallFlag(
this._rushConfiguration.commonTempFolder,
lastInstallState
);
}
/**
* Returns a map of all direct dependencies that only have a single semantic version specifier.
* Returns a map: dependency name --> version specifier
*/
public static collectImplicitlyPreferredVersions(
rushConfiguration: RushConfiguration,
options: {
variant?: string | undefined;
} = {}
): Map<string, string> {
// First, collect all the direct dependencies of all local projects, and their versions:
// direct dependency name --> set of version specifiers
const versionsForDependencies: Map<string, Set<string>> = new Map<string, Set<string>>();
rushConfiguration.projects.forEach((project: RushConfigurationProject) => {
InstallManager._collectVersionsForDependencies(rushConfiguration, {
versionsForDependencies,
dependencies: project.packageJsonEditor.dependencyList,
cyclicDependencies: project.cyclicDependencyProjects,
variant: options.variant
});
InstallManager._collectVersionsForDependencies(rushConfiguration, {
versionsForDependencies,
dependencies: project.packageJsonEditor.devDependencyList,
cyclicDependencies: project.cyclicDependencyProjects,
variant: options.variant
});
});
// If any dependency has more than one version, then filter it out (since we don't know which version
// should be preferred). What remains will be the list of preferred dependencies.
// dependency --> version specifier
const implicitlyPreferred: Map<string, string> = new Map<string, string>();
versionsForDependencies.forEach((versions: Set<string>, dep: string) => {
if (versions.size === 1) {
const version: string = versions.values().next().value;
implicitlyPreferred.set(dep, version);
}
});
return implicitlyPreferred;
}
// Helper for collectImplicitlyPreferredVersions()
private static _updateVersionsForDependencies(
versionsForDependencies: Map<string, Set<string>>,
dependency: string,
version: string
): void {
if (!versionsForDependencies.has(dependency)) {
versionsForDependencies.set(dependency, new Set<string>());
}
versionsForDependencies.get(dependency)!.add(version);
}
// Helper for collectImplicitlyPreferredVersions()
private static _collectVersionsForDependencies(
rushConfiguration: RushConfiguration,
options: {
versionsForDependencies: Map<string, Set<string>>;
dependencies: ReadonlyArray<PackageJsonDependency>;
cyclicDependencies: Set<string>;
variant: string | undefined;
}
): void {
const { variant, dependencies, versionsForDependencies, cyclicDependencies } = options;
const commonVersions: CommonVersionsConfiguration = rushConfiguration.getCommonVersions(variant);
const allowedAlternativeVersions: Map<string, ReadonlyArray<string>> =
commonVersions.allowedAlternativeVersions;
for (const dependency of dependencies) {
const alternativesForThisDependency: ReadonlyArray<string> =
allowedAlternativeVersions.get(dependency.name) || [];
// For each dependency, collectImplicitlyPreferredVersions() is collecting the set of all version specifiers
// that appear across the repo. If there is only one version specifier, then that's the "preferred" one.
// However, there are a few cases where additional version specifiers can be safely ignored.
let ignoreVersion: boolean = false;
// 1. If the version specifier was listed in "allowedAlternativeVersions", then it's never a candidate.
// (Even if it's the only version specifier anywhere in the repo, we still ignore it, because
// otherwise the rule would be difficult to explain.)
if (alternativesForThisDependency.indexOf(dependency.version) > 0) {
ignoreVersion = true;
} else {
// Is it a local project?
const localProject: RushConfigurationProject | undefined = rushConfiguration.getProjectByName(
dependency.name
);
if (localProject) {
// 2. If it's a symlinked local project, then it's not a candidate, because the package manager will
// never even see it.
// However there are two ways that a local project can NOT be symlinked:
// - if the local project doesn't satisfy the referenced semver specifier; OR
// - if the local project was specified in "cyclicDependencyProjects" in rush.json
if (
semver.satisfies(localProject.packageJsonEditor.version, dependency.version) &&
!cyclicDependencies.has(dependency.name)
) {
ignoreVersion = true;
}
}
if (!ignoreVersion) {
InstallManager._updateVersionsForDependencies(
versionsForDependencies,
dependency.name,
dependency.version
);
}
}
}
}
public get commonNodeModulesMarker(): LastInstallFlag {
return this._commonNodeModulesMarker;
}
public async doInstall(): Promise<void> {
const options: IInstallManagerOptions = this._options;
// Check the policies
PolicyValidator.validatePolicy(this._rushConfiguration, options);
// Git hooks are only installed if the repo opts in by including files in /common/git-hooks
const hookSource: string = path.join(this._rushConfiguration.commonFolder, 'git-hooks');
const hookDestination: string | undefined = Git.getHooksFolder();
if (FileSystem.exists(hookSource) && hookDestination) {
const allHookFilenames: string[] = FileSystem.readFolder(hookSource);
// Ignore the ".sample" file(s) in this folder.
const hookFilenames: string[] = allHookFilenames.filter((x) => !/\.sample$/.test(x));
if (hookFilenames.length > 0) {
console.log(os.EOL + colors.bold('Found files in the "common/git-hooks" folder.'));
// Clear the currently installed git hooks and install fresh copies
FileSystem.ensureEmptyFolder(hookDestination);
// Only copy files that look like Git hook names
const filteredHookFilenames: string[] = hookFilenames.filter((x) => /^[a-z\-]+/.test(x));
for (const filename of filteredHookFilenames) {
// Copy the file. Important: For Bash scripts, the EOL must not be CRLF.
const hookFileContent: string = FileSystem.readFile(path.join(hookSource, filename));
FileSystem.writeFile(path.join(hookDestination, filename), hookFileContent, {
convertLineEndings: NewlineKind.Lf
});
FileSystem.changePosixModeBits(
path.join(hookDestination, filename),
PosixModeBits.UserRead | PosixModeBits.UserExecute
);
}
console.log(
'Successfully installed these Git hook scripts: ' + filteredHookFilenames.join(', ') + os.EOL
);
}
}
const approvedPackagesChecker: ApprovedPackagesChecker = new ApprovedPackagesChecker(
this._rushConfiguration
);
if (approvedPackagesChecker.approvedPackagesFilesAreOutOfDate) {
if (this._options.allowShrinkwrapUpdates) {
approvedPackagesChecker.rewriteConfigFiles();
console.log(
colors.yellow(
'Approved package files have been updated. These updates should be committed to source control'
)
);
} else {
throw new Error(`Approved packages files are out-of date. Run "rush update" to update them.`);
}
}
// Ensure that the package manager is installed
await InstallHelpers.ensureLocalPackageManager(
this._rushConfiguration,
this._rushGlobalFolder,
this._options.maxInstallAttempts
);
let shrinkwrapFile: BaseShrinkwrapFile | undefined = undefined;
// (If it's a full update, then we ignore the shrinkwrap from Git since it will be overwritten)
if (!options.fullUpgrade) {
try {
shrinkwrapFile = ShrinkwrapFileFactory.getShrinkwrapFile(
this._rushConfiguration.packageManager,
this._rushConfiguration.packageManagerOptions,
this._rushConfiguration.getCommittedShrinkwrapFilename(options.variant)
);
} catch (ex) {
console.log();
console.log(`Unable to load the ${this._shrinkwrapFilePhrase}: ${ex.message}`);
if (!options.allowShrinkwrapUpdates) {
console.log();
console.log(colors.red('You need to run "rush update" to fix this problem'));
throw new AlreadyReportedError();
}
shrinkwrapFile = undefined;
}
}
// Write a file indicating which variant is being installed.
// This will be used by bulk scripts to determine the correct Shrinkwrap file to track.
const currentVariantJsonFilename: string = this._rushConfiguration.currentVariantJsonFilename;
const currentVariantJson: ICurrentVariantJson = {
variant: options.variant || null // eslint-disable-line @rushstack/no-null
};
// Determine if the variant is already current by updating current-variant.json.
// If nothing is written, the variant has not changed.
const variantIsUpToDate: boolean = !JsonFile.save(currentVariantJson, currentVariantJsonFilename, {
onlyIfChanged: true
});
if (options.variant) {
console.log();
console.log(colors.bold(`Using variant '${options.variant}' for installation.`));
} else if (!variantIsUpToDate && !options.variant) {
console.log();
console.log(colors.bold('Using the default variant for installation.'));
}
const shrinkwrapIsUpToDate: boolean =
this._createTempModulesAndCheckShrinkwrap({
shrinkwrapFile,
variant: options.variant
}) && !options.recheckShrinkwrap;
if (!shrinkwrapIsUpToDate) {
if (!options.allowShrinkwrapUpdates) {
console.log();
console.log(
colors.red(`The ${this._shrinkwrapFilePhrase} is out of date. You need to run "rush update".`)
);
throw new AlreadyReportedError();
}
}
await this._installCommonModules({
shrinkwrapIsUpToDate,
variantIsUpToDate,
...options
});
if (!options.noLink) {
const linkManager: BaseLinkManager = LinkManagerFactory.getLinkManager(this._rushConfiguration);
await linkManager.createSymlinksForProjects(false);
} else {
console.log(
os.EOL + colors.yellow('Since "--no-link" was specified, you will need to run "rush link" manually.')
);
}
}
/**
* Regenerates the common/package.json and all temp_modules projects.
* If shrinkwrapFile is provided, this function also validates whether it contains
* everything we need to install and returns true if so; in all other cases,
* the return value is false.
*/
private _createTempModulesAndCheckShrinkwrap(options: {
shrinkwrapFile: BaseShrinkwrapFile | undefined;
variant: string | undefined;
}): boolean {
const { shrinkwrapFile, variant } = options;
const stopwatch: Stopwatch = Stopwatch.start();
// Example: "C:\MyRepo\common\temp\projects"
const tempProjectsFolder: string = path.join(
this._rushConfiguration.commonTempFolder,
RushConstants.rushTempProjectsFolderName
);
console.log(os.EOL + colors.bold('Updating temp projects in ' + tempProjectsFolder));
Utilities.createFolderWithRetry(tempProjectsFolder);
const shrinkwrapWarnings: string[] = [];
// We will start with the assumption that it's valid, and then set it to false if
// any of the checks fail
let shrinkwrapIsUpToDate: boolean = true;
if (!shrinkwrapFile) {
shrinkwrapIsUpToDate = false;
}
// dependency name --> version specifier
const allExplicitPreferredVersions: Map<string, string> = this._rushConfiguration
.getCommonVersions(variant)
.getAllPreferredVersions();
if (shrinkwrapFile) {
// Check any (explicitly) preferred dependencies first
allExplicitPreferredVersions.forEach((version: string, dependency: string) => {
const dependencySpecifier: DependencySpecifier = new DependencySpecifier(dependency, version);
if (!shrinkwrapFile.hasCompatibleTopLevelDependency(dependencySpecifier)) {
shrinkwrapWarnings.push(
`"${dependency}" (${version}) required by the preferred versions from ` +
RushConstants.commonVersionsFilename
);
shrinkwrapIsUpToDate = false;
}
});
if (this._findOrphanedTempProjects(shrinkwrapFile)) {
// If there are any orphaned projects, then "npm install" would fail because the shrinkwrap
// contains references such as "resolved": "file:projects\\project1" that refer to nonexistent
// file paths.
shrinkwrapIsUpToDate = false;
}
}
// Also copy down the committed .npmrc file, if there is one
// "common\config\rush\.npmrc" --> "common\temp\.npmrc"
// Also ensure that we remove any old one that may be hanging around
Utilities.syncNpmrc(
this._rushConfiguration.commonRushConfigFolder,
this._rushConfiguration.commonTempFolder
);
// also, copy the pnpmfile.js if it exists
if (this._rushConfiguration.packageManager === 'pnpm') {
const committedPnpmFilePath: string = this._rushConfiguration.getPnpmfilePath(this._options.variant);
const tempPnpmFilePath: string = path.join(
this._rushConfiguration.commonTempFolder,
RushConstants.pnpmfileFilename
);
// ensure that we remove any old one that may be hanging around
this._syncFile(committedPnpmFilePath, tempPnpmFilePath);
}
const commonPackageJson: IPackageJson = {
dependencies: {},
description: 'Temporary file generated by the Rush tool',
name: 'rush-common',
private: true,
version: '0.0.0'
};
// dependency name --> version specifier
const allPreferredVersions: Map<string, string> = new Map<string, string>();
// Should we add implicitly preferred versions?
let useImplicitlyPinnedVersions: boolean;
if (this._rushConfiguration.commonVersions.implicitlyPreferredVersions !== undefined) {
// Use the manually configured setting
useImplicitlyPinnedVersions = this._rushConfiguration.commonVersions.implicitlyPreferredVersions;
} else {
// Default to true.
useImplicitlyPinnedVersions = true;
}
if (useImplicitlyPinnedVersions) {
// Add in the implicitly preferred versions.
// These are any first-level dependencies for which we only consume a single version range
// (e.g. every package that depends on react uses an identical specifier)
const implicitlyPreferredVersions: Map<
string,
string
> = InstallManager.collectImplicitlyPreferredVersions(this._rushConfiguration, { variant });
MapExtensions.mergeFromMap(allPreferredVersions, implicitlyPreferredVersions);
}
// Add in the explicitly preferred versions.
// Note that these take precedence over implicitly preferred versions.
MapExtensions.mergeFromMap(allPreferredVersions, allExplicitPreferredVersions);
// Add any preferred versions to the top of the commonPackageJson
// do this in alphabetical order for simpler debugging
for (const dependency of Array.from(allPreferredVersions.keys()).sort()) {
commonPackageJson.dependencies![dependency] = allPreferredVersions.get(dependency)!;
}
// To make the common/package.json file more readable, sort alphabetically
// according to rushProject.tempProjectName instead of packageName.
const sortedRushProjects: RushConfigurationProject[] = this._rushConfiguration.projects.slice(0);
Sort.sortBy(sortedRushProjects, (x) => x.tempProjectName);
for (const rushProject of sortedRushProjects) {
const packageJson: PackageJsonEditor = rushProject.packageJsonEditor;
// Example: "C:\MyRepo\common\temp\projects\my-project-2.tgz"
const tarballFile: string = this._getTarballFilePath(rushProject);
// Example: dependencies["@rush-temp/my-project-2"] = "file:./projects/my-project-2.tgz"
commonPackageJson.dependencies![
rushProject.tempProjectName
] = `file:./${RushConstants.rushTempProjectsFolderName}/${rushProject.unscopedTempProjectName}.tgz`;
const tempPackageJson: IRushTempPackageJson = {
name: rushProject.tempProjectName,
version: '0.0.0',
private: true,
dependencies: {}
};
// Collect pairs of (packageName, packageVersion) to be added as dependencies of the @rush-temp package.json
const tempDependencies: Map<string, string> = new Map<string, string>();
// These can be regular, optional, or peer dependencies (but NOT dev dependencies).
// (A given packageName will never appear more than once in this list.)
for (const dependency of packageJson.dependencyList) {
// If there are any optional dependencies, copy directly into the optionalDependencies field.
if (dependency.dependencyType === DependencyType.Optional) {
if (!tempPackageJson.optionalDependencies) {
tempPackageJson.optionalDependencies = {};
}
tempPackageJson.optionalDependencies[dependency.name] = dependency.version;
} else {
tempDependencies.set(dependency.name, dependency.version);
}
}
for (const dependency of packageJson.devDependencyList) {
// If there are devDependencies, we need to merge them with the regular dependencies. If the same
// library appears in both places, then the dev dependency wins (because presumably it's saying what you
// want right now for development, not the range that you support for consumers).
tempDependencies.set(dependency.name, dependency.version);
}
Sort.sortMapKeys(tempDependencies);
for (const [packageName, packageVersion] of tempDependencies.entries()) {
const dependencySpecifier: DependencySpecifier = new DependencySpecifier(packageName, packageVersion);
// Is there a locally built Rush project that could satisfy this dependency?
// If so, then we will symlink to the project folder rather than to common/temp/node_modules.
// In this case, we don't want "npm install" to process this package, but we do need
// to record this decision for "rush link" later, so we add it to a special 'rushDependencies' field.
const localProject: RushConfigurationProject | undefined = this._rushConfiguration.getProjectByName(
packageName
);
if (localProject) {
// Don't locally link if it's listed in the cyclicDependencyProjects
if (!rushProject.cyclicDependencyProjects.has(packageName)) {
// Also, don't locally link if the SemVer doesn't match
const localProjectVersion: string = localProject.packageJsonEditor.version;
if (semver.satisfies(localProjectVersion, packageVersion)) {
// We will locally link this package, so instead add it to our special "rushDependencies"
// field in the package.json file.
if (!tempPackageJson.rushDependencies) {
tempPackageJson.rushDependencies = {};
}
tempPackageJson.rushDependencies[packageName] = packageVersion;
continue;
}
}
}
// We will NOT locally link this package; add it as a regular dependency.
tempPackageJson.dependencies![packageName] = packageVersion;
let tryReusingPackageVersionsFromShrinkwrap: boolean = true;
if (this._rushConfiguration.packageManager === 'pnpm') {
// Shrinkwrap churn optimization doesn't make sense when --frozen-lockfile is true
tryReusingPackageVersionsFromShrinkwrap = !this._rushConfiguration.experimentsConfiguration
.configuration.usePnpmFrozenLockfileForRushInstall;
}
if (shrinkwrapFile) {
if (
!shrinkwrapFile.tryEnsureCompatibleDependency(
dependencySpecifier,
rushProject.tempProjectName,
tryReusingPackageVersionsFromShrinkwrap
)
) {
shrinkwrapWarnings.push(
`"${packageName}" (${packageVersion}) required by "${rushProject.packageName}"`
);
shrinkwrapIsUpToDate = false;
}
}
}
// Example: "C:\MyRepo\common\temp\projects\my-project-2"
const tempProjectFolder: string = this._getTempProjectFolder(rushProject);
// Example: "C:\MyRepo\common\temp\projects\my-project-2\package.json"
const tempPackageJsonFilename: string = path.join(tempProjectFolder, FileConstants.PackageJson);
// we only want to overwrite the package if the existing tarball's package.json is different from tempPackageJson
let shouldOverwrite: boolean = true;
try {
// if the tarball and the temp file still exist, then compare the contents
if (FileSystem.exists(tarballFile) && FileSystem.exists(tempPackageJsonFilename)) {
// compare the extracted package.json with the one we are about to write
const oldBuffer: Buffer = FileSystem.readFileToBuffer(tempPackageJsonFilename);
const newBuffer: Buffer = Buffer.from(JsonFile.stringify(tempPackageJson));
if (Buffer.compare(oldBuffer, newBuffer) === 0) {
shouldOverwrite = false;
}
}
} catch (error) {
// ignore the error, we will go ahead and create a new tarball
}
if (shouldOverwrite) {
try {
// ensure the folder we are about to zip exists
Utilities.createFolderWithRetry(tempProjectFolder);
// remove the old tarball & old temp package json, this is for any cases where new tarball creation
// fails, and the shouldOverwrite logic is messed up because the my-project-2\package.json
// exists and is updated, but the tarball is not accurate
FileSystem.deleteFile(tarballFile);
FileSystem.deleteFile(tempPackageJsonFilename);
// write the expected package.json file into the zip staging folder
JsonFile.save(tempPackageJson, tempPackageJsonFilename);
// Delete the existing tarball and create a new one
this._createTempProjectTarball(rushProject);
console.log(`Updating ${tarballFile}`);
} catch (error) {
console.log(colors.yellow(error));
// delete everything in case of any error
FileSystem.deleteFile(tarballFile);
FileSystem.deleteFile(tempPackageJsonFilename);
}
}
}
// Example: "C:\MyRepo\common\temp\package.json"
const commonPackageJsonFilename: string = path.join(
this._rushConfiguration.commonTempFolder,
FileConstants.PackageJson
);
if (shrinkwrapFile) {
// If we have a (possibly incomplete) shrinkwrap file, check to see if any shrinkwrap-specific
// changes make the shrinkwrap out-of-date, and save it as the temporary file.
if (shrinkwrapFile.shouldForceRecheck()) {
shrinkwrapIsUpToDate = false;
}
shrinkwrapFile.save(this._rushConfiguration.tempShrinkwrapFilename);
shrinkwrapFile.save(this._rushConfiguration.tempShrinkwrapPreinstallFilename);
} else {
// Otherwise delete the temporary file
FileSystem.deleteFile(this._rushConfiguration.tempShrinkwrapFilename);
if (this._rushConfiguration.packageManager === 'pnpm') {
// Workaround for https://github.com/pnpm/pnpm/issues/1890
//
// When "rush update --full" is run, rush deletes common/temp/pnpm-lock.yaml so that
// a new lockfile can be generated. But because of the above bug "pnpm install" would
// respect "common/temp/node_modules/.pnpm-lock.yaml" and thus would not generate a
// new lockfile. Deleting this file in addition to deleting common/temp/pnpm-lock.yaml
// ensures that a new lockfile will be generated with "rush update --full".
const pnpmPackageManager: PnpmPackageManager = this._rushConfiguration
.packageManagerWrapper as PnpmPackageManager;
FileSystem.deleteFile(
path.join(
this._rushConfiguration.commonTempFolder,
pnpmPackageManager.internalShrinkwrapRelativePath
)
);
}
}
// Don't update the file timestamp unless the content has changed, since "rush install"
// will consider this timestamp
JsonFile.save(commonPackageJson, commonPackageJsonFilename, { onlyIfChanged: true });
stopwatch.stop();
console.log(`Finished creating temporary modules (${stopwatch.toString()})`);
if (shrinkwrapWarnings.length > 0) {
console.log();
console.log(
colors.yellow(
Utilities.wrapWords(`The ${this._shrinkwrapFilePhrase} is missing the following dependencies:`)
)
);
for (const shrinkwrapWarning of shrinkwrapWarnings) {
console.log(colors.yellow(' ' + shrinkwrapWarning));
}
console.log();
}
return shrinkwrapIsUpToDate;
}
private _getTempProjectFolder(rushProject: RushConfigurationProject): string {
const unscopedTempProjectName: string = rushProject.unscopedTempProjectName;
return path.join(
this._rushConfiguration.commonTempFolder,
RushConstants.rushTempProjectsFolderName,
unscopedTempProjectName
);
}
/**
* Deletes the existing tarball and creates a tarball for the given rush project
*/
private _createTempProjectTarball(rushProject: RushConfigurationProject): void {
const tarballFile: string = this._getTarballFilePath(rushProject);
const tempProjectFolder: string = this._getTempProjectFolder(rushProject);
FileSystem.deleteFile(tarballFile);
// NPM expects the root of the tarball to have a directory called 'package'
const npmPackageFolder: string = 'package';
const tarOptions: tar.CreateOptions = {
gzip: true,
file: tarballFile,
cwd: tempProjectFolder,
portable: true,
noMtime: true,
noPax: true,
sync: true,
prefix: npmPackageFolder,
filter: (path: string, stat: tar.FileStat): boolean => {
if (
!this._rushConfiguration.experimentsConfiguration.configuration.noChmodFieldInTarHeaderNormalization
) {
stat.mode =
(stat.mode & ~0x1ff) | PosixModeBits.AllRead | PosixModeBits.UserWrite | PosixModeBits.AllExecute;
}
return true;
}
} as tar.CreateOptions;
// create the new tarball
tar.create(tarOptions, [FileConstants.PackageJson]);
}
/**
* Runs "npm/pnpm/yarn install" in the "common/temp" folder.
*/
private _installCommonModules(
options: {
shrinkwrapIsUpToDate: boolean;
variantIsUpToDate: boolean;
} & IInstallManagerOptions
): Promise<void> {
const { shrinkwrapIsUpToDate, variantIsUpToDate } = options;
const usePnpmFrozenLockfile: boolean =
this._rushConfiguration.packageManager === 'pnpm' &&
this._rushConfiguration.experimentsConfiguration.configuration.usePnpmFrozenLockfileForRushInstall ===
true;
return Promise.resolve().then(() => {
console.log(
os.EOL + colors.bold('Checking node_modules in ' + this._rushConfiguration.commonTempFolder) + os.EOL
);
const commonNodeModulesFolder: string = path.join(
this._rushConfiguration.commonTempFolder,
'node_modules'
);
// This marker file indicates that the last "rush install" completed successfully
const markerFileExistedAndWasValidAtStart: boolean = this._commonNodeModulesMarker.checkValidAndReportStoreIssues();
// If "--clean" or "--full-clean" was specified, or if the last install was interrupted,
// then we will need to delete the node_modules folder. Otherwise, we can do an incremental
// install.
const deleteNodeModules: boolean = !markerFileExistedAndWasValidAtStart;
// Based on timestamps, can we skip this install entirely?
if (shrinkwrapIsUpToDate && !deleteNodeModules && variantIsUpToDate) {
const potentiallyChangedFiles: string[] = [];
// Consider the timestamp on the node_modules folder; if someone tampered with it
// or deleted it entirely, then we can't skip this install
potentiallyChangedFiles.push(commonNodeModulesFolder);
// Additionally, if they pulled an updated npm-shrinkwrap.json file from Git,
// then we can't skip this install
potentiallyChangedFiles.push(this._rushConfiguration.getCommittedShrinkwrapFilename(options.variant));
// Add common-versions.json file to the potentially changed files list.
potentiallyChangedFiles.push(this._rushConfiguration.getCommonVersionsFilePath(options.variant));
if (this._rushConfiguration.packageManager === 'pnpm') {
// If the repo is using pnpmfile.js, consider that also
const pnpmFileFilename: string = this._rushConfiguration.getPnpmfilePath(options.variant);
if (FileSystem.exists(pnpmFileFilename)) {
potentiallyChangedFiles.push(pnpmFileFilename);
}
}
// Also consider timestamps for all the temp tarballs. (createTempModulesAndCheckShrinkwrap() will
// carefully preserve these timestamps unless something has changed.)
// Example: "C:\MyRepo\common\temp\projects\my-project-2.tgz"
potentiallyChangedFiles.push(
...this._rushConfiguration.projects.map((x) => {
return this._getTarballFilePath(x);
})
);
// NOTE: If commonNodeModulesMarkerFilename (or any of the potentiallyChangedFiles) does not
// exist, then isFileTimestampCurrent() returns false.
if (Utilities.isFileTimestampCurrent(this._commonNodeModulesMarker.path, potentiallyChangedFiles)) {
// Nothing to do, because everything is up to date according to time stamps
return;
}
}
// Since we are actually running npm/pnpm/yarn install, recreate all the temp project tarballs.
// This ensures that any existing tarballs with older header bits will be regenerated.
// It is safe to assume that temp project pacakge.jsons already exist.
for (const rushProject of this._rushConfiguration.projects) {
this._createTempProjectTarball(rushProject);
}
return this._checkIfReleaseIsPublished()
.catch((error) => {
// If the user is working in an environment that can't reach the registry,
// don't bother them with errors.
return undefined;
})
.then((publishedRelease: boolean | undefined) => {
if (publishedRelease === false) {
console.log(
colors.yellow('Warning: This release of the Rush tool was unpublished; it may be unstable.')
);
}
// Since we're going to be tampering with common/node_modules, delete the "rush link" flag file if it exists;
// this ensures that a full "rush link" is required next time
Utilities.deleteFile(this._rushConfiguration.rushLinkJsonFilename);
// Delete the successful install file to indicate the install transaction has started
this._commonNodeModulesMarker.clear();
// NOTE: The PNPM store is supposed to be transactionally safe, so we don't delete it automatically.
// The user must request that via the command line.
if (deleteNodeModules) {
if (this._rushConfiguration.packageManager === 'npm') {
console.log(`Deleting the "npm-cache" folder`);
// This is faster and more thorough than "npm cache clean"
this._commonTempFolderRecycler.moveFolder(this._rushConfiguration.npmCacheFolder);
console.log(`Deleting the "npm-tmp" folder`);
this._commonTempFolderRecycler.moveFolder(this._rushConfiguration.npmTmpFolder);
}
}
// Example: "C:\MyRepo\common\temp\npm-local\node_modules\.bin\npm"
const packageManagerFilename: string = this._rushConfiguration.packageManagerToolFilename;
let packageManagerEnv: NodeJS.ProcessEnv = process.env;
let configurationEnvironment: IConfigurationEnvironment | undefined = undefined;
if (this._rushConfiguration.packageManager === 'npm') {
if (
this._rushConfiguration.npmOptions &&
this._rushConfiguration.npmOptions.environmentVariables
) {
configurationEnvironment = this._rushConfiguration.npmOptions.environmentVariables;
}
} else if (this._rushConfiguration.packageManager === 'pnpm') {
if (
this._rushConfiguration.pnpmOptions &&
this._rushConfiguration.pnpmOptions.environmentVariables
) {
configurationEnvironment = this._rushConfiguration.pnpmOptions.environmentVariables;
}
} else if (this._rushConfiguration.packageManager === 'yarn') {
if (
this._rushConfiguration.yarnOptions &&
this._rushConfiguration.yarnOptions.environmentVariables
) {
configurationEnvironment = this._rushConfiguration.yarnOptions.environmentVariables;
}
}
packageManagerEnv = this._mergeEnvironmentVariables(process.env, configurationEnvironment);
// Is there an existing "node_modules" folder to consider?
if (FileSystem.exists(commonNodeModulesFolder)) {
// Should we delete the entire "node_modules" folder?
if (deleteNodeModules) {
// YES: Delete "node_modules"
// Explain to the user why we are hosing their node_modules folder
console.log('Deleting files from ' + commonNodeModulesFolder);
this._commonTempFolderRecycler.moveFolder(commonNodeModulesFolder);
Utilities.createFolderWithRetry(commonNodeModulesFolder);
} else {
// NO: Prepare to do an incremental install in the "node_modules" folder
// note: it is not necessary to run "prune" with pnpm
if (this._rushConfiguration.packageManager === 'npm') {
console.log(
`Running "${this._rushConfiguration.packageManager} prune"` +
` in ${this._rushConfiguration.commonTempFolder}`
);
const args: string[] = ['prune'];
this._pushConfigurationArgs(args, options);
Utilities.executeCommandWithRetry(
this._options.maxInstallAttempts,
packageManagerFilename,
args,
this._rushConfiguration.commonTempFolder,
packageManagerEnv
);
// Delete the (installed image of) the temp projects, since "npm install" does not
// detect changes for "file:./" references.
// We recognize the temp projects by their names, which always start with "rush-".
// Example: "C:\MyRepo\common\temp\node_modules\@rush-temp"
const pathToDeleteWithoutStar: string = path.join(
commonNodeModulesFolder,
RushConstants.rushTempNpmScope
);
console.log(`Deleting ${pathToDeleteWithoutStar}\\*`);
// Glob can't handle Windows paths
const normalizedpathToDeleteWithoutStar: string = Text.replaceAll(
pathToDeleteWithoutStar,