-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
package-linker.js
493 lines (430 loc) · 15.9 KB
/
package-linker.js
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
/* @flow */
import type {Manifest} from './types.js';
import type PackageResolver from './package-resolver.js';
import type {Reporter} from './reporters/index.js';
import type Config from './config.js';
import type {HoistManifestTuples} from './package-hoister.js';
import type {CopyQueueItem} from './util/fs.js';
import type {InstallArtifacts} from './package-install-scripts.js';
import PackageHoister from './package-hoister.js';
import * as constants from './constants.js';
import * as promise from './util/promise.js';
import {entries} from './util/misc.js';
import * as fs from './util/fs.js';
import lockMutex from './util/mutex.js';
import {satisfiesWithPreleases} from './util/semver.js';
import WorkspaceLayout from './workspace-layout.js';
const invariant = require('invariant');
const cmdShim = promise.promisify(require('cmd-shim'));
const path = require('path');
// Concurrency for creating bin links disabled because of the issue #1961
const linkBinConcurrency = 1;
type DependencyPairs = Array<{
dep: Manifest,
loc: string,
}>;
export async function linkBin(src: string, dest: string): Promise<void> {
if (process.platform === 'win32') {
const unlockMutex = await lockMutex(src);
try {
await cmdShim(src, dest);
} finally {
unlockMutex();
}
} else {
await fs.mkdirp(path.dirname(dest));
await fs.symlink(src, dest);
await fs.chmod(dest, '755');
}
}
export default class PackageLinker {
constructor(config: Config, resolver: PackageResolver) {
this.resolver = resolver;
this.reporter = config.reporter;
this.config = config;
this.artifacts = {};
this.topLevelBinLinking = true;
}
artifacts: InstallArtifacts;
reporter: Reporter;
resolver: PackageResolver;
config: Config;
topLevelBinLinking: boolean;
setArtifacts(artifacts: InstallArtifacts) {
this.artifacts = artifacts;
}
setTopLevelBinLinking(topLevelBinLinking: boolean) {
this.topLevelBinLinking = topLevelBinLinking;
}
async linkSelfDependencies(pkg: Manifest, pkgLoc: string, targetBinLoc: string): Promise<void> {
targetBinLoc = path.join(targetBinLoc, '.bin');
await fs.mkdirp(targetBinLoc);
targetBinLoc = await fs.realpath(targetBinLoc);
pkgLoc = await fs.realpath(pkgLoc);
for (const [scriptName, scriptCmd] of entries(pkg.bin)) {
const dest = path.join(targetBinLoc, scriptName);
const src = path.join(pkgLoc, scriptCmd);
if (!await fs.exists(src)) {
// TODO maybe throw an error
continue;
}
await linkBin(src, dest);
}
}
async linkBinDependencies(pkg: Manifest, dir: string): Promise<void> {
const deps: DependencyPairs = [];
const ref = pkg._reference;
invariant(ref, 'Package reference is missing');
const remote = pkg._remote;
invariant(remote, 'Package remote is missing');
// link up `bin scripts` in `dependencies`
for (const pattern of ref.dependencies) {
const dep = this.resolver.getStrictResolvedPattern(pattern);
if (
// Missing location means not installed inside node_modules
dep._reference &&
dep._reference.location &&
dep.bin &&
Object.keys(dep.bin).length
) {
deps.push({
dep,
loc: this.config.generateHardModulePath(dep._reference),
});
}
}
// link up the `bin` scripts in bundled dependencies
if (pkg.bundleDependencies) {
for (const depName of pkg.bundleDependencies) {
const loc = path.join(this.config.generateHardModulePath(ref), this.config.getFolder(pkg), depName);
try {
const dep = await this.config.readManifest(loc, remote.registry);
if (dep.bin && Object.keys(dep.bin).length) {
deps.push({dep, loc});
}
} catch (ex) {
if (ex.code !== 'ENOENT') {
throw ex;
}
// intentionally ignoring ENOENT error.
// bundledDependency either does not exist or does not contain a package.json
}
}
}
// no deps to link
if (!deps.length) {
return;
}
// write the executables
for (const {dep, loc} of deps) {
if (dep._reference && dep._reference.location) {
await this.linkSelfDependencies(dep, loc, dir);
}
}
}
getFlatHoistedTree(patterns: Array<string>, {ignoreOptional}: {ignoreOptional: ?boolean} = {}): HoistManifestTuples {
const hoister = new PackageHoister(this.config, this.resolver, {ignoreOptional});
hoister.seed(patterns);
return hoister.init();
}
async copyModules(
patterns: Array<string>,
workspaceLayout?: WorkspaceLayout,
{linkDuplicates, ignoreOptional}: {linkDuplicates: ?boolean, ignoreOptional: ?boolean} = {},
): Promise<void> {
let flatTree = this.getFlatHoistedTree(patterns, {ignoreOptional});
// sorted tree makes file creation and copying not to interfere with each other
flatTree = flatTree.sort(function(dep1, dep2): number {
return dep1[0].localeCompare(dep2[0]);
});
// list of artifacts in modules to remove from extraneous removal
const artifactFiles = [];
const copyQueue: Map<string, CopyQueueItem> = new Map();
const hardlinkQueue: Map<string, CopyQueueItem> = new Map();
const hardlinksEnabled = linkDuplicates && (await fs.hardlinksWork(this.config.cwd));
const copiedSrcs: Map<string, string> = new Map();
const symlinkPaths: Map<string, string> = new Map();
for (const [folder, {pkg, loc}] of flatTree) {
const remote = pkg._remote || {type: ''};
const ref = pkg._reference;
let dest = folder;
invariant(ref, 'expected package reference');
let src = loc;
let type = '';
if (remote.type === 'link') {
// replace package source from incorrect cache location (workspaces and link: are not cached)
// with a symlink source
src = remote.reference;
type = 'symlink';
} else if (workspaceLayout && remote.type === 'workspace') {
src = remote.reference;
type = 'symlink';
if (dest.indexOf(workspaceLayout.virtualManifestName) !== -1) {
// we don't need to install virtual manifest
continue;
}
// to get real path for non hoisted dependencies
symlinkPaths.set(dest, src);
} else {
// backwards compatibility: get build artifacts from metadata
// does not apply to symlinked dependencies
const metadata = await this.config.readPackageMetadata(src);
for (const file of metadata.artifacts) {
artifactFiles.push(path.join(dest, file));
}
}
for (const [symlink, realpath] of symlinkPaths.entries()) {
if (dest.indexOf(symlink + path.sep) === 0) {
// after hoisting we end up with this structure
// root/node_modules/workspace-package(symlink)/node_modules/package-a
// fs.copy operations can't copy files through a symlink, so all the paths under workspace-package
// need to be replaced with a real path, except for the symlink root/node_modules/workspace-package
dest = dest.replace(symlink, realpath);
}
}
ref.setLocation(dest);
const integrityArtifacts = this.artifacts[`${pkg.name}@${pkg.version}`];
if (integrityArtifacts) {
for (const file of integrityArtifacts) {
artifactFiles.push(path.join(dest, file));
}
}
const copiedDest = copiedSrcs.get(src);
if (!copiedDest) {
if (hardlinksEnabled) {
copiedSrcs.set(src, dest);
}
copyQueue.set(dest, {
src,
dest,
type,
onFresh() {
if (ref) {
ref.setFresh(true);
}
},
});
} else {
hardlinkQueue.set(dest, {
src: copiedDest,
dest,
onFresh() {
if (ref) {
ref.setFresh(true);
}
},
});
}
}
// keep track of all scoped paths to remove empty scopes after copy
const scopedPaths = new Set();
// register root & scoped packages as being possibly extraneous
const possibleExtraneous: Set<string> = new Set();
for (const folder of this.config.registryFolders) {
const loc = path.join(this.config.cwd, folder);
if (await fs.exists(loc)) {
const files = await fs.readdir(loc);
let filepath;
for (const file of files) {
filepath = path.join(loc, file);
if (file[0] === '@') {
// it's a scope, not a package
scopedPaths.add(filepath);
const subfiles = await fs.readdir(filepath);
for (const subfile of subfiles) {
possibleExtraneous.add(path.join(filepath, subfile));
}
} else {
possibleExtraneous.add(filepath);
}
}
}
}
// If an Extraneous is an entry created via "yarn link", we prevent it from being overwritten.
// Unfortunately, the only way we can know if they have been created this way is to check if they
// are symlinks - problem is that it then conflicts with the newly introduced "link:" protocol,
// which also creates symlinks :( a somewhat weak fix is to check if the symlink target is registered
// inside the linkFolder, in which case we assume it has been created via "yarn link". Otherwise, we
// assume it's a link:-managed dependency, and overwrite it as usual.
const linkTargets = new Map();
let linkedModules;
try {
linkedModules = await fs.readdir(this.config.linkFolder);
} catch (err) {
if (err.code === 'ENOENT') {
linkedModules = [];
} else {
throw err;
}
}
// TODO: Consolidate this logic with `this.config.linkedModules` logic
for (const entry of linkedModules) {
const entryPath = path.join(this.config.linkFolder, entry);
const stat = await fs.lstat(entryPath);
if (stat.isSymbolicLink()) {
const packageName = entry;
linkTargets.set(packageName, await fs.readlink(entryPath));
} else if (stat.isDirectory() && entry[0] === '@') {
// if the entry is directory beginning with '@', then we're dealing with a package scope, which
// means we must iterate inside to retrieve the package names it contains
const scopeName = entry;
for (const entry2 of await fs.readdir(entryPath)) {
const entryPath2 = path.join(entryPath, entry2);
const stat2 = await fs.lstat(entryPath2);
if (stat2.isSymbolicLink()) {
const packageName = `${scopeName}/${entry2}`;
linkTargets.set(packageName, await fs.readlink(entryPath2));
}
}
}
}
for (const loc of possibleExtraneous) {
let packageName = path.basename(loc);
const scopeName = path.basename(path.dirname(loc));
if (scopeName[0] === `@`) {
packageName = `${scopeName}/${packageName}`;
}
if (
(await fs.lstat(loc)).isSymbolicLink() &&
linkTargets.has(packageName) &&
linkTargets.get(packageName) === (await fs.readlink(loc))
) {
possibleExtraneous.delete(loc);
copyQueue.delete(loc);
}
}
//
let tick;
await fs.copyBulk(Array.from(copyQueue.values()), this.reporter, {
possibleExtraneous,
artifactFiles,
ignoreBasenames: [constants.METADATA_FILENAME, constants.TARBALL_FILENAME],
onStart: (num: number) => {
tick = this.reporter.progress(num);
},
onProgress(src: string) {
if (tick) {
tick();
}
},
});
await fs.hardlinkBulk(Array.from(hardlinkQueue.values()), this.reporter, {
possibleExtraneous,
artifactFiles,
onStart: (num: number) => {
tick = this.reporter.progress(num);
},
onProgress(src: string) {
if (tick) {
tick();
}
},
});
// remove all extraneous files that weren't in the tree
for (const loc of possibleExtraneous) {
this.reporter.verbose(this.reporter.lang('verboseFileRemoveExtraneous', loc));
await fs.unlink(loc);
}
// remove any empty scoped directories
for (const scopedPath of scopedPaths) {
const files = await fs.readdir(scopedPath);
if (files.length === 0) {
await fs.unlink(scopedPath);
}
}
// create binary links
if (this.config.binLinks) {
const topLevelDependencies = this.determineTopLevelBinLinks(flatTree);
const tickBin = this.reporter.progress(flatTree.length + topLevelDependencies.length);
// create links in transient dependencies
await promise.queue(
flatTree,
async ([dest, {pkg}]) => {
if (pkg._reference && pkg._reference.location) {
const binLoc = path.join(dest, this.config.getFolder(pkg));
await this.linkBinDependencies(pkg, binLoc);
tickBin();
}
},
linkBinConcurrency,
);
// create links at top level for all dependencies.
await promise.queue(
topLevelDependencies,
async ([dest, pkg]) => {
if (pkg._reference && pkg._reference.location && pkg.bin && Object.keys(pkg.bin).length) {
const binLoc = path.join(this.config.cwd, this.config.getFolder(pkg));
await this.linkSelfDependencies(pkg, dest, binLoc);
tickBin();
}
},
linkBinConcurrency,
);
}
for (const [, {pkg}] of flatTree) {
await this._warnForMissingBundledDependencies(pkg);
}
}
determineTopLevelBinLinks(flatTree: HoistManifestTuples): Array<[string, Manifest]> {
const linksToCreate = new Map();
for (const [dest, {pkg, isDirectRequire}] of flatTree) {
const {name} = pkg;
if (isDirectRequire || (this.topLevelBinLinking && !linksToCreate.has(name))) {
linksToCreate.set(name, [dest, pkg]);
}
}
return Array.from(linksToCreate.values());
}
resolvePeerModules() {
for (const pkg of this.resolver.getManifests()) {
this._resolvePeerModules(pkg);
}
}
_resolvePeerModules(pkg: Manifest) {
const peerDeps = pkg.peerDependencies;
if (!peerDeps) {
return;
}
const ref = pkg._reference;
invariant(ref, 'Package reference is missing');
for (const name in peerDeps) {
const range = peerDeps[name];
const pkgs = this.resolver.getAllInfoForPackageName(name);
const found = pkgs.find(pkg => {
const {root, version} = pkg._reference || {};
return root && this._satisfiesPeerDependency(range, version);
});
const foundPattern = found && found._reference && found._reference.patterns;
if (foundPattern) {
ref.addDependencies(foundPattern);
} else {
const depError = pkgs.length > 0 ? 'incorrectPeer' : 'unmetPeer';
const [pkgHuman, depHuman] = [`${pkg.name}@${pkg.version}`, `${name}@${range}`];
this.reporter.warn(this.reporter.lang(depError, pkgHuman, depHuman));
}
}
}
_satisfiesPeerDependency(range: string, version: string): boolean {
return range === '*' || satisfiesWithPreleases(version, range, this.config.looseSemver);
}
async _warnForMissingBundledDependencies(pkg: Manifest): Promise<void> {
const ref = pkg._reference;
if (pkg.bundleDependencies) {
for (const depName of pkg.bundleDependencies) {
const loc = path.join(this.config.generateHardModulePath(ref), this.config.getFolder(pkg), depName);
if (!await fs.exists(loc)) {
const pkgHuman = `${pkg.name}@${pkg.version}`;
this.reporter.warn(this.reporter.lang('missingBundledDependency', pkgHuman, depName));
}
}
}
}
async init(
patterns: Array<string>,
workspaceLayout?: WorkspaceLayout,
{linkDuplicates, ignoreOptional}: {linkDuplicates: ?boolean, ignoreOptional: ?boolean} = {},
): Promise<void> {
this.resolvePeerModules();
await this.copyModules(patterns, workspaceLayout, {linkDuplicates, ignoreOptional});
}
}