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

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @pnpm/fs.indexed-pkg-importer
2
2
 
3
+ ## 1100.0.32
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies:
8
+ - @pnpm/core-loggers@1101.0.2
9
+ - @pnpm/store.controller-types@1101.3.3
10
+
3
11
  ## 1100.0.31
4
12
 
5
13
  ### Patch Changes
@@ -0,0 +1,6 @@
1
+ export interface SymlinkDirs {
2
+ writtenDir: string;
3
+ finalDir: string;
4
+ imported: Set<string>;
5
+ }
6
+ export declare function copyInternalSymlink(src: string, dest: string, links: SymlinkDirs): boolean;
@@ -0,0 +1,61 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { isError } from '@pnpm/error';
4
+ export function copyInternalSymlink(src, dest, links) {
5
+ let target = fs.readlinkSync(src);
6
+ if (path.isAbsolute(target)) {
7
+ target = path.relative(realpathOrSelf(path.dirname(src)), realpathOrSelf(target));
8
+ }
9
+ const resolved = path.resolve(path.dirname(dest), target);
10
+ if (escapesDir(links.writtenDir, resolved))
11
+ return false;
12
+ if (!links.imported.has(path.relative(links.writtenDir, resolved).split(path.sep).join('/'))) {
13
+ return isDirectory(src);
14
+ }
15
+ if (process.platform !== 'win32') {
16
+ fs.symlinkSync(target, dest);
17
+ return true;
18
+ }
19
+ return copyInternalSymlinkWindows({ dest, links, resolved, src, target });
20
+ }
21
+ function copyInternalSymlinkWindows(opts) {
22
+ const { dest, links, resolved, src, target } = opts;
23
+ const isDir = isDirectory(src);
24
+ try {
25
+ fs.symlinkSync(target, dest, isDir ? 'dir' : 'file');
26
+ return true;
27
+ }
28
+ catch (err) {
29
+ if (!isError(err) || !('code' in err) || err.code !== 'EPERM')
30
+ throw err;
31
+ if (!isDir)
32
+ return false;
33
+ fs.symlinkSync(path.join(links.finalDir, path.relative(links.writtenDir, resolved)), dest, 'junction');
34
+ return true;
35
+ }
36
+ }
37
+ function realpathOrSelf(file) {
38
+ try {
39
+ return fs.realpathSync(file);
40
+ }
41
+ catch (err) {
42
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
43
+ return file;
44
+ throw err;
45
+ }
46
+ }
47
+ function isDirectory(file) {
48
+ try {
49
+ return fs.statSync(file).isDirectory();
50
+ }
51
+ catch (err) {
52
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
53
+ return false;
54
+ throw err;
55
+ }
56
+ }
57
+ function escapesDir(rootDir, targetPath) {
58
+ const rel = path.relative(rootDir, targetPath);
59
+ return rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel);
60
+ }
61
+ //# sourceMappingURL=copyInternalSymlink.js.map
@@ -0,0 +1,5 @@
1
+ export declare function clearDirentBlockingDir(newDir: string, relativeDir: string): void;
2
+ export declare function clearDirBlockingFile(dest: string): void;
3
+ export declare function makeFileMapDirs(newDir: string, filenames: Map<string, string>, opts?: {
4
+ clearBlockers: boolean;
5
+ }): void;
@@ -0,0 +1,72 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { isError } from '@pnpm/error';
4
+ import { lstatWithRetry, unlinkWithRetry } from '@pnpm/fs.graceful-fs';
5
+ import { rimrafSync } from '@zkochan/rimraf';
6
+ export function clearDirentBlockingDir(newDir, relativeDir) {
7
+ let dir = newDir;
8
+ for (const segment of relativeDir.split(/[\\/]/)) {
9
+ dir = path.join(dir, segment);
10
+ if (!clearDirentSegment(dir))
11
+ return;
12
+ }
13
+ }
14
+ function clearDirentSegment(dir) {
15
+ let stats;
16
+ try {
17
+ stats = lstatWithRetry(dir);
18
+ }
19
+ catch (err) {
20
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
21
+ return false;
22
+ throw err;
23
+ }
24
+ if (stats.isDirectory())
25
+ return true;
26
+ try {
27
+ unlinkWithRetry(dir);
28
+ }
29
+ catch (err) {
30
+ if (!dirFitsAt(dir))
31
+ throw err;
32
+ }
33
+ return false;
34
+ }
35
+ function dirFitsAt(dir) {
36
+ try {
37
+ return lstatWithRetry(dir).isDirectory();
38
+ }
39
+ catch (err) {
40
+ return isError(err) && 'code' in err && err.code === 'ENOENT';
41
+ }
42
+ }
43
+ export function clearDirBlockingFile(dest) {
44
+ let stats;
45
+ try {
46
+ stats = lstatWithRetry(dest);
47
+ }
48
+ catch (err) {
49
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
50
+ return;
51
+ throw err;
52
+ }
53
+ if (stats.isDirectory()) {
54
+ rimrafSync(dest);
55
+ }
56
+ }
57
+ export function makeFileMapDirs(newDir, filenames, opts) {
58
+ const allDirs = new Set();
59
+ for (const relativePath of filenames.keys()) {
60
+ const dir = path.dirname(relativePath);
61
+ if (dir === '.')
62
+ continue;
63
+ allDirs.add(dir);
64
+ }
65
+ for (const dir of Array.from(allDirs).sort((d1, d2) => d1.length - d2.length)) {
66
+ if (opts?.clearBlockers) {
67
+ clearDirentBlockingDir(newDir, dir);
68
+ }
69
+ fs.mkdirSync(path.join(newDir, dir), { recursive: true });
70
+ }
71
+ }
72
+ //# sourceMappingURL=dirUtils.js.map
@@ -0,0 +1,11 @@
1
+ export interface GetUniqueFileMapResult {
2
+ conflictingFileNames: Map<string, string>;
3
+ uniqueFileMap: Map<string, string>;
4
+ }
5
+ export declare function getUniqueFileMap(fileMap: Map<string, string>): GetUniqueFileMapResult;
6
+ export interface SanitizeFilenamesResult {
7
+ sanitizedFilenames: Map<string, string>;
8
+ invalidFilenames: string[];
9
+ }
10
+ export declare function sanitizeFilenames(filenames: Map<string, string>): SanitizeFilenamesResult;
11
+ export declare function moveOrMergeModulesDirs(src: string, dest: string): void;
@@ -0,0 +1,72 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { isError } from '@pnpm/error';
4
+ import gfs from '@pnpm/fs.graceful-fs';
5
+ import fsx from 'fs-extra';
6
+ import sanitizeFilename from 'sanitize-filename';
7
+ export function getUniqueFileMap(fileMap) {
8
+ const lowercaseFiles = new Map();
9
+ const conflictingFileNames = new Map();
10
+ const uniqueFileMap = new Map();
11
+ for (const filename of Array.from(fileMap.keys()).sort()) {
12
+ const lowercaseFilename = filename.toLowerCase();
13
+ if (lowercaseFiles.has(lowercaseFilename)) {
14
+ conflictingFileNames.set(filename, lowercaseFiles.get(lowercaseFilename));
15
+ continue;
16
+ }
17
+ lowercaseFiles.set(lowercaseFilename, filename);
18
+ uniqueFileMap.set(filename, fileMap.get(filename));
19
+ }
20
+ return {
21
+ conflictingFileNames,
22
+ uniqueFileMap,
23
+ };
24
+ }
25
+ export function sanitizeFilenames(filenames) {
26
+ const sanitizedFilenames = new Map();
27
+ const invalidFilenames = [];
28
+ for (const [filename, src] of filenames) {
29
+ const sanitizedFilename = filename.split('/').map((f) => sanitizeFilename(f)).join('/');
30
+ if (sanitizedFilename !== filename) {
31
+ invalidFilenames.push(filename);
32
+ }
33
+ sanitizedFilenames.set(sanitizedFilename, src);
34
+ }
35
+ return { sanitizedFilenames, invalidFilenames };
36
+ }
37
+ export function moveOrMergeModulesDirs(src, dest) {
38
+ try {
39
+ renameEvenAcrossDevices(src, dest);
40
+ }
41
+ catch (err) {
42
+ switch (isError(err) && 'code' in err && err.code) {
43
+ case 'ENOENT':
44
+ return;
45
+ case 'ENOTEMPTY':
46
+ case 'EPERM':
47
+ mergeModulesDirs(src, dest);
48
+ return;
49
+ default:
50
+ throw err;
51
+ }
52
+ }
53
+ }
54
+ function renameEvenAcrossDevices(src, dest) {
55
+ try {
56
+ gfs.renameSync(src, dest);
57
+ }
58
+ catch (err) {
59
+ if (!(isError(err) && 'code' in err && err.code === 'EXDEV'))
60
+ throw err;
61
+ fsx.copySync(src, dest);
62
+ }
63
+ }
64
+ function mergeModulesDirs(src, dest) {
65
+ const srcFiles = fs.readdirSync(src);
66
+ const destFiles = new Set(fs.readdirSync(dest));
67
+ const filesToMove = srcFiles.filter((file) => !destFiles.has(file));
68
+ for (const file of filesToMove) {
69
+ renameEvenAcrossDevices(path.join(src, file), path.join(dest, file));
70
+ }
71
+ }
72
+ //# sourceMappingURL=fileMapUtils.js.map
@@ -0,0 +1 @@
1
+ export declare function filesHaveEqualContents(left: string, right: string): boolean;
@@ -0,0 +1,32 @@
1
+ import fs from 'node:fs';
2
+ const FILE_COMPARE_BUFFER_SIZE = 64 * 1024;
3
+ export function filesHaveEqualContents(left, right) {
4
+ const leftBuffer = Buffer.allocUnsafe(FILE_COMPARE_BUFFER_SIZE);
5
+ const rightBuffer = Buffer.allocUnsafe(FILE_COMPARE_BUFFER_SIZE);
6
+ const leftFd = fs.openSync(left, 'r');
7
+ try {
8
+ const rightFd = fs.openSync(right, 'r');
9
+ try {
10
+ return compareOpenFiles(leftFd, rightFd, leftBuffer, rightBuffer);
11
+ }
12
+ finally {
13
+ fs.closeSync(rightFd);
14
+ }
15
+ }
16
+ finally {
17
+ fs.closeSync(leftFd);
18
+ }
19
+ }
20
+ function compareOpenFiles(leftFd, rightFd, leftBuffer, rightBuffer) {
21
+ for (;;) {
22
+ const leftBytes = fs.readSync(leftFd, leftBuffer, 0, leftBuffer.length, null);
23
+ const rightBytes = fs.readSync(rightFd, rightBuffer, 0, rightBuffer.length, null);
24
+ if (leftBytes !== rightBytes)
25
+ return false;
26
+ if (leftBytes === 0)
27
+ return true;
28
+ if (!leftBuffer.subarray(0, leftBytes).equals(rightBuffer.subarray(0, rightBytes)))
29
+ return false;
30
+ }
31
+ }
32
+ //# sourceMappingURL=filesHaveEqualContents.js.map
@@ -15,4 +15,10 @@ export interface ImportIndexedDirOptions {
15
15
  /** Symlinks to create in the package, keyed by their path relative to it, with their targets. */
16
16
  symlinks?: Map<string, string>;
17
17
  }
