@pnpm/fs.indexed-pkg-importer 1100.0.30 → 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/lib/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import assert from 'node:assert';
2
- import { constants, existsSync } from 'node:fs';
2
+ import { chmodSync, constants, existsSync } from 'node:fs';
3
3
  import path from 'node:path';
4
- import util from 'node:util';
5
4
  import { packageImportMethodLogger } from '@pnpm/core-loggers';
5
+ import { isError } from '@pnpm/error';
6
6
  import fs from '@pnpm/fs.graceful-fs';
7
7
  import { globalInfo, globalWarn } from '@pnpm/logger';
8
8
  import { fastPathTemp as pathTemp } from 'path-temp';
@@ -10,6 +10,10 @@ import { renameOverwriteSync } from 'rename-overwrite';
10
10
  import { importIndexedDir } from './importIndexedDir.js';
11
11
  import { isNativeBinary, removeQuarantine } from './removeQuarantine.js';
12
12
  export {};
13
+ const BASE_FILE_MODE = 0o666;
14
+ const EXEC_FILE_MODE = 0o755;
15
+ const CAFS_EXEC_SUFFIX = '-exec';
16
+ const CAFS_DIGEST_MIN_LENGTH = 40;
13
17
  export function createIndexedPkgImporter(packageImportMethod, opts) {
14
18
  const importPackage = createImportPackage(packageImportMethod, opts);
15
19
  return importPackage;
@@ -43,34 +47,44 @@ function createImportPackage(packageImportMethod, opts) {
43
47
  throw new Error(`Unknown package import method ${packageImportMethod}`);
44
48
  }
45
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
+ }
46
78
  function createAutoImporter(createOpts) {
47
79
  let auto = initialAuto;
48
80
  return (to, opts) => auto(to, opts);
49
81
  function initialAuto(to, opts) {
50
- // Although reflinks are supported on Windows Dev Drives,
51
- // they are 10x slower than hard links.
52
- // Hence, we prefer reflinks by default only on Linux and macOS.
53
- if (process.platform !== 'win32') {
54
- try {
55
- // Probe with the raw clone function (no ENOTSUP fallback).
56
- // On filesystems that don't support reflinks (e.g. ext4), this
57
- // throws and we fall through to hardlinks — which is much faster
58
- // than copying. If the probe succeeds, we switch to the full
59
- // clone importer (with ENOTSUP fallback for transient failures
60
- // during heavy parallel I/O) for all subsequent packages.
61
- if (!tryClonePkg(to, opts))
62
- return undefined;
63
- if (!createOpts?.disableLogging)
64
- packageImportMethodLogger.debug({ method: 'clone' });
65
- auto = createClonePkg();
66
- return 'clone';
67
- }
68
- catch {
69
- // ignore
70
- }
82
+ if (probeCloneAndSwitch(to, opts, createOpts)) {
83
+ auto = createClonePkg();
84
+ return 'clone';
71
85
  }
72
86
  try {
73
- if (!hardlinkPkg(fs.linkSync, to, opts))
87
+ if (!hardlinkPkg(linkOrThrowOnLinkFailure, to, opts))
74
88
  return undefined;
75
89
  if (!createOpts?.disableLogging)
76
90
  packageImportMethodLogger.debug({ method: 'hardlink' });
@@ -78,20 +92,9 @@ function createAutoImporter(createOpts) {
78
92
  return 'hardlink';
79
93
  }
80
94
  catch (err) {
81
- assert(util.types.isNativeError(err));
82
- if (err.message.startsWith('EXDEV: cross-device link not permitted')) {
83
- globalWarn(err.message);
84
- globalInfo('Falling back to copying packages from store');
85
- if (!createOpts?.disableLogging)
86
- packageImportMethodLogger.debug({ method: 'copy' });
87
- auto = copyPkg;
88
- return auto(to, opts);
89
- }
90
- // We still choose hard linking that will fall back to copying in edge cases.
91
- if (!createOpts?.disableLogging)
92
- packageImportMethodLogger.debug({ method: 'hardlink' });
93
- auto = hardlinkPkg.bind(null, linkOrCopy);
94
- return auto(to, opts);
95
+ const { nextAuto, result } = handleHardlinkProbeError(err, to, opts, createOpts);
96
+ auto = nextAuto;
97
+ return result;
95
98
  }
96
99
  }
97
100
  }
@@ -116,6 +119,12 @@ function createCloneOrCopyImporter(createOpts) {
116
119
  return auto(to, opts);
117
120
  }
118
121
  }
122
+ function alignedClone(clone) {
123
+ return (src, dest) => {
124
+ clone(src, dest);
125
+ alignStoreFileMode(src, dest);
126
+ };
127
+ }
119
128
  /**
120
129
  * Import a single package using a raw clone function (no ENOTSUP fallback).
121
130
  * Used by auto-mode to probe whether the filesystem supports cloning.
@@ -123,42 +132,44 @@ function createCloneOrCopyImporter(createOpts) {
123
132
  * through to a faster method (e.g. hardlinks).
124
133
  */
125
134
  function tryClonePkg(to, opts) {
126
- if (opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap)) {
127
- const clone = createCloneFunction();
135
+ if (shouldImportPkg(to, opts)) {
136
+ const clone = alignedClone(createCloneFunction());
128
137
  importIndexedDir({ importFile: clone, importFileAtomic: clone }, to, opts.filesMap, opts);
129
138
  removeQuarantineFromNativeBinaries(to, opts);
130
139
  return 'clone';
131
140
  }
132
141
  return undefined;
133
142
  }
134
- /**
135
- * Creates a clone-based package importer. Reflinks are atomic, so clone can
136
- * serve as both importFile and importFileAtomic. However, on Linux
137
- * copy_file_range can transiently fail with ENOTSUP under heavy parallel I/O,
138
- * so we fall back to copy on ENOTSUP. Regular files use a simple copy;
139
- * package.json (the completion marker) uses a temp+rename fallback to stay
140
- * atomic.
141
- */
142
- function createClonePkg() {
143
- const clone = createCloneFunction();
144
- const withFallback = (fallback) => (src, dest) => {
143
+ function withCloneFallback(clone, fallback) {
144
+ return (src, dest) => {
145
145
  try {
146
146
  clone(src, dest);
147
147
  }
148
148
  catch (err) {
149
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOTSUP') {
149
+ if (isError(err) && 'code' in err && err.code === 'ENOTSUP') {
150
150
  fallback(src, dest);
151
151
  return;
152
152
  }
153
153
  throw err;
154
154
  }
155
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());
156
167
  const importer = {
157
- importFile: withFallback(resilientCopyFileSync),
158
- importFileAtomic: withFallback(atomicCopyFileSync),
168
+ importFile: withCloneFallback(clone, resilientCopyFileSync),
169
+ importFileAtomic: withCloneFallback(clone, atomicCopyFileSync),
159
170
  };
160
171
  return (to, opts) => {
161
- if (opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap)) {
172
+ if (shouldImportPkg(to, opts)) {
162
173
  importIndexedDir(importer, to, opts.filesMap, opts);
163
174
  removeQuarantineFromNativeBinaries(to, opts);
164
175
  return 'clone';
@@ -166,6 +177,18 @@ function createClonePkg() {
166
177
  return undefined;
167
178
  };
168
179
  }
180
+ // Whether to (re)import via clone or copy. A directory dependency's source
181
+ // can change without the lockfile changing, so these methods import it
182
+ // unconditionally by default, the same way `shouldRelinkPkg` does for the
183
+ // hardlink path below, including the same guard against wiping an
184
+ // already-materialized target when a `publishConfig.directory` source has
185
+ // not been built yet.
186
+ function shouldImportPkg(to, opts) {
187
+ if (opts.resolvedFrom === 'local-dir' && opts.sourceExists === false) {
188
+ return targetHasNothingToLose(to);
189
+ }
190
+ return opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap);
191
+ }
169
192
  function pkgExistsAtTargetDir(targetDir, filesMap) {
170
193
  return existsSync(path.join(targetDir, pickFileFromFilesMap(filesMap)));
171
194
  }
@@ -182,41 +205,48 @@ function pickFileFromFilesMap(filesMap) {
182
205
  return filesMap.keys().next().value;
183
206
  }
184
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
+ }
185
232
  function createCloneFunction() {
186
233
  if (_cloneFunction)
187
234
  return _cloneFunction;
188
235
  // Node.js currently does not natively support reflinks on Windows and macOS.
189
236
  // Hence, we use a third party solution.
190
237
  if (process.platform === 'darwin' || process.platform === 'win32') {
191
- // eslint-disable-next-line
192
- const { reflinkFileSync } = require('@reflink/reflink');
193
- _cloneFunction = (fr, to) => {
194
- try {
195
- reflinkFileSync(fr, to);
196
- }
197
- catch (err) {
198
- // If the file already exists, then we just proceed.
199
- // This will probably only happen if the package's index file contains the same file twice.
200
- // For instance: { "index.js": "hash", "./index.js": "hash" }
201
- if (!util.types.isNativeError(err) || !('code' in err) || err.code !== 'EEXIST')
202
- throw err;
203
- }
204
- };
238
+ _cloneFunction = createNativeReflinkClone();
205
239
  }
206
240
  else {
207
- _cloneFunction = (src, dest) => {
208
- try {
209
- fs.copyFileSync(src, dest, constants.COPYFILE_FICLONE_FORCE);
210
- }
211
- catch (err) {
212
- if (!(util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST'))
213
- throw err;
214
- }
215
- };
241
+ _cloneFunction = createPosixReflinkClone();
216
242
  }
217
243
  return _cloneFunction;
218
244
  }
219
245
  function hardlinkPkg(importFile, to, opts) {
246
+ // Checked ahead of `opts.force`: a caller-requested force must not be able
247
+ // to bypass the missing-source preservation below.
248
+ if (missingSourceHasSomethingToPreserve(to, opts))
249
+ return undefined;
220
250
  if (opts.force || shouldRelinkPkg(to, opts)) {
221
251
  importIndexedDir({ importFile, importFileAtomic: importFile }, to, opts.filesMap, opts);
222
252
  removeQuarantineFromNativeBinaries(to, opts);
@@ -226,24 +256,97 @@ function hardlinkPkg(importFile, to, opts) {
226
256
  }
227
257
  function shouldRelinkPkg(to, opts) {
228
258
  if (opts.disableRelinkLocalDirDeps && opts.resolvedFrom === 'local-dir') {
229
- try {
230
- const files = fs.readdirSync(to);
231
- return files.length === 0 || files.length === 1 && files[0] === 'node_modules';
232
- }
233
- catch {
234
- return true;
235
- }
259
+ return targetHasNothingToLose(to);
260
+ }
261
+ // A directory dependency relinks on every install by default, since its
262
+ // source can change without the lockfile changing. But when the source is
263
+ // a `publishConfig.directory` its own `prepare` script has not (re)built
264
+ // yet, the fetcher tolerates the missing directory and comes back with an
265
+ // empty `filesMap`; relinking from that would wipe an already-materialized
266
+ // target. Only skip the relink when there is something in the target
267
+ // worth preserving — an empty or missing target has nothing to lose, and
268
+ // still needs the relink to create it so the post-build resync has
269
+ // somewhere to write into.
270
+ if (opts.resolvedFrom === 'local-dir' && opts.sourceExists === false) {
271
+ return targetHasNothingToLose(to);
236
272
  }
237
273
  return opts.resolvedFrom !== 'store' || !pkgLinkedToStore(opts.filesMap, to);
238
274
  }
275
+ // See `shouldRelinkPkg`'s comment on the missing-source case.
276
+ function missingSourceHasSomethingToPreserve(to, opts) {
277
+ return opts.resolvedFrom === 'local-dir' && opts.sourceExists === false && !targetHasNothingToLose(to);
278
+ }
279
+ function targetHasNothingToLose(to) {
280
+ try {
281
+ const files = fs.readdirSync(to);
282
+ return files.length === 0 || files.length === 1 && files[0] === 'node_modules';
283
+ }
284
+ catch {
285
+ return true;
286
+ }
287
+ }
288
+ // The CLI never changes its own umask and the import path asks for it once
289
+ // per file, so read it once.
290
+ let umask;
291
+ function storeEntryMode(executable) {
292
+ umask ??= process.umask();
293
+ return ((executable ? EXEC_FILE_MODE : BASE_FILE_MODE) & ~umask) & 0o777;
294
+ }
295
+ // The exact inverse of the CAFS layout `<store>/v<N>/files/<2 hex digits>/<digest>[-exec]`.
296
+ // This importer also materializes local-directory dependencies, whose files keep
297
+ // the modes their project gives them, so only a real store entry is re-derived.
298
+ function isCafsFile(src) {
299
+ const nameSep = src.lastIndexOf(path.sep);
300
+ if (nameSep < 0)
301
+ return false;
302
+ const shardSep = src.lastIndexOf(path.sep, nameSep - 1);
303
+ if (shardSep < 0 || nameSep - shardSep - 1 !== 2 || !isLowerHex(src, shardSep + 1, nameSep))
304
+ return false;
305
+ const filesSep = src.lastIndexOf(path.sep, shardSep - 1);
306
+ if (filesSep < 0 || shardSep - filesSep - 1 !== 'files'.length || !src.startsWith('files', filesSep + 1))
307
+ return false;
308
+ const digestEnd = src.endsWith(CAFS_EXEC_SUFFIX) ? src.length - CAFS_EXEC_SUFFIX.length : src.length;
309
+ return digestEnd - nameSep - 1 >= CAFS_DIGEST_MIN_LENGTH && isLowerHex(src, nameSep + 1, digestEnd);
310
+ }
311
+ function isLowerHex(value, from, to) {
312
+ for (let index = from; index < to; index++) {
313
+ const code = value.charCodeAt(index);
314
+ if (!((code >= 0x30 && code <= 0x39) || (code >= 0x61 && code <= 0x66)))
315
+ return false;
316
+ }
317
+ return true;
318
+ }
319
+ // A store file carries the umask that was active when it was written, so
320
+ // importing has to apply the current one as though the entry were just
321
+ // written (pnpm/pnpm#3807). POSIX mode bits do not apply on Windows.
322
+ function storeEntryModeForSource(src) {
323
+ if (process.platform === 'win32')
324
+ return undefined;
325
+ return isCafsFile(src) ? storeEntryMode(src.endsWith(CAFS_EXEC_SUFFIX)) : undefined;
326
+ }
327
+ function alignStoreFileMode(src, dest) {
328
+ const mode = storeEntryModeForSource(src);
329
+ if (mode === undefined)
330
+ return;
331
+ chmodSync(dest, mode);
332
+ }
333
+ function storeModeDiffers(existingPath) {
334
+ const storeMode = storeEntryModeForSource(existingPath);
335
+ return storeMode !== undefined && (fs.statSync(existingPath).mode & 0o777) !== storeMode;
336
+ }
239
337
  function linkOrCopy(existingPath, newPath) {
338
+ // A hardlink would pin the store-population mode onto the import.
339
+ if (storeModeDiffers(existingPath)) {
340
+ resilientCopyFileSync(existingPath, newPath);
341
+ return;
342
+ }
240
343
  try {
241
344
  fs.linkSync(existingPath, newPath);
242
345
  }
243
346
  catch (err) {
244
347
  // If a hard link to the same file already exists
245
348
  // then trying to copy it will make an empty file from it.
246
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST')
349
+ if (isError(err) && 'code' in err && err.code === 'EEXIST')
247
350
  return;
248
351
  // In some VERY rare cases (1 in a thousand), hard-link creation fails on Windows.
249
352
  // In that case, we just fall back to copying.
@@ -251,16 +354,27 @@ function linkOrCopy(existingPath, newPath) {
251
354
  resilientCopyFileSync(existingPath, newPath);
252
355
  }
253
356
  }
357
+ // The auto-mode hardlink probe: a filesystem that cannot hardlink must keep
358
+ // failing here, so auto backs off to copy.
359
+ function linkOrThrowOnLinkFailure(existingPath, newPath) {
360
+ if (storeModeDiffers(existingPath)) {
361
+ resilientCopyFileSync(existingPath, newPath);
362
+ return;
363
+ }
364
+ fs.linkSync(existingPath, newPath);
365
+ }
254
366
  // On Linux CI, the kernel's copy_file_range/sendfile can transiently fail
255
367
  // with ENOTSUP under heavy parallel I/O on the same store files.
256
368
  // Fall back to manual read+write which uses plain read/write syscalls.
257
369
  function resilientCopyFileSync(src, dest) {
258
370
  try {
259
371
  fs.copyFileSync(src, dest);
372
+ alignStoreFileMode(src, dest);
260
373
  }
261
374
  catch (err) {
262
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOTSUP') {
263
- const srcMode = fs.statSync(src).mode;
375
+ if (isError(err) && 'code' in err && err.code === 'ENOTSUP') {
376
+ const storeMode = storeEntryModeForSource(src);
377
+ const srcMode = storeMode ?? fs.statSync(src).mode;
264
378
  fs.writeFileSync(dest, fs.readFileSync(src), { mode: srcMode });
265
379
  }
266
380
  else {
@@ -276,7 +390,7 @@ function pkgLinkedToStore(filesMap, linkedPkgDir) {
276
390
  stats0 = fs.statSync(linkedFile);
277
391
  }
278
392
  catch (err) {
279
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
393
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
280
394
  return false;
281
395
  }
282
396
  const stats1 = fs.statSync(filesMap.get(filename));
@@ -286,7 +400,7 @@ function pkgLinkedToStore(filesMap, linkedPkgDir) {
286
400
  return false;
287
401
  }
288
402
  export function copyPkg(to, opts) {
289
- if (opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap)) {
403
+ if (shouldImportPkg(to, opts)) {
290
404
  // copyFileSync is not atomic on non-COW filesystems: a crash mid-copy
291
405
  // can leave a partially-written file. package.json is the completion
292
406
  // marker, so it must be written atomically via temp file + rename.
@@ -305,7 +419,7 @@ function atomicCopyFileSync(src, dest) {
305
419
  try {
306
420
  fs.unlinkSync(tmp);
307
421
  }
308
- catch { } // eslint-disable-line:no-empty
422
+ catch { }
309
423
  throw err;
310
424
  }
311
425
  renameOverwriteSync(tmp, dest);
@@ -1,5 +1,6 @@
1
1
  import { execFileSync } from 'node:child_process';
2
2
  import path from 'node:path';
3
+ import { isError } from '@pnpm/error';
3
4
  import { globalWarn } from '@pnpm/logger';
4
5
  const QUARANTINE_ATTR = 'com.apple.quarantine';
5
6
  // Quarantine removal only ever runs on macOS, so the set lists the binary
@@ -85,6 +86,6 @@ function getStderr(err) {
85
86
  if (stderr != null)
86
87
  return stderr.toString();
87
88
  }
88
- return err instanceof Error ? err.message : String(err);
89
+ return isError(err) ? err.message : String(err);
89
90
  }
90
91
  //# sourceMappingURL=removeQuarantine.js.map
@@ -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.30",
3
+ "version": "1100.0.32",
4
4
  "description": "Replicates indexed directories using hard links, copies, or cloning",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -37,9 +37,10 @@
37
37
  "test": "test"
38
38
  },
39
39
  "dependencies": {
40
- "@pnpm/core-loggers": "1101.0.1",
41
- "@pnpm/fs.graceful-fs": "1100.2.3",
42
- "@pnpm/store.controller-types": "1101.3.1",
40
+ "@pnpm/core-loggers": "1101.0.2",
41
+ "@pnpm/error": "1100.2.1",
42
+ "@pnpm/fs.graceful-fs": "1100.2.4",
43
+ "@pnpm/store.controller-types": "1101.3.3",
43
44
  "@reflink/reflink": "0.1.19",
44
45
  "@zkochan/rimraf": "^4.0.0",
45
46
  "fs-extra": "^11.4.1",
@@ -54,9 +55,9 @@
54
55
  },
55
56
  "devDependencies": {
56
57
  "@jest/globals": "30.4.1",
57
- "@pnpm/fs.indexed-pkg-importer": "1100.0.30",
58
+ "@pnpm/fs.indexed-pkg-importer": "1100.0.32",
58
59
  "@pnpm/logger": "1100.0.0",
59
- "@pnpm/prepare": "1100.0.32",
60
+ "@pnpm/prepare": "1100.0.35",
60
61
  "@types/fs-extra": "^11.0.4"
61
62
  },
62
63
  "engines": {