-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathafterInstallAsync.ts
More file actions
306 lines (270 loc) · 11.2 KB
/
afterInstallAsync.ts
File metadata and controls
306 lines (270 loc) · 11.2 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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type {
RushSession,
RushConfiguration,
RushConfigurationProject,
ILogger,
LookupByPath,
Subspace
} from '@rushstack/rush-sdk';
import type { IResolverCacheFile } from '@rushstack/webpack-workspace-resolve-plugin';
import { Async, FileSystem, PnpmShrinkwrapFile } from './externals';
import {
computeResolverCacheFromLockfileAsync,
type IPlatformInfo
} from './computeResolverCacheFromLockfileAsync';
import {
type PnpmMajorVersion,
type IPnpmVersionHelpers,
getPnpmVersionHelpersAsync
} from './pnpm/pnpmVersionHelpers';
import type { IResolverContext } from './types';
/**
* Gets information used to determine compatibility of optional dependencies.
* @returns Information about the platform Rush is running on
*/
function getPlatformInfo(): IPlatformInfo {
// Acquiring the libc version is a bit more obnoxious than platform and arch,
// but all of them are ultimately on the same object.
const {
platform: os,
arch: cpu,
glibcVersionRuntime
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} = (process.report?.getReport() as any)?.header ?? process;
const libc: 'glibc' | 'musl' = glibcVersionRuntime ? 'glibc' : 'musl';
return {
os,
cpu,
libc
};
}
const END_TOKEN: string = '/package.json":';
const RESOLVER_CACHE_FILE_VERSION: 2 = 2;
interface IExtendedResolverCacheFile extends IResolverCacheFile {
/**
* The hash of the shrinkwrap file this cache file was generated from.
*/
shrinkwrapHash: string;
/**
* The version of the resolver cache file.
*/
version: number;
}
interface INestedPackageJsonCache {
subPackagesByIntegrity: [string, string[] | boolean][];
version: number;
}
/**
* Plugin entry point for after install.
* @param rushSession - The Rush Session
* @param rushConfiguration - The Rush Configuration
* @param subspace - The subspace that was just installed
* @param variant - The variant that was just installed
* @param logger - The initialized logger
*/
export async function afterInstallAsync(
rushSession: RushSession,
rushConfiguration: RushConfiguration,
subspace: Subspace,
variant: string | undefined,
logger: ILogger
): Promise<void> {
const { terminal } = logger;
const rushRoot: string = `${rushConfiguration.rushJsonFolder}/`;
const lockFilePath: string = subspace.getCommittedShrinkwrapFilePath(variant);
const pnpmStorePath: string = rushConfiguration.pnpmOptions.pnpmStorePath;
const pnpmMajorVersion: PnpmMajorVersion = (() => {
const major: number = parseInt(rushConfiguration.packageManagerToolVersion, 10);
switch (major) {
case 10:
return 10;
case 9:
return 9;
case 8:
return 8;
default:
throw new Error(`Unsupported pnpm major version: ${major}`);
}
})();
const pnpmHelpers: IPnpmVersionHelpers = await getPnpmVersionHelpersAsync(pnpmMajorVersion);
terminal.writeLine(`Using pnpm-lock from: ${lockFilePath}`);
terminal.writeLine(`Using pnpm ${pnpmMajorVersion} store at: ${pnpmStorePath}`);
const workspaceRoot: string = subspace.getSubspaceTempFolderPath();
const cacheFilePath: string = `${workspaceRoot}/resolver-cache.json`;
const lockFile: PnpmShrinkwrapFile | undefined = PnpmShrinkwrapFile.loadFromFile(lockFilePath, {
withCaching: true,
subspaceHasNoProjects: subspace.getProjects().length === 0
});
if (!lockFile) {
throw new Error(`Failed to load shrinkwrap file: ${lockFilePath}`);
}
if (!lockFile.hash) {
throw new Error(
`Shrinkwrap file does not have a hash. This indicates linking to an old version of Rush.`
);
}
try {
const oldCacheFileContent: string = await FileSystem.readFileAsync(cacheFilePath);
const oldCache: IExtendedResolverCacheFile = JSON.parse(oldCacheFileContent);
if (oldCache.version === RESOLVER_CACHE_FILE_VERSION && oldCache.shrinkwrapHash === lockFile.hash) {
// Cache is valid, use it
return;
}
} catch (err) {
// Ignore
}
const projectByImporterPath: LookupByPath<RushConfigurationProject> =
rushConfiguration.getProjectLookupForRoot(workspaceRoot);
const subPackageCacheFilePath: string = `${workspaceRoot}/subpackage-entry-cache.json`;
terminal.writeLine(`Resolver cache will be written at ${cacheFilePath}`);
let oldSubPackagesByIntegrity: Map<string, string[] | boolean> | undefined;
const subPackagesByIntegrity: Map<string, string[] | boolean> = new Map();
try {
const cacheContent: string = await FileSystem.readFileAsync(subPackageCacheFilePath);
const cacheJson: INestedPackageJsonCache = JSON.parse(cacheContent);
if (cacheJson.version !== RESOLVER_CACHE_FILE_VERSION) {
terminal.writeLine(
`Expected subpackage cache version ${RESOLVER_CACHE_FILE_VERSION}, got ${cacheJson.version}`
);
} else {
oldSubPackagesByIntegrity = new Map(cacheJson.subPackagesByIntegrity);
terminal.writeLine(`Loaded subpackage cache from ${subPackageCacheFilePath}`);
}
} catch (err) {
// Ignore
}
async function afterExternalPackagesAsync(
contexts: Map<string, IResolverContext>,
missingOptionalDependencies: Set<string>
): Promise<void> {
/**
* Loads the index file from the pnpm store to discover nested package.json files in an external package
* For internal packages, assumes there are no nested package.json files.
* @param context - The context to find nested package.json files for
* @returns A promise that resolves to the nested package.json paths, false if the package fails to load, or true if the package has no nested package.json files.
*/
async function tryFindNestedPackageJsonsForContextAsync(
context: IResolverContext
): Promise<string[] | boolean> {
const { descriptionFileRoot, descriptionFileHash } = context;
if (descriptionFileHash === undefined) {
// Assume this package has no nested package json files for now.
terminal.writeDebugLine(
`Package at ${descriptionFileRoot} does not have a file list. Assuming no nested "package.json" files.`
);
return true;
}
// Convert an integrity hash like
// sha512-C6uiGQJ+Gt4RyHXXYt+v9f+SN1v83x68URwgxNQ98cvH8kxiuywWGP4XeNZ1paOzZ63aY3cTciCEQJNFUljlLw==
// To its hex representation, e.g.
// 0baba219027e1ade11c875d762dfaff5ff92375bfcdf1ebc511c20c4d43df1cbc7f24c62bb2c1618fe1778d675a5a3b367adda6377137220844093455258e52f
const prefixIndex: number = descriptionFileHash.indexOf('-');
const hash: string = Buffer.from(descriptionFileHash.slice(prefixIndex + 1), 'base64').toString('hex');
const indexPath: string = pnpmHelpers.getStoreIndexPath(pnpmStorePath, context, hash);
try {
const indexContent: string = await FileSystem.readFileAsync(indexPath);
let endIndex: number = indexContent.lastIndexOf(END_TOKEN);
if (endIndex > 0) {
const nestedPackageDirs: string[] = [];
do {
const startIndex: number = indexContent.lastIndexOf('"', endIndex);
if (startIndex < 0) {
throw new Error(
`Malformed index file at ${indexPath}: missing starting quote for nested package.json path`
);
}
const nestedPath: string = indexContent.slice(startIndex + 1, endIndex);
nestedPackageDirs.push(nestedPath);
endIndex = indexContent.lastIndexOf(END_TOKEN, startIndex - 1);
} while (endIndex > 0);
return nestedPackageDirs;
}
return true;
} catch (error) {
if (!context.optional) {
throw new Error(
`Error reading index file for: "${
context.descriptionFileRoot
}" (${descriptionFileHash}): ${error.toString()}`
);
}
return false;
}
}
/**
* Loads the index file from the pnpm store to discover nested package.json files in an external package
* For internal packages, assumes there are no nested package.json files.
* @param context - The context to find nested package.json files for
* @returns A promise that resolves when the nested package.json files are found, if applicable
*/
async function findNestedPackageJsonsForContextAsync(context: IResolverContext): Promise<void> {
const { descriptionFileRoot, descriptionFileHash } = context;
if (descriptionFileHash === undefined) {
// Assume this package has no nested package json files for now.
terminal.writeDebugLine(
`Package at ${descriptionFileRoot} does not have a file list. Assuming no nested "package.json" files.`
);
return;
}
let result: string[] | boolean | undefined =
oldSubPackagesByIntegrity?.get(descriptionFileHash) ??
subPackagesByIntegrity.get(descriptionFileHash);
if (result === undefined) {
result = await tryFindNestedPackageJsonsForContextAsync(context);
}
subPackagesByIntegrity.set(descriptionFileHash, result);
if (result === true) {
// Default case. Do nothing.
} else if (result === false) {
terminal.writeLine(`Trimming missing optional dependency at: ${descriptionFileRoot}`);
contexts.delete(descriptionFileRoot);
missingOptionalDependencies.add(descriptionFileRoot);
} else {
terminal.writeDebugLine(
`Nested "package.json" files found for package at ${descriptionFileRoot}: ${result.join(', ')}`
);
// eslint-disable-next-line require-atomic-updates
context.nestedPackageDirs = result;
}
}
// For external packages, update the contexts with data from the pnpm store
// This gives us the list of nested package.json files, as well as the actual package name
// We could also cache package.json contents, but that proves to be inefficient.
await Async.forEachAsync(contexts.values(), findNestedPackageJsonsForContextAsync, {
concurrency: 20
});
}
const rawCacheFile: IResolverCacheFile = await computeResolverCacheFromLockfileAsync({
workspaceRoot,
commonPrefixToTrim: rushRoot,
platformInfo: getPlatformInfo(),
projectByImporterPath,
lockfile: lockFile,
pnpmVersion: pnpmMajorVersion,
afterExternalPackagesAsync
});
const extendedCacheFile: IExtendedResolverCacheFile = {
version: RESOLVER_CACHE_FILE_VERSION,
shrinkwrapHash: lockFile.hash,
...rawCacheFile
};
const newSubPackageCache: INestedPackageJsonCache = {
version: RESOLVER_CACHE_FILE_VERSION,
subPackagesByIntegrity: Array.from(subPackagesByIntegrity)
};
const serializedSubpackageCache: string = JSON.stringify(newSubPackageCache);
const serialized: string = JSON.stringify(extendedCacheFile);
await Promise.all([
FileSystem.writeFileAsync(cacheFilePath, serialized, {
ensureFolderExists: true
}),
FileSystem.writeFileAsync(subPackageCacheFilePath, serializedSubpackageCache, {
ensureFolderExists: true
})
]);
// Free the memory used by the lockfiles, since nothing should read the lockfile from this point on.
PnpmShrinkwrapFile.clearCache();
terminal.writeLine(`Resolver cache written.`);
}