18
+ export interface IndexedDirImport {
19
+ importer: Importer;
20
+ newDir: string;
21
+ filenames: Map<string, string>;
22
+ opts: ImportIndexedDirOptions;
23
+ }
18
24
  export declare function importIndexedDir(importer: Importer, newDir: string, filenames: Map<string, string>, opts: ImportIndexedDirOptions): void;
@@ -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
package/lib/index.js CHANGED
@@ -47,31 +47,41 @@ function createImportPackage(packageImportMethod, opts) {
47
47
  throw new Error(`Unknown package import method ${packageImportMethod}`);
48
48
  }
49
49
  }
50
+ function probeCloneAndSwitch(to, opts, createOpts) {
51
+ if (process.platform === 'win32')
52
+ return undefined;
53
+ try {
54
+ if (!tryClonePkg(to, opts))
55
+ return undefined;
56
+ if (!createOpts?.disableLogging)
57
+ packageImportMethodLogger.debug({ method: 'clone' });
58
+ return 'clone';
59
+ }
60
+ catch {
61
+ return undefined;
62
+ }
63
+ }
64
+ function handleHardlinkProbeError(err, to, opts, createOpts) {
65
+ assert(isError(err));
66
+ if (err.message.startsWith('EXDEV: cross-device link not permitted')) {
67
+ globalWarn(err.message);
68
+ globalInfo('Falling back to copying packages from store');
69
+ if (!createOpts?.disableLogging)
70
+ packageImportMethodLogger.debug({ method: 'copy' });
71
+ return { nextAuto: copyPkg, result: copyPkg(to, opts) };
72
+ }
73
+ if (!createOpts?.disableLogging)
74
+ packageImportMethodLogger.debug({ method: 'hardlink' });
75
+ const nextAuto = hardlinkPkg.bind(null, linkOrCopy);
76
+ return { nextAuto, result: nextAuto(to, opts) };
77
+ }
50
78
  function createAutoImporter(createOpts) {
51
79
  let auto = initialAuto;
52
80
  return (to, opts) => auto(to, opts);
53
81
  function initialAuto(to, opts) {
54
- // Although reflinks are supported on Windows Dev Drives,
55
- // they are 10x slower than hard links.
56
- // Hence, we prefer reflinks by default only on Linux and macOS.
57
- if (process.platform !== 'win32') {
58
- try {
59
- // Probe with the raw clone function (no ENOTSUP fallback).
60
- // On filesystems that don't support reflinks (e.g. ext4), this
61
- // throws and we fall through to hardlinks — which is much faster
62
- // than copying. If the probe succeeds, we switch to the full
63
- // clone importer (with ENOTSUP fallback for transient failures
64
- // during heavy parallel I/O) for all subsequent packages.
65
- if (!tryClonePkg(to, opts))
66
- return undefined;
67
- if (!createOpts?.disableLogging)
68
- packageImportMethodLogger.debug({ method: 'clone' });
69
- auto = createClonePkg();
70
- return 'clone';
71
- }
72
- catch {
73
- // ignore
74
- }
82
+ if (probeCloneAndSwitch(to, opts, createOpts)) {
83
+ auto = createClonePkg();
84
+ return 'clone';
75
85
  }
76
86
  try {
77
87
  if (!hardlinkPkg(linkOrThrowOnLinkFailure, to, opts))
@@ -82,20 +92,9 @@ function createAutoImporter(createOpts) {
82
92
  return 'hardlink';
83
93
  }
84
94
  catch (err) {
85
- assert(isError(err));
86
- if (err.message.startsWith('EXDEV: cross-device link not permitted')) {
87
- globalWarn(err.message);
88
- globalInfo('Falling back to copying packages from store');
89
- if (!createOpts?.disableLogging)
90
- packageImportMethodLogger.debug({ method: 'copy' });
91
- auto = copyPkg;
92
- return auto(to, opts);
93
- }
94
- // We still choose hard linking that will fall back to copying in edge cases.
95
- if (!createOpts?.disableLogging)
96
- packageImportMethodLogger.debug({ method: 'hardlink' });
97
- auto = hardlinkPkg.bind(null, linkOrCopy);
98
- return auto(to, opts);
95
+ const { nextAuto, result } = handleHardlinkProbeError(err, to, opts, createOpts);
96
+ auto = nextAuto;
97
+ return result;
99
98
  }
100
99
  }
101
100
  }
@@ -141,17 +140,8 @@ function tryClonePkg(to, opts) {
141
140
  }
142
141
  return undefined;
143
142
  }
144
- /**
145
- * Creates a clone-based package importer. Reflinks are atomic, so clone can
146
- * serve as both importFile and importFileAtomic. However, on Linux
147
- * copy_file_range can transiently fail with ENOTSUP under heavy parallel I/O,
148
- * so we fall back to copy on ENOTSUP. Regular files use a simple copy;
149
- * package.json (the completion marker) uses a temp+rename fallback to stay
150
- * atomic.
151
- */
152
- function createClonePkg() {
153
- const clone = alignedClone(createCloneFunction());
154
- const withFallback = (fallback) => (src, dest) => {
143
+ function withCloneFallback(clone, fallback) {
144
+ return (src, dest) => {
155
145
  try {
156
146
  clone(src, dest);
157
147
  }
@@ -163,9 +153,20 @@ function createClonePkg() {
163
153
  throw err;
164
154
  }
165
155
  };
156
+ }
157
+ /**
158
+ * Creates a clone-based package importer. Reflinks are atomic, so clone can
159
+ * serve as both importFile and importFileAtomic. However, on Linux
160
+ * copy_file_range can transiently fail with ENOTSUP under heavy parallel I/O,
161
+ * so we fall back to copy on ENOTSUP. Regular files use a simple copy;
162
+ * package.json (the completion marker) uses a temp+rename fallback to stay
163
+ * atomic.
164
+ */
165
+ function createClonePkg() {
166
+ const clone = alignedClone(createCloneFunction());
166
167
  const importer = {
167
- importFile: withFallback(resilientCopyFileSync),
168
- importFileAtomic: withFallback(atomicCopyFileSync),
168
+ importFile: withCloneFallback(clone, resilientCopyFileSync),
169
+ importFileAtomic: withCloneFallback(clone, atomicCopyFileSync),
169
170
  };
170
171
  return (to, opts) => {
171
172
  if (shouldImportPkg(to, opts)) {
@@ -204,37 +205,40 @@ function pickFileFromFilesMap(filesMap) {
204
205
  return filesMap.keys().next().value;
205
206
  }
206
207
  let _cloneFunction;
208
+ function createNativeReflinkClone() {
209
+ // eslint-disable-next-line @typescript-eslint/no-require-imports -- loads the native addon only on the platforms that use it
210
+ const { reflinkFileSync } = require('@reflink/reflink');
211
+ return (fr, to) => {
212
+ try {
213
+ reflinkFileSync(fr, to);
214
+ }
215
+ catch (err) {
216
+ if (!isError(err) || !('code' in err) || err.code !== 'EEXIST')
217
+ throw err;
218
+ }
219
+ };
220
+ }
221
+ function createPosixReflinkClone() {
222
+ return (src, dest) => {
223
+ try {
224
+ fs.copyFileSync(src, dest, constants.COPYFILE_FICLONE_FORCE);
225
+ }
226
+ catch (err) {
227
+ if (!(isError(err) && 'code' in err && err.code === 'EEXIST'))
228
+ throw err;
229
+ }
230
+ };
231
+ }
207
232
  function createCloneFunction() {
208
233
  if (_cloneFunction)
209
234
  return _cloneFunction;
210
235
  // Node.js currently does not natively support reflinks on Windows and macOS.
211
236
  // Hence, we use a third party solution.
212
237
  if (process.platform === 'darwin' || process.platform === 'win32') {
213
- // eslint-disable-next-line
214
- const { reflinkFileSync } = require('@reflink/reflink');
215
- _cloneFunction = (fr, to) => {
216
- try {
217
- reflinkFileSync(fr, to);
218
- }
219
- catch (err) {
220
- // If the file already exists, then we just proceed.
221
- // This will probably only happen if the package's index file contains the same file twice.
222
- // For instance: { "index.js": "hash", "./index.js": "hash" }
223
- if (!isError(err) || !('code' in err) || err.code !== 'EEXIST')
224
- throw err;
225
- }
226
- };
238
+ _cloneFunction = createNativeReflinkClone();
227
239
  }
228
240
  else {
229
- _cloneFunction = (src, dest) => {
230
- try {
231
- fs.copyFileSync(src, dest, constants.COPYFILE_FICLONE_FORCE);
232
- }
233
- catch (err) {
234
- if (!(isError(err) && 'code' in err && err.code === 'EEXIST'))
235
- throw err;
236
- }
237
- };
241
+ _cloneFunction = createPosixReflinkClone();
238
242
  }
239
243
  return _cloneFunction;
240
244
  }
@@ -262,16 +266,13 @@ function shouldRelinkPkg(to, opts) {
262
266
  // target. Only skip the relink when there is something in the target
263
267
  // worth preserving — an empty or missing target has nothing to lose, and
264
268
  // still needs the relink to create it so the post-build resync has
265
- // somewhere to write into. `hardlinkPkg` checks this same condition ahead
266
- // of its own `opts.force`, so this only runs once that has already passed.
269
+ // somewhere to write into.
267
270
  if (opts.resolvedFrom === 'local-dir' && opts.sourceExists === false) {
268
271
  return targetHasNothingToLose(to);
269
272
  }
270
273
  return opts.resolvedFrom !== 'store' || !pkgLinkedToStore(opts.filesMap, to);
271
274
  }
272
- // See `shouldRelinkPkg`'s comment on the missing-source case. Pulled out so
273
- // `hardlinkPkg` can apply the same guard ahead of its `opts.force` check,
274
- // which would otherwise bypass it.
275
+ // See `shouldRelinkPkg`'s comment on the missing-source case.
275
276
  function missingSourceHasSomethingToPreserve(to, opts) {
276
277
  return opts.resolvedFrom === 'local-dir' && opts.sourceExists === false && !targetHasNothingToLose(to);
277
278
  }
@@ -308,8 +309,8 @@ function isCafsFile(src) {
308
309
  return digestEnd - nameSep - 1 >= CAFS_DIGEST_MIN_LENGTH && isLowerHex(src, nameSep + 1, digestEnd);
309
310
  }
310
311
  function isLowerHex(value, from, to) {
311
- for (let i = from; i < to; i++) {
312
- const code = value.charCodeAt(i);
312
+ for (let index = from; index < to; index++) {
313
+ const code = value.charCodeAt(index);
313
314
  if (!((code >= 0x30 && code <= 0x39) || (code >= 0x61 && code <= 0x66)))
314
315
  return false;
315
316
  }
@@ -418,7 +419,7 @@ function atomicCopyFileSync(src, dest) {
418
419
  try {
419
420
  fs.unlinkSync(tmp);
420
421
  }
421
- catch { } // eslint-disable-line:no-empty
422
+ catch { }
422
423
  throw err;
423
424
  }
424
425
  renameOverwriteSync(tmp, dest);
@@ -0,0 +1,8 @@
1
+ import type { ImportFile, IndexedDirImport } from './importIndexedDir.js';
2
+ export declare function repairIndexedDir({ importer, newDir, filenames, opts }: IndexedDirImport): void;
3
+ export declare function replaceFileIfDifferent(importFile: ImportFile, src: string, dest: string): void;
4
+ export declare function replaceSymlinkIfDifferent(target: string, dest: string): void;
5
+ export declare function allFilesMatch(dir: string, filenames: Map<string, string>): boolean;
6
+ export declare function allSymlinksMatch(dir: string, symlinks: Map<string, string> | undefined): boolean;
7
+ export declare function symlinkMatches(dest: string, target: string): boolean;
8
+ export declare function mismatchReason(target: string, src: string): string | undefined;
@@ -0,0 +1,134 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import gfs, { renameFileWithRetry } from '@pnpm/fs.graceful-fs';
4
+ import { globalInfo } from '@pnpm/logger';
5
+ import { fastPathTemp as pathTemp } from 'path-temp';
6
+ import { clearDirBlockingFile, clearDirentBlockingDir, makeFileMapDirs } from './dirUtils.js';
7
+ import { filesHaveEqualContents } from './filesHaveEqualContents.js';
8
+ export function repairIndexedDir({ importer, newDir, filenames, opts }) {
9
+ makeFileMapDirs(newDir, filenames, { clearBlockers: true });
10
+ let packageJsonSrc;
11
+ for (const [relativePath, src] of filenames) {
12
+ if (relativePath === 'package.json') {
13
+ packageJsonSrc = src;
14
+ continue;
15
+ }
16
+ replaceFileIfDifferent(importer.importFile, src, path.join(newDir, relativePath));
17
+ }
18
+ for (const [relativePath, target] of opts.symlinks ?? []) {
19
+ const dir = path.posix.dirname(relativePath);
20
+ if (dir !== '.')
21
+ clearDirentBlockingDir(newDir, dir);
22
+ replaceSymlinkIfDifferent(target, path.join(newDir, relativePath));
23
+ }
24
+ if (packageJsonSrc !== undefined) {
25
+ replaceFileIfDifferent(importer.importFile, packageJsonSrc, path.join(newDir, 'package.json'));
26
+ }
27
+ }
28
+ export function replaceFileIfDifferent(importFile, src, dest) {
29
+ if (mismatchReason(dest, src) === undefined)
30
+ return;
31
+ const tmp = pathTemp(dest);
32
+ try {
33
+ importFile(src, tmp);
34
+ }
35
+ catch (err) {
36
+ try {
37
+ fs.unlinkSync(tmp);
38
+ }
39
+ catch { }
40
+ throw err;
41
+ }
42
+ try {
43
+ clearDirBlockingFile(dest);
44
+ renameFileWithRetry(tmp, dest);
45
+ }
46
+ catch (err) {
47
+ try {
48
+ fs.unlinkSync(tmp);
49
+ }
50
+ catch { }
51
+ if (mismatchReason(dest, src) === undefined)
52
+ return;
53
+ throw err;
54
+ }
55
+ }
56
+ export function replaceSymlinkIfDifferent(target, dest) {
57
+ if (symlinkMatches(dest, target))
58
+ return;
59
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
60
+ const tmp = pathTemp(dest);
61
+ fs.symlinkSync(target, tmp);
62
+ try {
63
+ clearDirBlockingFile(dest);
64
+ renameFileWithRetry(tmp, dest);
65
+ }
66
+ catch (err) {
67
+ try {
68
+ fs.unlinkSync(tmp);
69
+ }
70
+ catch { }
71
+ if (symlinkMatches(dest, target))
72
+ return;
73
+ throw err;
74
+ }
75
+ }
76
+ export function allFilesMatch(dir, filenames) {
77
+ const markerSrc = filenames.get('package.json');
78
+ if (markerSrc !== undefined && !fileMatches(dir, 'package.json', markerSrc))
79
+ return false;
80
+ for (const [relativePath, src] of filenames) {
81
+ if (relativePath === 'package.json')
82
+ continue;
83
+ if (!fileMatches(dir, relativePath, src))
84
+ return false;
85
+ }
86
+ return true;
87
+ }
88
+ export function allSymlinksMatch(dir, symlinks) {
89
+ for (const [relativePath, target] of symlinks ?? []) {
90
+ if (!symlinkMatches(path.join(dir, relativePath), target)) {
91
+ globalInfo(`Re-importing "${dir}" because symlink "${relativePath}" does not point to "${target}"`);
92
+ return false;
93
+ }
94
+ }
95
+ return true;
96
+ }
97
+ export function symlinkMatches(dest, target) {
98
+ try {
99
+ return fs.readlinkSync(dest) === target;
100
+ }
101
+ catch {
102
+ return false;
103
+ }
104
+ }
105
+ function fileMatches(dir, relativePath, src) {
106
+ const reason = mismatchReason(path.join(dir, relativePath), src);
107
+ if (reason === undefined)
108
+ return true;
109
+ globalInfo(`Re-importing "${dir}" because file "${relativePath}" ${reason}`);
110
+ return false;
111
+ }
112
+ export function mismatchReason(target, src) {
113
+ try {
114
+ const targetStat = fs.lstatSync(target, { bigint: true });
115
+ if (!targetStat.isFile())
116
+ return 'is not a regular file';
117
+ const srcStat = gfs.statSync(src, { bigint: true });
118
+ const hasSameFileIdentity = targetStat.ino !== 0n &&
119
+ targetStat.dev !== 0n &&
120
+ targetStat.ino === srcStat.ino &&
121
+ targetStat.dev === srcStat.dev;
122
+ if (hasSameFileIdentity)
123
+ return undefined;
124
+ if (targetStat.size !== srcStat.size)
125
+ return 'has a different size';
126
+ if (!filesHaveEqualContents(target, src))
127
+ return 'has different content';
128
+ return undefined;
129
+ }
130
+ catch {
131
+ return 'is missing or unreadable';
132
+ }
133
+ }
134
+ //# sourceMappingURL=repairIndexedDir.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/fs.indexed-pkg-importer",
3
- "version": "1100.0.31",
3
+ "version": "1100.0.32",
4
4
  "description": "Replicates indexed directories using hard links, copies, or cloning",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -37,10 +37,10 @@
37
37
  "test": "test"
38
38
  },
39
39
  "dependencies": {
40
- "@pnpm/core-loggers": "1101.0.1",
40
+ "@pnpm/core-loggers": "1101.0.2",
41
41
  "@pnpm/error": "1100.2.1",
42
42
  "@pnpm/fs.graceful-fs": "1100.2.4",
43
- "@pnpm/store.controller-types": "1101.3.2",
43
+ "@pnpm/store.controller-types": "1101.3.3",
44
44
  "@reflink/reflink": "0.1.19",
45
45
  "@zkochan/rimraf": "^4.0.0",
46
46
  "fs-extra": "^11.4.1",
@@ -55,9 +55,9 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "@jest/globals": "30.4.1",
58
- "@pnpm/fs.indexed-pkg-importer": "1100.0.31",
58
+ "@pnpm/fs.indexed-pkg-importer": "1100.0.32",
59
59
  "@pnpm/logger": "1100.0.0",
60
- "@pnpm/prepare": "1100.0.33",
60
+ "@pnpm/prepare": "1100.0.35",
61
61
  "@types/fs-extra": "^11.0.4"
62
62
  },
63
63
  "engines": {