-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PnpLinker.ts
484 lines (379 loc) Β· 18.3 KB
/
PnpLinker.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
import {miscUtils, structUtils, formatUtils, Descriptor, LocatorHash} from '@yarnpkg/core';
import {FetchResult, Locator, Package} from '@yarnpkg/core';
import {Linker, LinkOptions, MinimalLinkOptions, Manifest, MessageName, DependencyMeta, LinkType, Installer} from '@yarnpkg/core';
import {CwdFS, PortablePath, npath, ppath, xfs, Filename} from '@yarnpkg/fslib';
import {generateInlinedScript, generateSplitScript, PackageRegistry, PnpSettings} from '@yarnpkg/pnp';
import {UsageError} from 'clipanion';
import {getPnpPath} from './index';
import * as jsInstallUtils from './jsInstallUtils';
import * as pnpUtils from './pnpUtils';
const FORCED_UNPLUG_PACKAGES = new Set([
// Some packages do weird stuff and MUST be unplugged. I don't like them.
structUtils.makeIdent(null, `nan`).identHash,
structUtils.makeIdent(null, `node-gyp`).identHash,
structUtils.makeIdent(null, `node-pre-gyp`).identHash,
structUtils.makeIdent(null, `node-addon-api`).identHash,
// Those ones contain native builds (*.node), and Node loads them through dlopen
structUtils.makeIdent(null, `fsevents`).identHash,
]);
export class PnpLinker implements Linker {
protected mode = `strict`;
supportsPackage(pkg: Package, opts: MinimalLinkOptions) {
if (opts.project.configuration.get(`nodeLinker`) !== `pnp`)
return false;
if (opts.project.configuration.get(`pnpMode`) !== this.mode)
return false;
return true;
}
async findPackageLocation(locator: Locator, opts: LinkOptions) {
const pnpPath = getPnpPath(opts.project).main;
if (!xfs.existsSync(pnpPath))
throw new UsageError(`The project in ${formatUtils.pretty(opts.project.configuration, `${opts.project.cwd}/package.json`, formatUtils.Type.PATH)} doesn't seem to have been installed - running an install there might help`);
const pnpFile = miscUtils.dynamicRequireNoCache(pnpPath);
const packageLocator = {name: structUtils.requirableIdent(locator), reference: locator.reference};
const packageInformation = pnpFile.getPackageInformation(packageLocator);
if (!packageInformation)
throw new UsageError(`Couldn't find ${structUtils.prettyLocator(opts.project.configuration, locator)} in the currently installed PnP map - running an install might help`);
return npath.toPortablePath(packageInformation.packageLocation);
}
async findPackageLocator(location: PortablePath, opts: LinkOptions) {
const pnpPath = getPnpPath(opts.project).main;
if (!xfs.existsSync(pnpPath))
return null;
const physicalPath = npath.fromPortablePath(pnpPath);
const pnpFile = miscUtils.dynamicRequire(physicalPath);
delete require.cache[physicalPath];
const locator = pnpFile.findPackageLocator(npath.fromPortablePath(location));
if (!locator)
return null;
return structUtils.makeLocator(structUtils.parseIdent(locator.name), locator.reference);
}
makeInstaller(opts: LinkOptions) {
return new PnpInstaller(opts);
}
}
export class PnpInstaller implements Installer {
protected mode = `strict`;
private readonly packageRegistry: PackageRegistry = new Map();
private readonly blacklistedPaths: Set<PortablePath> = new Set();
constructor(protected opts: LinkOptions) {
this.opts = opts;
}
getCustomDataKey() {
return JSON.stringify({
name: `PnpInstaller`,
version: 1,
});
}
private customData: {
store: Map<LocatorHash, CustomPackageData>,
} = {
store: new Map(),
};
attachCustomData(customData: any) {
this.customData = customData;
}
async installPackage(pkg: Package, fetchResult: FetchResult) {
const key1 = structUtils.requirableIdent(pkg);
const key2 = pkg.reference;
const isWorkspace =
!!this.opts.project.tryWorkspaceByLocator(pkg);
const hasVirtualInstances =
// Only packages with peer dependencies have virtual instances
pkg.peerDependencies.size > 0 &&
// Only packages with peer dependencies have virtual instances
!structUtils.isVirtualLocator(pkg);
const mayNeedToBeBuilt =
// Virtual instance templates don't need to be built, since they don't truly exist
!hasVirtualInstances &&
// Workspaces aren't built by the linkers; they are managed by the core itself
!isWorkspace;
const mayNeedToBeUnplugged =
// Virtual instance templates don't need to be unplugged, since they don't truly exist
!hasVirtualInstances &&
// We never need to unplug soft links, since we don't control them
pkg.linkType !== LinkType.SOFT;
let customPackageData = this.customData.store.get(pkg.locatorHash);
if (typeof customPackageData === `undefined`) {
customPackageData = await extractCustomPackageData(pkg, fetchResult);
if (pkg.linkType === LinkType.HARD) {
this.customData.store.set(pkg.locatorHash, customPackageData);
}
}
const dependencyMeta = this.opts.project.getDependencyMeta(pkg, pkg.version);
const buildScripts = mayNeedToBeBuilt
? jsInstallUtils.extractBuildScripts(pkg, customPackageData, dependencyMeta, {configuration: this.opts.project.configuration, report: this.opts.report})
: [];
const packageFs = mayNeedToBeUnplugged
? await this.unplugPackageIfNeeded(pkg, customPackageData, fetchResult, dependencyMeta)
: fetchResult.packageFs;
if (ppath.isAbsolute(fetchResult.prefixPath))
throw new Error(`Assertion failed: Expected the prefix path (${fetchResult.prefixPath}) to be relative to the parent`);
const packageRawLocation = ppath.resolve(packageFs.getRealPath(), fetchResult.prefixPath);
const packageLocation = normalizeDirectoryPath(this.opts.project.cwd, packageRawLocation);
const packageDependencies = new Map<string, string | [string, string] | null>();
const packagePeers = new Set<string>();
// Only virtual packages should have effective peer dependencies, but the
// workspaces are a special case because the original packages are kept in
// the dependency tree even after being virtualized; so in their case we
// just ignore their declared peer dependencies.
if (structUtils.isVirtualLocator(pkg)) {
for (const descriptor of pkg.peerDependencies.values()) {
packageDependencies.set(structUtils.requirableIdent(descriptor), null);
packagePeers.add(structUtils.stringifyIdent(descriptor));
}
}
miscUtils.getMapWithDefault(this.packageRegistry, key1).set(key2, {
packageLocation,
packageDependencies,
packagePeers,
linkType: pkg.linkType,
discardFromLookup: fetchResult.discardFromLookup || false,
});
if (hasVirtualInstances && !isWorkspace)
this.blacklistedPaths.add(packageLocation);
return {
packageLocation: packageRawLocation,
buildDirective: buildScripts.length > 0 ? buildScripts : null,
};
}
async attachInternalDependencies(locator: Locator, dependencies: Array<[Descriptor, Locator]>) {
const packageInformation = this.getPackageInformation(locator);
for (const [descriptor, locator] of dependencies) {
const target = !structUtils.areIdentsEqual(descriptor, locator)
? [structUtils.requirableIdent(locator), locator.reference] as [string, string]
: locator.reference;
packageInformation.packageDependencies.set(structUtils.requirableIdent(descriptor), target);
}
}
async attachExternalDependents(locator: Locator, dependentPaths: Array<PortablePath>) {
for (const dependentPath of dependentPaths) {
const packageInformation = this.getDiskInformation(dependentPath);
packageInformation.packageDependencies.set(structUtils.requirableIdent(locator), locator.reference);
}
}
async finalizeInstall() {
this.trimBlacklistedPackages();
this.packageRegistry.set(null, new Map([
[null, this.getPackageInformation(this.opts.project.topLevelWorkspace.anchoredLocator)],
]));
const pnpFallbackMode = this.opts.project.configuration.get(`pnpFallbackMode`);
const blacklistedLocations = this.blacklistedPaths;
const dependencyTreeRoots = this.opts.project.workspaces.map(({anchoredLocator}) => ({name: structUtils.requirableIdent(anchoredLocator), reference: anchoredLocator.reference}));
const enableTopLevelFallback = pnpFallbackMode !== `none`;
const fallbackExclusionList = [];
const fallbackPool = new Map();
const ignorePattern = miscUtils.buildIgnorePattern([`.yarn/sdks/**`, ...this.opts.project.configuration.get(`pnpIgnorePatterns`)]);
const packageRegistry = this.packageRegistry;
const shebang = this.opts.project.configuration.get(`pnpShebang`);
if (pnpFallbackMode === `dependencies-only`)
for (const pkg of this.opts.project.storedPackages.values())
if (this.opts.project.tryWorkspaceByLocator(pkg))
fallbackExclusionList.push({name: structUtils.requirableIdent(pkg), reference: pkg.reference});
await this.finalizeInstallWithPnp({
blacklistedLocations,
dependencyTreeRoots,
enableTopLevelFallback,
fallbackExclusionList,
fallbackPool,
ignorePattern,
packageRegistry,
shebang,
});
return {
customData: this.customData,
};
}
async finalizeInstallWithPnp(pnpSettings: PnpSettings) {
if (this.opts.project.configuration.get(`pnpMode`) !== this.mode)
return;
const pnpPath = getPnpPath(this.opts.project);
const pnpDataPath = this.opts.project.configuration.get(`pnpDataPath`);
await xfs.removePromise(pnpPath.other);
if (this.opts.project.topLevelWorkspace.manifest.type !== `module`)
await xfs.removePromise(pnpPath.esmLoader);
if (this.opts.project.configuration.get(`nodeLinker`) !== `pnp`) {
await xfs.removePromise(pnpPath.main);
await xfs.removePromise(pnpDataPath);
await xfs.removePromise(pnpPath.esmLoader);
return;
}
const nodeModules = await this.locateNodeModules(pnpSettings.ignorePattern);
if (nodeModules.length > 0) {
this.opts.report.reportWarning(MessageName.DANGEROUS_NODE_MODULES, `One or more node_modules have been detected and will be removed. This operation may take some time.`);
for (const nodeModulesPath of nodeModules) {
await xfs.removePromise(nodeModulesPath);
}
}
if (this.opts.project.configuration.get(`pnpEnableInlining`)) {
const loaderFile = generateInlinedScript(pnpSettings);
await xfs.changeFilePromise(pnpPath.main, loaderFile, {automaticNewlines: true});
await xfs.chmodPromise(pnpPath.main, 0o755);
await xfs.removePromise(pnpDataPath);
} else {
const dataLocation = ppath.relative(ppath.dirname(pnpPath.main), pnpDataPath);
const {dataFile, loaderFile} = generateSplitScript({...pnpSettings, dataLocation});
await xfs.changeFilePromise(pnpPath.main, loaderFile, {automaticNewlines: true});
await xfs.chmodPromise(pnpPath.main, 0o755);
await xfs.changeFilePromise(pnpDataPath, dataFile, {automaticNewlines: true});
await xfs.chmodPromise(pnpDataPath, 0o644);
}
if (pnpPath.main.endsWith(`.cjs`)) {
await xfs.writeFilePromise(pnpPath.esmLoader, `import { syncBuiltinESMExports, createRequire, builtinModules } from 'module';
import { fileURLToPath, pathToFileURL, URL } from 'url';
syncBuiltinESMExports();
const pnpapi = createRequire(import.meta.url)('pnpapi');
function isValidURL(str) {
try {
new URL(str);
return true;
} catch {
return false;
}
}
const builtins = new Set([...builtinModules]);
export async function resolve(specifier, context, defaultResolver) {
if (builtins.has(specifier) || isValidURL(specifier)) {
return defaultResolver(specifier, context, defaultResolver);
}
const { parentURL = null } = context;
const resolvedPath = pnpapi.resolveRequest(
specifier,
parentURL ? fileURLToPath(parentURL) : undefined
);
if (!resolvedPath) {
throw new Error(
\`Assertion failed: resolveRequest returned a falsy value for '\${specifier}' from '\${parentURL}'\`
);
}
return {
url: pathToFileURL(resolvedPath).href,
};
}
`);
}
const pnpUnpluggedFolder = this.opts.project.configuration.get(`pnpUnpluggedFolder`);
if (this.unpluggedPaths.size === 0) {
await xfs.removePromise(pnpUnpluggedFolder);
} else {
for (const entry of await xfs.readdirPromise(pnpUnpluggedFolder)) {
const unpluggedPath = ppath.resolve(pnpUnpluggedFolder, entry);
if (!this.unpluggedPaths.has(unpluggedPath)) {
await xfs.removePromise(unpluggedPath);
}
}
}
}
private async locateNodeModules(ignorePattern?: string | null) {
const nodeModules: Array<PortablePath> = [];
const ignoreRegExp = ignorePattern ? new RegExp(ignorePattern) : null;
for (const workspace of this.opts.project.workspaces) {
const nodeModulesPath = ppath.join(workspace.cwd, `node_modules` as Filename);
if (ignoreRegExp && ignoreRegExp.test(ppath.relative(this.opts.project.cwd, workspace.cwd)) || !xfs.existsSync(nodeModulesPath))
continue;
const directoryListing = await xfs.readdirPromise(nodeModulesPath, {
withFileTypes: true,
});
const nonCacheEntries = directoryListing.filter(entry => {
return !entry.isDirectory() || entry.name === `.bin` || !entry.name.startsWith(`.`);
});
if (nonCacheEntries.length === directoryListing.length) {
nodeModules.push(nodeModulesPath);
} else {
for (const entry of nonCacheEntries) {
nodeModules.push(ppath.join(nodeModulesPath, entry.name));
}
}
}
return nodeModules;
}
private readonly unpluggedPaths: Set<string> = new Set();
private async unplugPackageIfNeeded(pkg: Package, customPackageData: CustomPackageData, fetchResult: FetchResult, dependencyMeta: DependencyMeta) {
if (this.shouldBeUnplugged(pkg, customPackageData, dependencyMeta)) {
return this.unplugPackage(pkg, fetchResult);
} else {
return fetchResult.packageFs;
}
}
private shouldBeUnplugged(pkg: Package, customPackageData: CustomPackageData, dependencyMeta: DependencyMeta) {
if (typeof dependencyMeta.unplugged !== `undefined`)
return dependencyMeta.unplugged;
if (FORCED_UNPLUG_PACKAGES.has(pkg.identHash))
return true;
if (customPackageData.manifest.preferUnplugged !== null)
return customPackageData.manifest.preferUnplugged;
if (jsInstallUtils.extractBuildScripts(pkg, customPackageData, dependencyMeta, {configuration: this.opts.project.configuration}).length > 0 || customPackageData.misc.extractHint)
return true;
return false;
}
private async unplugPackage(locator: Locator, fetchResult: FetchResult) {
const unplugPath = pnpUtils.getUnpluggedPath(locator, {configuration: this.opts.project.configuration});
this.unpluggedPaths.add(unplugPath);
const readyFile = ppath.join(unplugPath, fetchResult.prefixPath, `.ready` as Filename);
if (await xfs.existsPromise(readyFile))
return new CwdFS(unplugPath);
await xfs.mkdirPromise(unplugPath, {recursive: true});
await xfs.copyPromise(unplugPath, PortablePath.dot, {baseFs: fetchResult.packageFs, overwrite: false});
await xfs.writeFilePromise(readyFile, ``);
return new CwdFS(unplugPath);
}
private getPackageInformation(locator: Locator) {
const key1 = structUtils.requirableIdent(locator);
const key2 = locator.reference;
const packageInformationStore = this.packageRegistry.get(key1);
if (!packageInformationStore)
throw new Error(`Assertion failed: The package information store should have been available (for ${structUtils.prettyIdent(this.opts.project.configuration, locator)})`);
const packageInformation = packageInformationStore.get(key2);
if (!packageInformation)
throw new Error(`Assertion failed: The package information should have been available (for ${structUtils.prettyLocator(this.opts.project.configuration, locator)})`);
return packageInformation;
}
private getDiskInformation(path: PortablePath) {
const packageStore = miscUtils.getMapWithDefault(this.packageRegistry, `@@disk`);
const normalizedPath = normalizeDirectoryPath(this.opts.project.cwd, path);
return miscUtils.getFactoryWithDefault(packageStore, normalizedPath, () => ({
packageLocation: normalizedPath,
packageDependencies: new Map(),
packagePeers: new Set<string>(),
linkType: LinkType.SOFT,
discardFromLookup: false,
}));
}
private trimBlacklistedPackages() {
for (const packageStore of this.packageRegistry.values()) {
for (const [key2, packageInformation] of packageStore) {
if (packageInformation.packageLocation && this.blacklistedPaths.has(packageInformation.packageLocation)) {
packageStore.delete(key2);
}
}
}
}
}
function normalizeDirectoryPath(root: PortablePath, folder: PortablePath) {
let relativeFolder = ppath.relative(root, folder);
if (!relativeFolder.match(/^\.{0,2}\//))
// Don't use ppath.join here, it ignores the `.`
relativeFolder = `./${relativeFolder}` as PortablePath;
return relativeFolder.replace(/\/?$/, `/`) as PortablePath;
}
type UnboxPromise<T extends Promise<any>> = T extends Promise<infer U> ? U: never;
type CustomPackageData = UnboxPromise<ReturnType<typeof extractCustomPackageData>>;
async function extractCustomPackageData(pkg: Package, fetchResult: FetchResult) {
const manifest = await Manifest.tryFind(fetchResult.prefixPath, {baseFs: fetchResult.packageFs}) ?? new Manifest();
const preservedScripts = new Set([`preinstall`, `install`, `postinstall`]);
for (const scriptName of manifest.scripts.keys())
if (!preservedScripts.has(scriptName))
manifest.scripts.delete(scriptName);
return {
manifest: {
os: manifest.os,
cpu: manifest.cpu,
scripts: manifest.scripts,
preferUnplugged: manifest.preferUnplugged,
},
misc: {
extractHint: jsInstallUtils.getExtractHint(fetchResult),
hasBindingGyp: jsInstallUtils.hasBindingGyp(fetchResult),
},
};
}