@pnpm/fs.indexed-pkg-importer 1100.0.31 → 1100.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,48 +1,33 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { isError } from '@pnpm/error';
4
- import gfs, { lstatWithRetry, renameFileWithRetry, unlinkWithRetry, withFileLockRetry } from '@pnpm/fs.graceful-fs';
5
- import { globalInfo, globalWarn, logger } from '@pnpm/logger';
4
+ import { withFileLockRetry } from '@pnpm/fs.graceful-fs';
5
+ import { globalWarn, logger } from '@pnpm/logger';
6
6
  import { rimrafSync } from '@zkochan/rimraf';
7
- import fsx from 'fs-extra';
8
7
  import { makeEmptyDirSync } from 'make-empty-dir';
9
8
  import { fastPathTemp as pathTemp } from 'path-temp';
10
9
  import { renameOverwriteSync } from 'rename-overwrite';
11
- import sanitizeFilename from 'sanitize-filename';
10
+ import { copyInternalSymlink } from './copyInternalSymlink.js';
11
+ import { makeFileMapDirs } from './dirUtils.js';
12
+ import { getUniqueFileMap, moveOrMergeModulesDirs, sanitizeFilenames, } from './fileMapUtils.js';
13
+ import { allFilesMatch, allSymlinksMatch, repairIndexedDir, } from './repairIndexedDir.js';
12
14
  const filenameConflictsLogger = logger('_filename-conflicts');
13
- const FILE_COMPARE_BUFFER_SIZE = 64 * 1024;
14
15
  export function importIndexedDir(importer, newDir, filenames, opts) {
15
16
  const dirImport = { importer, newDir, filenames, opts };
16
- // Content-addressed target (e.g. global virtual store): the path is shared
17
- // across projects, so concurrent importers are expected and the directory
18
- // must never be removed or swapped out from under them. It is populated in
19
- // place, adopted when it already matches, and otherwise repaired entry by
20
- // entry. Repairing in place leaves a nested node_modules/ where it is, so
21
- // keepModulesDir has nothing to preserve here either.
22
- //
23
- // A local directory is copied into the target at install time, so its
24
- // contents can change without the lockfile changing and the path pins
25
- // nothing. Such a target is rebuilt below rather than adopted or repaired.
26
17
  if (opts.safeToSkip && opts.resolvedFrom !== 'local-dir') {
27
18
  importIntoSharedDir(dirImport);
28
19
  return;
29
20
  }
30
- // Fast path: import directly without staging. Callers already verified
31
- // the target package is missing (pkgExistsAtTargetDir / pkgLinkedToStore),
32
- // so we can write straight into newDir and skip the temp dir + rename.
33
- // On failure we fall through to the staging path, which has full error
34
- // handling (EEXIST dedup, ENOENT sanitized-filename retry, etc.) and
35
- // atomically swaps in a complete directory.
36
- // keepModulesDir needs the staging path to preserve the existing node_modules.
37
21
  if (!opts.keepModulesDir && tryExclusiveImport(importer, newDir, filenames, {
38
22
  links: symlinkDirs(opts, filenames, { writtenDir: newDir, finalDir: newDir }),
39
23
  symlinks: opts.symlinks,
40
24
  })) {
41
25
  return;
42
26
  }
43
- // Staging path: create in temp dir, then atomically rename.
44
- // The dir rename is itself atomic, so individual file atomicity is not
45
- // needed here — use importFile for everything.
27
+ importWithStaging(dirImport);
28
+ }
29
+ function importWithStaging(dirImport) {
30
+ const { importer, newDir, filenames, opts } = dirImport;
46
31
  const stage = pathTemp(newDir);
47
32
  try {
48
33
  makeEmptyDirSync(stage, { recursive: true });
@@ -51,226 +36,73 @@ export function importIndexedDir(importer, newDir, filenames, opts) {
51
36
  symlinks: opts.symlinks,
52
37
  });
53
38
  if (opts.keepModulesDir) {
54
- // Keeping node_modules is needed only when the hoisted node linker is used.
55
39
  moveOrMergeModulesDirs(path.join(newDir, 'node_modules'), path.join(stage, 'node_modules'));
56
40
  }
57
41
  }
