-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathPnpLinker.ts
More file actions
338 lines (257 loc) Β· 13.7 KB
/
Copy pathPnpLinker.ts
File metadata and controls
338 lines (257 loc) Β· 13.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
import {Installer, Linker, LinkOptions, MinimalLinkOptions, Manifest, LinkType, MessageName, DependencyMeta} from '@yarnpkg/core';
import {FetchResult, Descriptor, Ident, Locator, Package, BuildDirective, BuildType} from '@yarnpkg/core';
import {miscUtils, structUtils} from '@yarnpkg/core';
import {CwdFS, FakeFS, PortablePath, npath, ppath, toFilename, xfs} from '@yarnpkg/fslib';
import {PackageRegistry, generateInlinedScript, generateSplitScript, PnpSettings} from '@yarnpkg/pnp';
import {UsageError} from 'clipanion';
import {getPnpPath} from './index';
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 {
supportsPackage(pkg: Package, opts: MinimalLinkOptions) {
return true;
}
async findPackageLocation(locator: Locator, opts: LinkOptions) {
const pnpPath = getPnpPath(opts.project);
if (!xfs.existsSync(pnpPath))
throw new UsageError(`The project in ${opts.project.cwd}/package.json 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);
if (!xfs.existsSync(pnpPath))
throw new UsageError(`The project in ${opts.project.cwd}/package.json doesn't seem to have been installed - running an install there might help`);
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);
}
}
class PnpInstaller implements Installer {
private readonly packageRegistry: PackageRegistry = new Map();
private readonly unpluggedPaths: Set<string> = new Set();
private readonly blacklistedPaths: Set<string> = new Set();
private readonly opts: LinkOptions;
constructor(opts: LinkOptions) {
this.opts = opts;
}
async installPackage(pkg: Package, fetchResult: FetchResult) {
const key1 = structUtils.requirableIdent(pkg);
const key2 = pkg.reference;
const buildScripts = await this.getBuildScripts(fetchResult);
if (buildScripts.length > 0 && !this.opts.project.configuration.get(`enableScripts`)) {
this.opts.report.reportWarningOnce(MessageName.DISABLED_BUILD_SCRIPTS, `${structUtils.prettyLocator(this.opts.project.configuration, pkg)} lists build scripts, but all build scripts have been disabled.`);
buildScripts.length = 0;
}
if (buildScripts.length > 0 && pkg.linkType !== LinkType.HARD && !this.opts.project.tryWorkspaceByLocator(pkg)) {
this.opts.report.reportWarningOnce(MessageName.SOFT_LINK_BUILD, `${structUtils.prettyLocator(this.opts.project.configuration, pkg)} lists build scripts, but is referenced through a soft link. Soft links don't support build scripts, so they'll be ignored.`);
buildScripts.length = 0;
}
const dependencyMeta = this.opts.project.getDependencyMeta(pkg, pkg.version);
if (buildScripts.length > 0 && dependencyMeta && dependencyMeta.built === false) {
this.opts.report.reportInfoOnce(MessageName.BUILD_DISABLED, `${structUtils.prettyLocator(this.opts.project.configuration, pkg)} lists build scripts, but its build has been explicitly disabled through configuration.`);
buildScripts.length = 0;
}
const hasVirtualInstances =
pkg.peerDependencies.size > 0 &&
!structUtils.isVirtualLocator(pkg) &&
!this.opts.project.tryWorkspaceByLocator(pkg);
const packageFs = !hasVirtualInstances && pkg.linkType !== LinkType.SOFT && (buildScripts.length > 0 || this.isUnplugged(pkg, dependencyMeta))
? await this.unplugPackage(pkg, fetchResult.packageFs)
: fetchResult.packageFs;
const packageRawLocation = ppath.resolve(packageFs.getRealPath(), ppath.relative(PortablePath.root, fetchResult.prefixPath));
const packageLocation = this.normalizeDirectoryPath(packageRawLocation);
const packageDependencies = new Map<string, string | [string, string] | null>();
const packagePeers = new Set<string>();
for (const descriptor of pkg.peerDependencies.values()) {
packageDependencies.set(structUtils.requirableIdent(descriptor), null);
packagePeers.add(descriptor.name);
}
const packageStore = this.getPackageStore(key1);
packageStore.set(key2, {packageLocation, packageDependencies, packagePeers, linkType: pkg.linkType});
if (hasVirtualInstances)
this.blacklistedPaths.add(packageLocation);
return {
packageLocation,
buildDirective: buildScripts.length > 0 ? buildScripts as BuildDirective[] : 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() {
if (await this.shouldWarnNodeModules())
this.opts.report.reportWarning(MessageName.DANGEROUS_NODE_MODULES, `One or more node_modules have been detected; they risk hiding legitimate problems until your application reaches production.`);
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 ignorePattern = this.opts.project.configuration.get(`pnpIgnorePattern`);
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});
const pnpPath = getPnpPath(this.opts.project);
const pnpDataPath = this.opts.project.configuration.get(`pnpDataPath`);
const pnpSettings: PnpSettings = {
blacklistedLocations,
dependencyTreeRoots,
enableTopLevelFallback,
fallbackExclusionList,
ignorePattern,
packageRegistry,
shebang,
};
if (this.opts.project.configuration.get(`pnpEnableInlining`)) {
const loaderFile = generateInlinedScript(pnpSettings);
await xfs.changeFilePromise(pnpPath, loaderFile);
await xfs.chmodPromise(pnpPath, 0o755);
await xfs.removePromise(pnpDataPath);
} else {
const dataLocation = ppath.relative(ppath.dirname(pnpPath), pnpDataPath);
const {dataFile, loaderFile} = generateSplitScript({...pnpSettings, dataLocation});
await xfs.changeFilePromise(pnpPath, loaderFile);
await xfs.chmodPromise(pnpPath, 0o755);
await xfs.changeFilePromise(pnpDataPath, dataFile);
await xfs.chmodPromise(pnpDataPath, 0o644);
}
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 getPackageStore(key: string) {
let packageStore = this.packageRegistry.get(key);
if (!packageStore)
this.packageRegistry.set(key, packageStore = new Map());
return packageStore;
}
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 = this.getPackageStore(`@@disk`);
const normalizedPath = this.normalizeDirectoryPath(path);
let diskInformation = packageStore.get(normalizedPath);
if (!diskInformation) {
packageStore.set(normalizedPath, diskInformation = {
packageLocation: normalizedPath,
packageDependencies: new Map(),
packagePeers: new Set(),
linkType: LinkType.SOFT,
});
}
return diskInformation;
}
private trimBlacklistedPackages() {
for (const packageStore of this.packageRegistry.values()) {
for (const [key2, packageInformation] of packageStore) {
if (this.blacklistedPaths.has(packageInformation.packageLocation)) {
packageStore.delete(key2);
}
}
}
}
private async shouldWarnNodeModules() {
for (const workspace of this.opts.project.workspaces) {
const nodeModulesPath = ppath.join(workspace.cwd, toFilename(`node_modules`));
if (!xfs.existsSync(nodeModulesPath))
continue;
const directoryListing = await xfs.readdirPromise(nodeModulesPath);
if (directoryListing.every(entry => entry.startsWith(`.`)))
continue;
return true;
}
return false;
}
private normalizeDirectoryPath(folder: PortablePath) {
let relativeFolder = ppath.relative(this.opts.project.cwd, folder);
if (!relativeFolder.match(/^\.{0,2}\//))
// Don't use ppath.join here, it ignores the `.`
relativeFolder = `./${relativeFolder}` as PortablePath;
return relativeFolder.replace(/\/?$/, '/') as PortablePath;
}
private async getBuildScripts(fetchResult: FetchResult) {
if (!await fetchResult.packageFs.existsPromise(ppath.resolve(fetchResult.prefixPath, toFilename(`package.json`))))
return [];
const buildScripts = [];
const {scripts} = await Manifest.find(fetchResult.prefixPath, {baseFs: fetchResult.packageFs});
for (const scriptName of [`preinstall`, `install`, `postinstall`])
if (scripts.has(scriptName))
buildScripts.push([BuildType.SCRIPT, scriptName]);
// Detect cases where a package has a binding.gyp but no install script
const bindingFilePath = ppath.resolve(fetchResult.prefixPath, toFilename(`binding.gyp`));
if (!scripts.has(`install`) && fetchResult.packageFs.existsSync(bindingFilePath))
buildScripts.push([BuildType.SHELLCODE, `node-gyp rebuild`]);
return buildScripts;
}
private getUnpluggedPath(locator: Locator) {
return ppath.resolve(this.opts.project.configuration.get(`pnpUnpluggedFolder`), structUtils.slugifyLocator(locator));
}
private async unplugPackage(locator: Locator, packageFs: FakeFS<PortablePath>) {
const unplugPath = this.getUnpluggedPath(locator);
this.unpluggedPaths.add(unplugPath);
await xfs.mkdirpPromise(unplugPath);
await xfs.copyPromise(unplugPath, PortablePath.dot, {baseFs: packageFs, overwrite: false});
return new CwdFS(unplugPath);
}
private isUnplugged(ident: Ident, dependencyMeta: DependencyMeta) {
if (dependencyMeta.unplugged)
return true;
if (FORCED_UNPLUG_PACKAGES.has(ident.identHash))
return true;
return false;
}
}