-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathSubspace.ts
More file actions
523 lines (456 loc) · 19.7 KB
/
Subspace.ts
File metadata and controls
523 lines (456 loc) · 19.7 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import * as crypto from 'node:crypto';
import { FileSystem } from '@rushstack/node-core-library';
import type { IPackageJson } from '@rushstack/node-core-library';
import type { RushConfiguration } from './RushConfiguration';
import type { RushConfigurationProject } from './RushConfigurationProject';
import { EnvironmentConfiguration } from './EnvironmentConfiguration';
import { RushConstants } from '../logic/RushConstants';
import { CommonVersionsConfiguration } from './CommonVersionsConfiguration';
import { RepoStateFile } from '../logic/RepoStateFile';
import type { PnpmPackageManager } from './packageManager/PnpmPackageManager';
import { PnpmOptionsConfiguration } from '../logic/pnpm/PnpmOptionsConfiguration';
import { SubspacePnpmfileConfiguration } from '../logic/pnpm/SubspacePnpmfileConfiguration';
import type { ISubspacePnpmfileShimSettings } from '../logic/pnpm/IPnpmfile';
/**
* @internal
*/
export interface ISubspaceOptions {
subspaceName: string;
rushConfiguration: RushConfiguration;
splitWorkspaceCompatibility: boolean;
}
interface ISubspaceDetail {
subspaceConfigFolderPath: string;
subspacePnpmPatchesFolderPath: string;
subspaceTempFolderPath: string;
tempShrinkwrapFilePath: string;
tempShrinkwrapPreinstallFilePath: string;
}
interface IPackageJsonLite extends Omit<IPackageJson, 'version'> {}
/**
* This represents the subspace configurations for a repository, based on the "subspaces.json"
* configuration file.
* @public
*/
export class Subspace {
public readonly subspaceName: string;
private readonly _rushConfiguration: RushConfiguration;
private readonly _projects: RushConfigurationProject[] = [];
private readonly _splitWorkspaceCompatibility: boolean;
private _commonVersionsConfiguration: CommonVersionsConfiguration | undefined = undefined;
private _detail: ISubspaceDetail | undefined;
private _cachedPnpmOptions: PnpmOptionsConfiguration | undefined = undefined;
// If true, then _cachedPnpmOptions has been initialized.
private _cachedPnpmOptionsInitialized: boolean = false;
public constructor(options: ISubspaceOptions) {
this.subspaceName = options.subspaceName;
this._rushConfiguration = options.rushConfiguration;
this._splitWorkspaceCompatibility = options.splitWorkspaceCompatibility;
}
/**
* Returns the list of projects belonging to this subspace.
* @beta
*/
public getProjects(): RushConfigurationProject[] {
return this._projects;
}
/**
* Returns the parsed contents of the pnpm-config.json config file.
* @beta
*/
public getPnpmOptions(): PnpmOptionsConfiguration | undefined {
if (!this._cachedPnpmOptionsInitialized) {
// Calculate these outside the try/catch block since their error messages shouldn't be annotated:
const subspaceTempFolder: string = this.getSubspaceTempFolderPath();
try {
this._cachedPnpmOptions = PnpmOptionsConfiguration.loadFromJsonFileOrThrow(
this.getPnpmConfigFilePath(),
subspaceTempFolder
);
this._cachedPnpmOptionsInitialized = true;
} catch (e) {
if (FileSystem.isNotExistError(e as Error)) {
this._cachedPnpmOptions = undefined;
this._cachedPnpmOptionsInitialized = true;
} else {
throw new Error(
`The subspace "${this.subspaceName}" has an invalid pnpm-config.json file:\n` + e.message
);
}
}
}
return this._cachedPnpmOptions;
}
private _ensureDetail(): ISubspaceDetail {
if (!this._detail) {
const rushConfiguration: RushConfiguration = this._rushConfiguration;
let subspaceConfigFolderPath: string;
let subspacePnpmPatchesFolderPath: string;
if (rushConfiguration.subspacesFeatureEnabled) {
if (!rushConfiguration.pnpmOptions.useWorkspaces) {
throw new Error(
`The Rush subspaces feature is enabled. You must set useWorkspaces=true in pnpm-config.json.`
);
}
// If this subspace doesn't have a configuration folder, check if it is in the project folder itself
// if the splitWorkspaceCompatibility option is enabled in the subspace configuration
// Example: C:\MyRepo\common\config\subspaces\my-subspace
const standardSubspaceConfigFolder: string = `${rushConfiguration.commonFolder}/config/subspaces/${this.subspaceName}`;
subspaceConfigFolderPath = standardSubspaceConfigFolder;
if (this._splitWorkspaceCompatibility && this.subspaceName.startsWith('split_')) {
if (FileSystem.exists(standardSubspaceConfigFolder + '/pnpm-lock.yaml')) {
throw new Error(
`The split workspace subspace "${this.subspaceName}" cannot use a common/config folder: ` +
standardSubspaceConfigFolder
);
}
if (this._projects.length !== 1) {
throw new Error(
`The split workspace subspace "${this.subspaceName}" contains ${this._projects.length}` +
` projects; there must be exactly one project.`
);
}
const project: RushConfigurationProject = this._projects[0];
subspaceConfigFolderPath = `${project.projectFolder}/subspace/${this.subspaceName}`;
// Ensure that this project does not have it's own pnpmfile.cjs or .npmrc file
if (FileSystem.exists(`${project.projectFolder}/.npmrc`)) {
throw new Error(
`The project level configuration file ${project.projectFolder}/.npmrc is no longer valid. Please use a ${subspaceConfigFolderPath}/.npmrc file instead.`
);
}
if (FileSystem.exists(`${project.projectFolder}/.pnpmfile.cjs`)) {
throw new Error(
`The project level configuration file ${project.projectFolder}/.pnpmfile.cjs is no longer valid. Please use a ${subspaceConfigFolderPath}/.pnpmfile.cjs file instead.`
);
}
}
if (!FileSystem.exists(subspaceConfigFolderPath)) {
throw new Error(
`The configuration folder for the "${this.subspaceName}" subspace does not exist: ` +
subspaceConfigFolderPath
);
}
subspacePnpmPatchesFolderPath = `${subspaceConfigFolderPath}/${RushConstants.pnpmPatchesCommonFolderName}`;
} else {
// Example: C:\MyRepo\common\config\rush
subspaceConfigFolderPath = rushConfiguration.commonRushConfigFolder;
// Example: C:\MyRepo\common\pnpm-patches
subspacePnpmPatchesFolderPath = `${rushConfiguration.commonFolder}/${RushConstants.pnpmPatchesCommonFolderName}`;
}
// Example: C:\MyRepo\common\temp
const commonTempFolder: string =
EnvironmentConfiguration.rushTempFolderOverride || rushConfiguration.commonTempFolder;
let subspaceTempFolderPath: string;
if (rushConfiguration.subspacesFeatureEnabled) {
// Example: C:\MyRepo\common\temp\my-subspace
subspaceTempFolderPath = `${commonTempFolder}/${this.subspaceName}`;
} else {
// Example: C:\MyRepo\common\temp
subspaceTempFolderPath = commonTempFolder;
}
// Example: C:\MyRepo\common\temp\my-subspace\pnpm-lock.yaml
const tempShrinkwrapFilePath: string = `${subspaceTempFolderPath}/${rushConfiguration.shrinkwrapFilename}`;
/// From "C:\MyRepo\common\temp\pnpm-lock.yaml" --> "C:\MyRepo\common\temp\pnpm-lock-preinstall.yaml"
const parsedPath: path.ParsedPath = path.parse(tempShrinkwrapFilePath);
const tempShrinkwrapPreinstallFilePath: string = `${parsedPath.dir}/${parsedPath.name}-preinstall${parsedPath.ext}`;
this._detail = {
subspaceConfigFolderPath,
subspacePnpmPatchesFolderPath,
subspaceTempFolderPath,
tempShrinkwrapFilePath,
tempShrinkwrapPreinstallFilePath
};
}
return this._detail;
}
/**
* Returns the full path of the folder containing this subspace's variant-dependent configuration files
* such as `pnpm-lock.yaml`.
*
* Example (variants): `C:\MyRepo\common\config\rush\variants\my-variant`
* Example (variants and subspaces): `C:\MyRepo\common\config\subspaces\my-subspace\variants\my-variant`
* Example (subspaces): `C:\MyRepo\common\config\subspaces\my-subspace`
* Example (neither): `C:\MyRepo\common\config\rush`
* @beta
*
* @remarks
* The following files may be variant-dependent:
* - Lockfiles: (i.e. - `pnpm-lock.yaml`, `npm-shrinkwrap.json`, `yarn.lock`, etc)
* - 'common-versions.json'
* - 'pnpmfile.js'/'.pnpmfile.cjs'
*/
public getVariantDependentSubspaceConfigFolderPath(variant: string | undefined): string {
const subspaceConfigFolderPath: string = this.getSubspaceConfigFolderPath();
if (!variant) {
return subspaceConfigFolderPath;
} else {
return `${subspaceConfigFolderPath}/${RushConstants.rushVariantsFolderName}/${variant}`;
}
}
/**
* Returns the full path of the folder containing this subspace's configuration files such as `pnpm-lock.yaml`.
*
* Example (subspaces feature enabled): `C:\MyRepo\common\config\subspaces\my-subspace`
* Example (subspaces feature disabled): `C:\MyRepo\common\config\rush`
* @beta
*/
public getSubspaceConfigFolderPath(): string {
return this._ensureDetail().subspaceConfigFolderPath;
}
/**
* Returns the full path of the folder containing this subspace's configuration files such as `pnpm-lock.yaml`.
*
* Example (subspaces feature enabled): `C:\MyRepo\common\config\subspaces\my-subspace\pnpm-patches`
* Example (subspaces feature disabled): `C:\MyRepo\common\pnpm-patches`
* @beta
*/
public getSubspacePnpmPatchesFolderPath(): string {
return this._ensureDetail().subspacePnpmPatchesFolderPath;
}
/**
* The full path of the folder where the subspace's node_modules and other temporary files will be stored.
*
* Example (subspaces feature enabled): `C:\MyRepo\common\temp\subspaces\my-subspace`
* Example (subspaces feature disabled): `C:\MyRepo\common\temp`
* @beta
*/
public getSubspaceTempFolderPath(): string {
return this._ensureDetail().subspaceTempFolderPath;
}
/**
* Returns full path of the temporary shrinkwrap file for a specific subspace and returns the common workspace
* shrinkwrap if no subspaceName is provided.
* @remarks
* This function takes the subspace name, and returns the full path for the subspace's shrinkwrap file.
* This function also consults the deprecated option to allow for shrinkwraps to be stored under a package folder.
* This shrinkwrap file is used during "rush install", and may be rewritten by the package manager during installation
* This property merely reports the filename, the file itself may not actually exist.
* example: `C:\MyRepo\common\<subspace_name>\pnpm-lock.yaml`
* @beta
*/
public getTempShrinkwrapFilename(): string {
return this._ensureDetail().tempShrinkwrapFilePath;
}
/**
* @deprecated - Use {@link Subspace.getTempShrinkwrapPreinstallFilePath} instead.
*/
public getTempShrinkwrapPreinstallFilename(subspaceName?: string | undefined): string {
return this.getTempShrinkwrapPreinstallFilePath();
}
/**
* The full path of a backup copy of tempShrinkwrapFilename. This backup copy is made
* before installation begins, and can be compared to determine how the package manager
* modified tempShrinkwrapFilename.
* @remarks
* This property merely reports the filename; the file itself may not actually exist.
* Example: `C:\MyRepo\common\temp\npm-shrinkwrap-preinstall.json`
* or `C:\MyRepo\common\temp\pnpm-lock-preinstall.yaml`
* @beta
*/
public getTempShrinkwrapPreinstallFilePath(): string {
return this._ensureDetail().tempShrinkwrapPreinstallFilePath;
}
/**
* Gets the full path to the common-versions.json config file for this subspace.
*
* Example (subspaces feature enabled): `C:\MyRepo\common\config\subspaces\my-subspace\common-versions.json`
* Example (subspaces feature disabled): `C:\MyRepo\common\config\rush\common-versions.json`
* @beta
*/
public getCommonVersionsFilePath(variant?: string): string {
return (
this.getVariantDependentSubspaceConfigFolderPath(variant) + '/' + RushConstants.commonVersionsFilename
);
}
/**
* Gets the full path to the pnpm-config.json config file for this subspace.
*
* Example (subspaces feature enabled): `C:\MyRepo\common\config\subspaces\my-subspace\pnpm-config.json`
* Example (subspaces feature disabled): `C:\MyRepo\common\config\rush\pnpm-config.json`
* @beta
*/
public getPnpmConfigFilePath(): string {
return this.getSubspaceConfigFolderPath() + '/' + RushConstants.pnpmConfigFilename;
}
/**
* Gets the settings from the common-versions.json config file.
* @beta
*/
public getCommonVersions(variant?: string): CommonVersionsConfiguration {
const commonVersionsFilePath: string = this.getCommonVersionsFilePath(variant);
if (!this._commonVersionsConfiguration) {
this._commonVersionsConfiguration = CommonVersionsConfiguration.loadFromFile(
commonVersionsFilePath,
this._rushConfiguration
);
}
return this._commonVersionsConfiguration;
}
/**
* Gets the ensureConsistentVersions property from the common-versions.json config file,
* or from the rush.json file if it isn't defined in common-versions.json
* @beta
*/
public shouldEnsureConsistentVersions(variant?: string): boolean {
// If the ensureConsistentVersions field is defined, return the value of the field
const commonVersions: CommonVersionsConfiguration = this.getCommonVersions(variant);
if (commonVersions.ensureConsistentVersions !== undefined) {
return commonVersions.ensureConsistentVersions;
}
// Fallback to ensureConsistentVersions in rush.json if the setting is not defined in
// the common-versions.json file
return this._rushConfiguration.ensureConsistentVersions;
}
/**
* Gets the path to the repo-state.json file.
* @beta
*/
public getRepoStateFilePath(): string {
return this.getSubspaceConfigFolderPath() + '/' + RushConstants.repoStateFilename;
}
/**
* Gets the contents from the repo-state.json file.
* @param subspaceName - The name of the subspace in use by the active command.
* @beta
*/
public getRepoState(): RepoStateFile {
const repoStateFilePath: string = this.getRepoStateFilePath();
return RepoStateFile.loadFromFile(repoStateFilePath);
}
/**
* @deprecated - Use {@link Subspace.getCommittedShrinkwrapFilePath} instead.
*/
public getCommittedShrinkwrapFilename(): string {
return this.getCommittedShrinkwrapFilePath(undefined);
}
/**
* Gets the committed shrinkwrap file name for a specific variant.
* @param variant - The name of the current variant in use by the active command.
* @beta
*/
public getCommittedShrinkwrapFilePath(variant?: string): string {
const subspaceConfigFolderPath: string = this.getVariantDependentSubspaceConfigFolderPath(variant);
return `${subspaceConfigFolderPath}/${this._rushConfiguration.shrinkwrapFilename}`;
}
/**
* Gets the absolute path for "pnpmfile.js" for a specific subspace.
* @param subspace - The name of the current subspace in use by the active command.
* @remarks
* The file path is returned even if PNPM is not configured as the package manager.
* @beta
*/
public getPnpmfilePath(variant?: string): string {
const subspaceConfigFolderPath: string = this.getVariantDependentSubspaceConfigFolderPath(variant);
const pnpmFilename: string = (this._rushConfiguration.packageManagerWrapper as PnpmPackageManager)
.pnpmfileFilename;
return `${subspaceConfigFolderPath}/${pnpmFilename}`;
}
/**
* Returns true if the specified project belongs to this subspace.
* @beta
*/
public contains(project: RushConfigurationProject): boolean {
return project.subspace.subspaceName === this.subspaceName;
}
/** @internal */
public _addProject(project: RushConfigurationProject): void {
this._projects.push(project);
}
/**
* Computes a hash of the PNPM catalog definitions for this subspace.
* Returns undefined if no catalogs are defined.
*/
public getPnpmCatalogsHash(): string | undefined {
const pnpmOptions: PnpmOptionsConfiguration | undefined = this.getPnpmOptions();
if (!pnpmOptions) {
return undefined;
}
const catalogData: Record<string, unknown> = {};
if (pnpmOptions.globalCatalogs && Object.keys(pnpmOptions.globalCatalogs).length !== 0) {
Object.assign(catalogData, pnpmOptions.globalCatalogs);
}
// If no catalogs are defined, return undefined
if (Object.keys(catalogData).length === 0) {
return undefined;
}
const hash: crypto.Hash = crypto.createHash('sha1');
hash.update(JSON.stringify(catalogData));
return hash.digest('hex');
}
/**
* Returns hash value of injected dependencies in related package.json.
* @beta
*/
public getPackageJsonInjectedDependenciesHash(variant?: string): string | undefined {
const allPackageJson: IPackageJsonLite[] = [];
const relatedProjects: RushConfigurationProject[] = [];
const subspacePnpmfileShimSettings: ISubspacePnpmfileShimSettings =
SubspacePnpmfileConfiguration.getSubspacePnpmfileShimSettings(this._rushConfiguration, this, variant);
for (const rushProject of this.getProjects()) {
const injectedDependencies: Array<string> =
subspacePnpmfileShimSettings?.subspaceProjects[rushProject.packageName]?.injectedDependencies || [];
if (injectedDependencies.length === 0) {
continue;
}
const injectedDependencySet: Set<string> = new Set(injectedDependencies);
for (const dependencyProject of rushProject.dependencyProjects) {
if (injectedDependencySet.has(dependencyProject.packageName)) {
relatedProjects.push(dependencyProject);
}
}
}
// this means no injected dependencies found for current subspace
if (relatedProjects.length === 0) {
return undefined;
}
const allWorkspaceProjectSet: Set<string> = new Set(
this._rushConfiguration.projects.map((rushProject) => rushProject.packageName)
);
// get all related package.json
while (relatedProjects.length > 0) {
const rushProject: RushConfigurationProject = relatedProjects.pop()!;
// collect fields that could update the `pnpm-lock.yaml`
const {
name,
bin,
dependencies,
peerDependencies,
optionalDependencies,
dependenciesMeta,
peerDependenciesMeta,
resolutions
} = rushProject.packageJson;
// special handing for peerDependencies
// for workspace packages, the version range is meaningless here.
if (peerDependencies) {
for (const packageName of Object.keys(peerDependencies)) {
if (allWorkspaceProjectSet.has(packageName)) {
peerDependencies[packageName] = 'workspace:*';
}
}
}
allPackageJson.push({
name,
bin,
dependencies,
peerDependencies,
optionalDependencies,
dependenciesMeta,
peerDependenciesMeta,
resolutions
});
relatedProjects.push(...rushProject.dependencyProjects);
}
const collator: Intl.Collator = new Intl.Collator('en');
allPackageJson.sort((pa, pb) => collator.compare(pa.name, pb.name));
const hash: crypto.Hash = crypto.createHash('sha1');
for (const packageFile of allPackageJson) {
hash.update(JSON.stringify(packageFile));
}
const packageJsonInjectedDependenciesHash: string = hash.digest('hex');
return packageJsonInjectedDependenciesHash;
}
}