58
42
  catch (err) {
59
- try {
60
- rimrafSync(stage);
61
- }
62
- catch { } // eslint-disable-line:no-empty
43
+ cleanStageDir(stage);
63
44
  if (retryWithFixedFileMap(err, dirImport))
64
45
  return;
65
46
  throw err;
66
47
  }
48
+ commitStagedDir(stage, newDir);
49
+ }
50
+ function commitStagedDir(stage, newDir) {
67
51
  try {
68
- // rename-overwrite retries Windows lock errors (EPERM, EBUSY) but not the
69
- // EACCES that the same locks produce on a Windows drive mounted into WSL.
70
52
  withFileLockRetry(() => {
71
53
  renameOverwriteSync(stage, newDir);
72
54
  });
73
55
  }
74
56
  catch (renameErr) {
75
- try {
76
- rimrafSync(stage);
77
- }
78
- catch { } // eslint-disable-line:no-empty
57
+ cleanStageDir(stage);
79
58
  throw renameErr;
80
59
  }
81
60
  }
82
- // Import into a directory whose path is shared with other projects, and with
83
- // the installs running in them. Nothing here removes a dirent: the winner of
84
- // the exclusive mkdir writes the package, and everyone else adopts the result
85
- // when it already matches and otherwise replaces only the entries that do not,
86
- // so concurrent importers converge on the same tree.
87
- //
88
- // Replacing rather than adopting is what keeps a shared slot repairable. The
89
- // linking tiers report EEXIST for a dirent that is already there, so an import
90
- // that adopts what it finds keeps a file truncated by an interrupted copy, and
91
- // then puts the completion marker on top of it — after which the directory
92
- // looks finished to every later install and is never repaired.
93
- function importIntoSharedDir(dirImport) {
94
- const { importer, newDir, filenames, opts } = dirImport;
95
- fs.mkdirSync(path.dirname(newDir), { recursive: true });
96
- let created = false;
61
+ function cleanStageDir(stage) {
97
62
  try {
98
- fs.mkdirSync(newDir);
99
- created = true;
100
- }
101
- catch (err) {
102
- if (!isError(err) || !('code' in err) || err.code !== 'EEXIST')
103
- throw err;
104
- }
105
- if (created) {
106
- try {
107
- tryImportIndexedDir(importer, newDir, filenames, { symlinks: opts.symlinks });
108
- return;
109
- }
110
- catch (err) {
111
- if (retryWithFixedFileMap(err, dirImport))
112
- return;
113
- // Our own write stopped partway. Another importer may already be reading
114
- // what did land, so finish the directory in place instead of staging a
115
- // replacement for it.
116
- }
117
- }
118
- if (allFilesMatch(newDir, filenames) && allSymlinksMatch(newDir, opts.symlinks))
119
- return;
120
- try {
121
- repairIndexedDir(dirImport);
122
- }
123
- catch (err) {
124
- if (retryWithFixedFileMap(err, dirImport))
125
- return;
126
- throw err;
127
- }
128
- }
129
- // Bring an occupied shared directory up to date with the file map, entry by
130
- // entry. Files the package does not declare are left alone: a build output
131
- // belongs to whoever put it there, and a slot other installs are reading is
132
- // not somewhere to delete from speculatively.
133
- function repairIndexedDir({ importer, newDir, filenames, opts }) {
134
- makeFileMapDirs(newDir, filenames, { clearBlockers: true });
135
- let packageJsonSrc;
136
- for (const [f, src] of filenames) {
137
- if (f === 'package.json') {
138
- packageJsonSrc = src;
139
- continue;
140
- }
141
- replaceFileIfDifferent(importer.importFile, src, path.join(newDir, f));
142
- }
143
- for (const [f, target] of opts.symlinks ?? []) {
144
- const dir = path.posix.dirname(f);
145
- if (dir !== '.')
146
- clearDirentBlockingDir(newDir, dir);
147
- replaceSymlinkIfDifferent(target, path.join(newDir, f));
148
- }
149
- if (packageJsonSrc !== undefined) {
150
- replaceFileIfDifferent(importer.importFile, packageJsonSrc, path.join(newDir, 'package.json'));
63
+ rimrafSync(stage);
151
64
  }
65
+ catch { }
152
66
  }
153
- // Swap the file in through a temp sibling: a reader sees either the old dirent
154
- // or the new one, and the rename replaces what the linking tiers would have
155
- // refused to overwrite.
156
- function replaceFileIfDifferent(importFile, src, dest) {
157
- if (mismatchReason(dest, src) === undefined)
158
- return;
159
- const tmp = pathTemp(dest);
160
- try {
161
- importFile(src, tmp);
162
- }
163
- catch (err) {
164
- try {
165
- fs.unlinkSync(tmp);
166
- }
167
- catch { } // eslint-disable-line:no-empty
168
- throw err;
169
- }
170
- try {
171
- clearDirBlockingFile(dest);
172
- renameFileWithRetry(tmp, dest);
173
- }
174
- catch (err) {
175
- try {
176
- fs.unlinkSync(tmp);
177
- }
178
- catch { } // eslint-disable-line:no-empty
179
- if (mismatchReason(dest, src) === undefined)
180
- return;
181
- throw err;
182
- }
183
- }
184
- function replaceSymlinkIfDifferent(target, dest) {
185
- if (symlinkMatches(dest, target))
186
- return;
187
- fs.mkdirSync(path.dirname(dest), { recursive: true });
188
- const tmp = pathTemp(dest);
189
- fs.symlinkSync(target, tmp);
67
+ function tryExclusiveMkdir(newDir) {
68
+ fs.mkdirSync(path.dirname(newDir), { recursive: true });
190
69
  try {
191
- clearDirBlockingFile(dest);
192
- renameFileWithRetry(tmp, dest);
70
+ fs.mkdirSync(newDir);
71
+ return true;
193
72
  }
194
73
  catch (err) {
195
- try {
196
- fs.unlinkSync(tmp);
197
- }
198
- catch { } // eslint-disable-line:no-empty
199
- if (symlinkMatches(dest, target))
200
- return;
74
+ if (isError(err) && 'code' in err && err.code === 'EEXIST')
75
+ return false;
201
76
  throw err;
202
77
  }
203
78
  }
204
- // Whether a package directory can go at `dir` now: nothing is there, or
205
- // what is there is already a directory.
206
- //
207
- // A failed removal is not a failed clearing when this holds. The installers
208
- // healing a slot together race over these paths, and one that finishes the
209
- // same work first leaves exactly what the removal was for, whether it merely
210
- // unlinked the blocker or replaced it outright. The inspection retries for
211
- // the same reason the others here do.
212
- function dirFitsAt(dir) {
79
+ function tryDirectSharedImport(dirImport) {
80
+ const { importer, newDir, filenames, opts } = dirImport;
81
+ if (!tryExclusiveMkdir(newDir))
82
+ return false;
213
83
  try {
214
- return lstatWithRetry(dir).isDirectory();
84
+ tryImportIndexedDir(importer, newDir, filenames, { symlinks: opts.symlinks });
85
+ return true;
215
86
  }
216
87
  catch (err) {
217
- return isError(err) && 'code' in err && err.code === 'ENOENT';
88
+ return retryWithFixedFileMap(err, dirImport);
218
89
  }
219
90
  }
220
- // A rename cannot put a file where a directory is (EISDIR), so one standing in
221
- // the way has to go first. Only a damaged tree has one.
222
- //
223
- // The inspection retries, because installs in different projects heal one
224
- // shared slot at the same time and Windows reports a dirent one of them has
225
- // just unlinked as inaccessible until the unlink lands.
226
- function clearDirBlockingFile(dest) {
227
- let stats;
91
+ function importIntoSharedDir(dirImport) {
92
+ if (tryDirectSharedImport(dirImport))
93
+ return;
94
+ const { newDir, filenames, opts } = dirImport;
95
+ if (allFilesMatch(newDir, filenames) && allSymlinksMatch(newDir, opts.symlinks))
96
+ return;
228
97
  try {
229
- stats = lstatWithRetry(dest);
98
+ repairIndexedDir(dirImport);
230
99
  }
231
100
  catch (err) {
232
- if (isError(err) && 'code' in err && err.code === 'ENOENT')
101
+ if (retryWithFixedFileMap(err, dirImport))
233
102
  return;
234
103
  throw err;
235
104
  }
236
- if (stats.isDirectory()) {
237
- rimrafSync(dest);
238
- }
239
- }
240
- // A file where the package needs a directory turns up only in a damaged tree,
241
- // and would fail the mkdir below. Walking top-down means a segment whose
242
- // parent is itself a file is never reached: the parent goes first, and
243
- // everything under a missing segment is missing too.
244
- function clearDirentBlockingDir(newDir, relativeDir) {
245
- let dir = newDir;
246
- for (const segment of relativeDir.split(/[\\/]/)) {
247
- dir = path.join(dir, segment);
248
- let stats;
249
- try {
250
- stats = lstatWithRetry(dir);
251
- }
252
- catch (err) {
253
- if (isError(err) && 'code' in err && err.code === 'ENOENT')
254
- return;
255
- throw err;
256
- }
257
- if (stats.isDirectory())
258
- continue;
259
- try {
260
- unlinkWithRetry(dir);
261
- }
262
- catch (err) {
263
- if (!dirFitsAt(dir))
264
- throw err;
265
- }
266
- return;
267
- }
268
105
  }
269
- // The two failures an indexed file map can cause on its own: names that
270
- // collide on a case-insensitive filesystem, and names the filesystem rejects
271
- // outright. Both are recovered by rewriting the map and importing again.
272
- // Returns false for anything else, which the caller must treat as a real
273
- // failure.
274
106
  function retryWithFixedFileMap(err, dirImport) {
275
107
  const { importer, newDir, filenames, opts } = dirImport;
276
108
  if (!isError(err) || !('code' in err))
@@ -295,33 +127,9 @@ function retryWithFixedFileMap(err, dirImport) {
295
127
  }
296
128
  return false;
297
129
  }
298
- // Fast path for the regular virtual store: write directly into newDir, but
299
- // only when we can create it exclusively. A successful exclusive mkdir proves
300
- // no other process is importing the same package concurrently, so the direct
301
- // (non-atomic) write is safe. If newDir already exists — a concurrent importer
302
- // or a stale partial directory from an interrupted import — this returns false
303
- // so the caller falls back to the staging path, which builds a complete
304
- // directory in a private temp dir and atomically renames it into place.
305
- //
306
- // Destructively emptying a directory another process may be populating could
307
- // otherwise leave a partial package behind: if the surviving files include the
308
- // package.json completion marker, every later install treats the broken
309
- // directory as complete and never repairs it.
310
130
  function tryExclusiveImport(importer, newDir, filenames, entries) {
311
- fs.mkdirSync(path.dirname(newDir), { recursive: true });
312
- try {
313
- fs.mkdirSync(newDir);
314
- }
315
- catch (err) {
316
- if (isError(err) && 'code' in err && err.code === 'EEXIST')
317
- return false;
318
- throw err;
319
- }
320
- // We exclusively created newDir, so no other process writes into it directly
321
- // (concurrent importers see EEXIST above and take the staging path). The
322
- // directory is ours: on failure we remove our partial result before falling
323
- // back to staging, so the next attempt — including this process's own method
324
- // fallbacks (clone → hardlink → copy) — can fast-path again.
131
+ if (!tryExclusiveMkdir(newDir))
132
+ return false;
325
133
  try {
326
134
  tryImportIndexedDir(importer, newDir, filenames, entries);
327
135
  return true;
@@ -330,98 +138,8 @@ function tryExclusiveImport(importer, newDir, filenames, entries) {
330
138
  try {
331
139
  rimrafSync(newDir);
332
140
  }
333
- catch { } // eslint-disable-line:no-empty
334
- return false;
335
- }
336
- }
337
- function allFilesMatch(dir, filenames) {
338
- // The completion marker is written last, so its absence settles the common
339
- // case — a directory another importer is still filling — in one stat,
340
- // wherever the map happens to hold it.
341
- const markerSrc = filenames.get('package.json');
342
- if (markerSrc !== undefined && !fileMatches(dir, 'package.json', markerSrc))
141
+ catch { }
343
142
  return false;
344
- for (const [f, src] of filenames) {
345
- if (f === 'package.json')
346
- continue;
347
- if (!fileMatches(dir, f, src))
348
- return false;
349
- }
350
- return true;
351
- }
352
- function allSymlinksMatch(dir, symlinks) {
353
- for (const [f, target] of symlinks ?? []) {
354
- if (!symlinkMatches(path.join(dir, f), target)) {
355
- globalInfo(`Re-importing "${dir}" because symlink "${f}" does not point to "${target}"`);
356
- return false;
357
- }
358
- }
359
- return true;
360
- }
361
- function symlinkMatches(dest, target) {
362
- try {
363
- return fs.readlinkSync(dest) === target;
364
- }
365
- catch {
366
- return false;
367
- }
368
- }
369
- function fileMatches(dir, f, src) {
370
- const reason = mismatchReason(path.join(dir, f), src);
371
- if (reason === undefined)
372
- return true;
373
- globalInfo(`Re-importing "${dir}" because file "${f}" ${reason}`);
374
- return false;
375
- }
376
- // Why `target` is not the store file at `src`, or undefined when it already
377
- // is. Files imported by hardlink or reflink share the store inode, which
378
- // settles it without a read; the copy tier compares size first, then content.
379
- function mismatchReason(target, src) {
380
- try {
381
- const targetStat = fs.lstatSync(target, { bigint: true });
382
- if (!targetStat.isFile())
383
- return 'is not a regular file';
384
- const srcStat = gfs.statSync(src, { bigint: true });
385
- const hasSameFileIdentity = targetStat.ino !== 0n &&
386
- targetStat.dev !== 0n &&
387
- targetStat.ino === srcStat.ino &&
388
- targetStat.dev === srcStat.dev;
389
- if (hasSameFileIdentity)
390
- return undefined;
391
- if (targetStat.size !== srcStat.size)
392
- return 'has a different size';
393
- if (!filesHaveEqualContents(target, src))
394
- return 'has different content';
395
- return undefined;
396
- }
397
- catch {
398
- return 'is missing or unreadable';
399
- }
400
- }
401
- function filesHaveEqualContents(left, right) {
402
- const leftBuffer = Buffer.allocUnsafe(FILE_COMPARE_BUFFER_SIZE);
403
- const rightBuffer = Buffer.allocUnsafe(FILE_COMPARE_BUFFER_SIZE);
404
- const leftFd = fs.openSync(left, 'r');
405
- try {
406
- const rightFd = fs.openSync(right, 'r');
407
- try {
408
- for (;;) {
409
- const leftBytes = fs.readSync(leftFd, leftBuffer, 0, leftBuffer.length, null);
410
- const rightBytes = fs.readSync(rightFd, rightBuffer, 0, rightBuffer.length, null);
411
- if (leftBytes !== rightBytes)
412
- return false;
413
- if (leftBytes === 0)
414
- return true;
415
- if (!leftBuffer.subarray(0, leftBytes).equals(rightBuffer.subarray(0, rightBytes)))
416
- return false;
417
- }
418
- }
419
- finally {
420
- fs.closeSync(rightFd);
421
- }
422
- }
423
- finally {
424
- fs.closeSync(leftFd);
425
143
  }
426
144
  }
427
145
  function retryWithSanitizedFilenames({ importer, newDir, filenames, opts }) {
@@ -435,34 +153,18 @@ They were renamed.`);
435
153
  importIndexedDir(importer, newDir, sanitizedFilenames, opts);
436
154
  return true;
437
155
  }
438
- function sanitizeFilenames(filenames) {
439
- const sanitizedFilenames = new Map();
440
- const invalidFilenames = [];
441
- for (const [filename, src] of filenames) {
442
- const sanitizedFilename = filename.split('/').map((f) => sanitizeFilename(f)).join('/');
443
- if (sanitizedFilename !== filename) {
444
- invalidFilenames.push(filename);
445
- }
446
- sanitizedFilenames.set(sanitizedFilename, src);
447
- }
448
- return { sanitizedFilenames, invalidFilenames };
449
- }
450
156
  function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames, { links, symlinks } = {}) {
451
157
  makeFileMapDirs(newDir, filenames);
452
- // Write package.json last so it acts as a completion marker.
453
- // pkgExistsAtTargetDir() checks for package.json to decide if a package
454
- // is already imported — writing it last ensures a crash mid-import won't
455
- // leave a partially-populated directory that appears fully imported.
456
158
  let packageJsonSrc;
457
- for (const [f, src] of filenames) {
458
- if (f === 'package.json') {
159
+ for (const [relativePath, src] of filenames) {
160
+ if (relativePath === 'package.json') {
459
161
  packageJsonSrc = src;
460
162
  continue;
461
163
  }
462
- importEntry(importFile, src, path.join(newDir, f), links);
164
+ importEntry(importFile, src, path.join(newDir, relativePath), links);
463
165
  }
464
- for (const [f, target] of symlinks ?? []) {
465
- const dest = path.join(newDir, f);
166
+ for (const [relativePath, target] of symlinks ?? []) {
167
+ const dest = path.join(newDir, relativePath);
466
168
  fs.mkdirSync(path.dirname(dest), { recursive: true });
467
169
  fs.symlinkSync(target, dest);
468
170
  }
@@ -470,14 +172,12 @@ function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames
470
172
  importEntry(importFileAtomic, packageJsonSrc, path.join(newDir, 'package.json'), links);
471
173
  }
472
174
  }
473
- // Only a local directory has symlinks to preserve. The store holds regular
474
- // files, so other imports skip the lstat per file.
475
175
  function symlinkDirs(opts, filenames, dirs) {
476
176
  if (opts.resolvedFrom !== 'local-dir')
477
177
  return undefined;
478
178
  const imported = new Set(['']);
479
- for (const f of filenames.keys()) {
480
- for (let entry = f; entry !== '' && !imported.has(entry); entry = path.posix.dirname(entry).replace(/^\.$/, '')) {
179
+ for (const relativePath of filenames.keys()) {
180
+ for (let entry = relativePath; entry !== '' && !imported.has(entry); entry = path.posix.dirname(entry).replace(/^\.$/, '')) {
481
181
  imported.add(entry);
482
182
  }
483
183
  }
@@ -488,136 +188,4 @@ function importEntry(importFile, src, dest, links) {
488
188
  return;
489
189
  importFile(src, dest);
490
190
  }
491
- // Recreate the symlink at src as dest when its target stays inside the
492
- // package, and report whether the entry is handled. A link that points outside
493
- // the package, or at a file the import leaves out, is imported as the file it
494
- // points to. A link to a directory the import leaves out is left out too.
495
- function copyInternalSymlink(src, dest, links) {
496
- let target = fs.readlinkSync(src);
497
- if (path.isAbsolute(target)) {
498
- // The link text can reach the package through another path than src,
499
- // such as a symlinked workspace, so compare real paths.
500
- target = path.relative(realpathOrSelf(path.dirname(src)), realpathOrSelf(target));
501
- }
502
- const resolved = path.resolve(path.dirname(dest), target);
503
- if (escapesDir(links.writtenDir, resolved))
504
- return false;
505
- if (!links.imported.has(path.relative(links.writtenDir, resolved).split(path.sep).join('/'))) {
506
- return isDirectory(src);
507
- }
508
- if (process.platform !== 'win32') {
509
- fs.symlinkSync(target, dest);
510
- return true;
511
- }
512
- const isDir = isDirectory(src);
513
- try {
514
- fs.symlinkSync(target, dest, isDir ? 'dir' : 'file');
515
- }
516
- catch (err) {
517
- // Creating a symlink on Windows needs a privilege that a junction does
518
- // not, and a file has no junction to fall back to.
519
- if (!isError(err) || !('code' in err) || err.code !== 'EPERM')
520
- throw err;
521
- if (!isDir)
522
- return false;
523
- fs.symlinkSync(path.join(links.finalDir, path.relative(links.writtenDir, resolved)), dest, 'junction');
524
- }
525
- return true;
526
- }
527
- function realpathOrSelf(file) {
528
- try {
529
- return fs.realpathSync(file);
530
- }
531
- catch (err) {
532
- if (isError(err) && 'code' in err && err.code === 'ENOENT')
533
- return file;
534
- throw err;
535
- }
536
- }
537
- function isDirectory(file) {
538
- try {
539
- return fs.statSync(file).isDirectory();
540
- }
541
- catch (err) {
542
- if (isError(err) && 'code' in err && err.code === 'ENOENT')
543
- return false;
544
- throw err;
545
- }
546
- }
547
- function escapesDir(rootDir, targetPath) {
548
- const rel = path.relative(rootDir, targetPath);
549
- return rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel);
550
- }
551
- // Sorting shortest-first means the recursive mkdir for a deeper directory
552
- // always finds its ancestor already on disk.
553
- function makeFileMapDirs(newDir, filenames, opts) {
554
- const allDirs = new Set();
555
- for (const f of filenames.keys()) {
556
- const dir = path.dirname(f);
557
- if (dir === '.')
558
- continue;
559
- allDirs.add(dir);
560
- }
561
- for (const dir of Array.from(allDirs).sort((d1, d2) => d1.length - d2.length)) {
562
- if (opts?.clearBlockers) {
563
- clearDirentBlockingDir(newDir, dir);
564
- }
565
- fs.mkdirSync(path.join(newDir, dir), { recursive: true });
566
- }
567
- }
568
- function getUniqueFileMap(fileMap) {
569
- const lowercaseFiles = new Map();
570
- const conflictingFileNames = new Map();
571
- const uniqueFileMap = new Map();
572
- for (const filename of Array.from(fileMap.keys()).sort()) {
573
- const lowercaseFilename = filename.toLowerCase();
574
- if (lowercaseFiles.has(lowercaseFilename)) {
575
- conflictingFileNames.set(filename, lowercaseFiles.get(lowercaseFilename));
576
- continue;
577
- }
578
- lowercaseFiles.set(lowercaseFilename, filename);
579
- uniqueFileMap.set(filename, fileMap.get(filename));
580
- }
581
- return {
582
- conflictingFileNames,
583
- uniqueFileMap,
584
- };
585
- }
586
- function moveOrMergeModulesDirs(src, dest) {
587
- try {
588
- renameEvenAcrossDevices(src, dest);
589
- }
590
- catch (err) {
591
- switch (isError(err) && 'code' in err && err.code) {
592
- case 'ENOENT':
593
- // If src directory doesn't exist, there is nothing to do
594
- return;
595
- case 'ENOTEMPTY':
596
- case 'EPERM': // This error code is thrown on Windows
597
- // The newly added dependency might have node_modules if it has bundled dependencies.
598
- mergeModulesDirs(src, dest);
599
- return;
600
- default:
601
- throw err;
602
- }
603
- }
604
- }
605
- function renameEvenAcrossDevices(src, dest) {
606
- try {
607
- gfs.renameSync(src, dest);
608
- }
609
- catch (err) {
610
- if (!(isError(err) && 'code' in err && err.code === 'EXDEV'))
611
- throw err;
612
- fsx.copySync(src, dest);
613
- }
614
- }
615
- function mergeModulesDirs(src, dest) {
616
- const srcFiles = fs.readdirSync(src);
617
- const destFiles = new Set(fs.readdirSync(dest));
618
- const filesToMove = srcFiles.filter((file) => !destFiles.has(file));
619
- for (const file of filesToMove) {
620
- renameEvenAcrossDevices(path.join(src, file), path.join(dest, file));
621
- }
622
- }
623
191
  //# sourceMappingURL=importIndexedDir.js.map