@pnpm/fs.indexed-pkg-importer 1100.0.34 → 1100.0.35

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,17 @@
1
1
  # @pnpm/fs.indexed-pkg-importer
2
2
 
3
+ ## 1100.0.35
4
+
5
+ ### Patch Changes
6
+
7
+ - Concurrent installs that import the same package into one project now all complete. One of them used to fail with `ENOTEMPTY` when its copy of the package collided with the copy another install had already placed [pnpm/tasks#48](https://github.com/pnpm/tasks/issues/48).
8
+
9
+ - A script listed in `syncInjectedDepsAfterScripts` now keeps a package's links to its own files as links in the injected copies, the way `pnpm install` writes them. It no longer fails when the package links to one directory twice or links back to a directory above the link. `pnpm install` with `deployAllFiles` no longer fails on such a link back either.
10
+
11
+ - Updated dependencies:
12
+ - @pnpm/core-loggers@1101.0.3
13
+ - @pnpm/store.controller-types@1101.3.4
14
+
3
15
  ## 1100.0.34
4
16
 
5
17
  ### Patch Changes
@@ -3,4 +3,9 @@ export interface SymlinkDirs {
3
3
  finalDir: string;
4
4
  imported: Set<string>;
5
5
  }
6
+ /**
7
+ * The target a copy of the link at `src` holds: the link's own, with an
8
+ * absolute one made relative to the link's directory.
9
+ */
10
+ export declare function internalSymlinkTarget(src: string): string;
6
11
  export declare function copyInternalSymlink(src: string, dest: string, links: SymlinkDirs): boolean;
@@ -1,11 +1,18 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { isError } from '@pnpm/error';
4
+ /**
5
+ * The target a copy of the link at `src` holds: the link's own, with an
6
+ * absolute one made relative to the link's directory.
7
+ */
8
+ export function internalSymlinkTarget(src) {
9
+ const target = fs.readlinkSync(src);
10
+ return path.isAbsolute(target)
11
+ ? path.relative(realpathOrSelf(path.dirname(src)), realpathOrSelf(target))
12
+ : target;
13
+ }
4
14
  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
- }
15
+ const target = internalSymlinkTarget(src);
9
16
  const resolved = path.resolve(path.dirname(dest), target);
10
17
  if (escapesDir(links.writtenDir, resolved))
11
18
  return false;
@@ -0,0 +1,3 @@
1
+ export declare function storeEntryModeForSource(src: string): number | undefined;
2
+ export declare function fileModeForImport(sourceMode: bigint, storeMode: number | undefined): number | undefined;
3
+ export declare function isLinkableStoreMode(mode: number, storeMode: number): boolean;
@@ -0,0 +1,57 @@
1
+ import path from 'node:path';
2
+ const BASE_FILE_MODE = 0o666;
3
+ const EXEC_FILE_MODE = 0o755;
4
+ const CAFS_EXEC_SUFFIX = '-exec';
5
+ const CAFS_DIGEST_MIN_LENGTH = 40;
6
+ // The CLI never changes its own umask and the import path asks for it once
7
+ // per file, so read it once.
8
+ let umask;
9
+ // A store file carries the umask that was active when it was written, so
10
+ // importing has to apply the current one as though the entry were just
11
+ // written (pnpm/pnpm#3807). POSIX mode bits do not apply on Windows.
12
+ export function storeEntryModeForSource(src) {
13
+ if (process.platform === 'win32' || !isCafsFile(src))
14
+ return undefined;
15
+ umask ??= process.umask();
16
+ return ((src.endsWith(CAFS_EXEC_SUFFIX) ? EXEC_FILE_MODE : BASE_FILE_MODE) & ~umask) & 0o777;
17
+ }
18
+ // The mode an import of `src` materializes: the store-entry mode for a store
19
+ // file, otherwise the source's own mode, which a local-directory dependency keeps.
20
+ export function fileModeForImport(sourceMode, storeMode) {
21
+ if (process.platform === 'win32')
22
+ return undefined;
23
+ return storeMode ?? Number(sourceMode & 511n);
24
+ }
25
+ // Group- and other-write may be missing, and group-write may be extra: a
26
+ // store write takes those bits from its shard directory, not from the umask,
27
+ // and the shared inode already grants them through the store. Every other bit
28
+ // must match, so a store populated under a wider umask is still not linked.
29
+ export function isLinkableStoreMode(mode, storeMode) {
30
+ const groupOtherWrite = 0o022;
31
+ return ((mode ^ storeMode) & 0o777 & ~groupOtherWrite) === 0 && (mode & ~storeMode & 0o002) === 0;
32
+ }
33
+ // The exact inverse of the CAFS layout `<store>/v<N>/files/<2 hex digits>/<digest>[-exec]`.
34
+ // This importer also materializes local-directory dependencies, whose files keep
35
+ // the modes their project gives them, so only a real store entry is re-derived.
36
+ function isCafsFile(src) {
37
+ const nameSep = src.lastIndexOf(path.sep);
38
+ if (nameSep < 0)
39
+ return false;
40
+ const shardSep = src.lastIndexOf(path.sep, nameSep - 1);
41
+ if (shardSep < 0 || nameSep - shardSep - 1 !== 2 || !isLowerHex(src, shardSep + 1, nameSep))
42
+ return false;
43
+ const filesSep = src.lastIndexOf(path.sep, shardSep - 1);
44
+ if (filesSep < 0 || shardSep - filesSep - 1 !== 'files'.length || !src.startsWith('files', filesSep + 1))
45
+ return false;
46
+ const digestEnd = src.endsWith(CAFS_EXEC_SUFFIX) ? src.length - CAFS_EXEC_SUFFIX.length : src.length;
47
+ return digestEnd - nameSep - 1 >= CAFS_DIGEST_MIN_LENGTH && isLowerHex(src, nameSep + 1, digestEnd);
48
+ }
49
+ function isLowerHex(value, from, to) {
50
+ for (let index = from; index < to; index++) {
51
+ const code = value.charCodeAt(index);
52
+ if (!((code >= 0x30 && code <= 0x39) || (code >= 0x61 && code <= 0x66)))
53
+ return false;
54
+ }
55
+ return true;
56
+ }
57
+ //# sourceMappingURL=fileMode.js.map
@@ -7,12 +7,15 @@ import { sanitizeInline } from '@pnpm/text.sanitize';
7
7
  import { rimrafSync } from '@zkochan/rimraf';
8
8
  import { makeEmptyDirSync } from 'make-empty-dir';
9
9
  import { fastPathTemp as pathTemp } from 'path-temp';
10
- import { renameOverwriteSync } from 'rename-overwrite';
11
10
  import { copyInternalSymlink } from './copyInternalSymlink.js';
12
11
  import { makeFileMapDirs } from './dirUtils.js';
13
12
  import { getUniqueFileMap, moveOrMergeModulesDirs, sanitizeFilenames, } from './fileMapUtils.js';
14
- import { allFilesMatch, allSymlinksMatch, repairIndexedDir, } from './repairIndexedDir.js';
13
+ import { overwriteDirSync } from './overwriteDir.js';
14
+ import { allFilesMatch, allImportedEntriesMatchExactly, allSymlinksMatch, repairIndexedDir, } from './repairIndexedDir.js';
15
15
  const filenameConflictsLogger = logger('_filename-conflicts');
16
+ const concurrentImportSettleSleep = new Int32Array(new SharedArrayBuffer(4));
17
+ const CONCURRENT_IMPORT_SETTLE_ATTEMPTS = 5;
18
+ const CONCURRENT_IMPORT_SETTLE_DELAY_MS = 20;
16
19
  export function importIndexedDir(importer, newDir, filenames, opts) {
17
20
  const dirImport = { importer, newDir, filenames, opts };
18
21
  if (opts.safeToSkip && opts.resolvedFrom !== 'local-dir') {
@@ -46,19 +49,51 @@ function importWithStaging(dirImport) {
46
49
  return;
47
50
  throw err;
48
51
  }
49
- commitStagedDir(stage, newDir);
52
+ commitStagedDir(stage, dirImport);
50
53
  }
51
- function commitStagedDir(stage, newDir) {
54
+ // A rename that fails because a concurrent import of the same package already
55
+ // completed the target is a success: the target holds exactly this import, and
56
+ // a preserved `node_modules/` moved into the stage goes back where it was.
57
+ function commitStagedDir(stage, { newDir, filenames, opts }) {
58
+ const keepModulesDir = opts.keepModulesDir === true;
59
+ const entries = { dir: newDir, filenames, symlinks: opts.symlinks, keepModulesDir };
52
60
  try {
61
+ let attempt = 0;
53
62
  withFileLockRetry(() => {
54
- renameOverwriteSync(stage, newDir);
63
+ if (attempt++ > 0 && allImportedEntriesMatchExactly(entries)) {
64
+ acceptExistingImport(stage, newDir, keepModulesDir);
65
+ return;
66
+ }
67
+ overwriteDirSync(stage, newDir);
55
68
  });
56
69
  }
57
70
  catch (renameErr) {
71
+ if (destinationHoldsThisImport(entries)) {
72
+ acceptExistingImport(stage, newDir, keepModulesDir);
73
+ return;
74
+ }
58
75
  cleanStageDir(stage);
59
76
  throw renameErr;
60
77
  }
61
78
  }
79
+ function acceptExistingImport(stage, newDir, keepModulesDir) {
80
+ if (keepModulesDir) {
81
+ moveOrMergeModulesDirs(path.join(stage, 'node_modules'), path.join(newDir, 'node_modules'));
82
+ }
83
+ cleanStageDir(stage);
84
+ }
85
+ // A concurrent import that is mid-swap leaves the destination absent or
86
+ // incomplete for a moment. Give it that moment before concluding that the
87
+ // destination is not this package.
88
+ function destinationHoldsThisImport(entries) {
89
+ for (let attempt = 1;; attempt++) {
90
+ if (allImportedEntriesMatchExactly(entries))
91
+ return true;
92
+ if (attempt === CONCURRENT_IMPORT_SETTLE_ATTEMPTS)
93
+ return false;
94
+ Atomics.wait(concurrentImportSettleSleep, 0, 0, CONCURRENT_IMPORT_SETTLE_DELAY_MS);
95
+ }
96
+ }
62
97
  function cleanStageDir(stage) {
63
98
  try {
64
99
  rimrafSync(stage);
@@ -131,18 +166,40 @@ function retryWithFixedFileMap(err, dirImport) {
131
166
  function tryExclusiveImport(importer, newDir, filenames, entries) {
132
167
  if (!tryExclusiveMkdir(newDir))
133
168
  return false;
169
+ const createdStat = safeLstat(newDir);
134
170
  try {
135
171
  tryImportIndexedDir(importer, newDir, filenames, entries);
136
172
  return true;
137
173
  }
138
174
  catch {
139
- try {
140
- rimrafSync(newDir);
175
+ if (sameCreatedDirectory(newDir, createdStat)) {
176
+ try {
177
+ rimrafSync(newDir);
178
+ }
179
+ catch { }
141
180
  }
142
- catch { }
143
181
  return false;
144
182
  }
145
183
  }
184
+ function safeLstat(dir) {
185
+ try {
186
+ return fs.lstatSync(dir, { bigint: true });
187
+ }
188
+ catch {
189
+ return undefined;
190
+ }
191
+ }
192
+ function sameCreatedDirectory(dir, createdStat) {
193
+ if (createdStat == null)
194
+ return false;
195
+ const currentStat = safeLstat(dir);
196
+ if (currentStat == null || !currentStat.isDirectory())
197
+ return false;
198
+ return currentStat.dev === createdStat.dev &&
199
+ currentStat.ino === createdStat.ino &&
200
+ currentStat.birthtimeNs === createdStat.birthtimeNs &&
201
+ !fs.existsSync(path.join(dir, 'package.json'));
202
+ }
146
203
  function retryWithSanitizedFilenames({ importer, newDir, filenames, opts }) {
147
204
  const { sanitizedFilenames, invalidFilenames } = sanitizeFilenames(filenames);
148
205
  if (invalidFilenames.length === 0)
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { FilesMap, ImportIndexedPackage, ImportOptions } from '@pnpm/store.controller-types';
2
2
  export { type FilesMap, type ImportIndexedPackage, type ImportOptions };
3
+ export { copyInternalSymlink, internalSymlinkTarget, type SymlinkDirs } from './copyInternalSymlink.js';
3
4
  export type PackageImportMethod = 'auto' | 'hardlink' | 'copy' | 'clone' | 'clone-or-copy';
4
5
  export interface CreateIndexedPkgImporterOptions {
5
6
  disableLogging?: boolean;
package/lib/index.js CHANGED
@@ -7,14 +7,12 @@ import fs from '@pnpm/fs.graceful-fs';
7
7
  import { globalInfo, globalWarn } from '@pnpm/logger';
8
8
  import { fastPathTemp as pathTemp } from 'path-temp';
9
9
  import { renameOverwriteSync } from 'rename-overwrite';
10
+ import { isLinkableStoreMode, storeEntryModeForSource } from './fileMode.js';
10
11
  import { hardlinkedDirMatches } from './hardlinkedDirMatches.js';
11
12
  import { importIndexedDir } from './importIndexedDir.js';
12
13
  import { isNativeBinary, removeQuarantine } from './removeQuarantine.js';
13
14
  export {};
14
- const BASE_FILE_MODE = 0o666;
15
- const EXEC_FILE_MODE = 0o755;
16
- const CAFS_EXEC_SUFFIX = '-exec';
17
- const CAFS_DIGEST_MIN_LENGTH = 40;
15
+ export { copyInternalSymlink, internalSymlinkTarget } from './copyInternalSymlink.js';
18
16
  export function createIndexedPkgImporter(packageImportMethod, opts) {
19
17
  const importPackage = createImportPackage(packageImportMethod, opts);
20
18
  return importPackage;
@@ -290,45 +288,6 @@ function targetHasNothingToLose(to) {
290
288
  return true;
291
289
  }
292
290
  }
293
- // The CLI never changes its own umask and the import path asks for it once
294
- // per file, so read it once.
295
- let umask;
296
- function storeEntryMode(executable) {
297
- umask ??= process.umask();
298
- return ((executable ? EXEC_FILE_MODE : BASE_FILE_MODE) & ~umask) & 0o777;
299
- }
300
- // The exact inverse of the CAFS layout `<store>/v<N>/files/<2 hex digits>/<digest>[-exec]`.
301
- // This importer also materializes local-directory dependencies, whose files keep
302
- // the modes their project gives them, so only a real store entry is re-derived.
303
- function isCafsFile(src) {
304
- const nameSep = src.lastIndexOf(path.sep);
305
- if (nameSep < 0)
306
- return false;
307
- const shardSep = src.lastIndexOf(path.sep, nameSep - 1);
308
- if (shardSep < 0 || nameSep - shardSep - 1 !== 2 || !isLowerHex(src, shardSep + 1, nameSep))
309
- return false;
310
- const filesSep = src.lastIndexOf(path.sep, shardSep - 1);
311
- if (filesSep < 0 || shardSep - filesSep - 1 !== 'files'.length || !src.startsWith('files', filesSep + 1))
312
- return false;
313
- const digestEnd = src.endsWith(CAFS_EXEC_SUFFIX) ? src.length - CAFS_EXEC_SUFFIX.length : src.length;
314
- return digestEnd - nameSep - 1 >= CAFS_DIGEST_MIN_LENGTH && isLowerHex(src, nameSep + 1, digestEnd);
315
- }
316
- function isLowerHex(value, from, to) {
317
- for (let index = from; index < to; index++) {
318
- const code = value.charCodeAt(index);
319
- if (!((code >= 0x30 && code <= 0x39) || (code >= 0x61 && code <= 0x66)))
320
- return false;
321
- }
322
- return true;
323
- }
324
- // A store file carries the umask that was active when it was written, so
325
- // importing has to apply the current one as though the entry were just
326
- // written (pnpm/pnpm#3807). POSIX mode bits do not apply on Windows.
327
- function storeEntryModeForSource(src) {
328
- if (process.platform === 'win32')
329
- return undefined;
330
- return isCafsFile(src) ? storeEntryMode(src.endsWith(CAFS_EXEC_SUFFIX)) : undefined;
331
- }
332
291
  function alignStoreFileMode(src, dest) {
333
292
  const mode = storeEntryModeForSource(src);
334
293
  if (mode === undefined)
@@ -339,14 +298,6 @@ function storeModeDiffers(existingPath) {
339
298
  const storeMode = storeEntryModeForSource(existingPath);
340
299
  return storeMode !== undefined && !isLinkableStoreMode(fs.statSync(existingPath).mode, storeMode);
341
300
  }
342
- // Group- and other-write may be missing, and group-write may be extra: a
343
- // store write takes those bits from its shard directory, not from the umask,
344
- // and the shared inode already grants them through the store. Every other bit
345
- // must match, so a store populated under a wider umask is still not linked.
346
- function isLinkableStoreMode(mode, storeMode) {
347
- const groupOtherWrite = 0o022;
348
- return ((mode ^ storeMode) & 0o777 & ~groupOtherWrite) === 0 && (mode & ~storeMode & 0o002) === 0;
349
- }
350
301
  function linkOrCopy(existingPath, newPath) {
351
302
  // A hardlink would pin the store-population mode onto the import.
352
303
  if (storeModeDiffers(existingPath)) {
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Move `stage` onto `newDir`, replacing an existing entry at `newDir` by
3
+ * moving it aside first instead of deleting it in place.
4
+ *
5
+ * `rename-overwrite` only moves an occupied target aside on `ENOTEMPTY`,
6
+ * `EEXIST`, and `ENOTDIR`; on Windows `fs.renameSync` onto an existing
7
+ * directory throws `EPERM` or `EACCES`, which `rename-overwrite` answers by
8
+ * deleting the target in place and retrying in a 60-second loop.
9
+ */
10
+ export declare function overwriteDirSync(stage: string, newDir: string): void;
@@ -0,0 +1,125 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { isError } from '@pnpm/error';
4
+ import { rimrafSync } from '@zkochan/rimraf';
5
+ import { fastPathTemp as pathTemp } from 'path-temp';
6
+ /**
7
+ * Move `stage` onto `newDir`, replacing an existing entry at `newDir` by
8
+ * moving it aside first instead of deleting it in place.
9
+ *
10
+ * `rename-overwrite` only moves an occupied target aside on `ENOTEMPTY`,
11
+ * `EEXIST`, and `ENOTDIR`; on Windows `fs.renameSync` onto an existing
12
+ * directory throws `EPERM` or `EACCES`, which `rename-overwrite` answers by
13
+ * deleting the target in place and retrying in a 60-second loop.
14
+ */
15
+ export function overwriteDirSync(stage, newDir) {
16
+ try {
17
+ fs.renameSync(stage, newDir);
18
+ }
19
+ catch (err) {
20
+ if (!isError(err) || !('code' in err))
21
+ throw err;
22
+ switch (err.code) {
23
+ case 'ENOENT':
24
+ fs.statSync(stage);
25
+ fs.mkdirSync(path.dirname(newDir), { recursive: true });
26
+ overwriteDirSync(stage, newDir);
27
+ return;
28
+ case 'EXDEV':
29
+ copyStageAcrossDevicesSync(stage, newDir);
30
+ return;
31
+ case 'ENOTEMPTY':
32
+ case 'EEXIST':
33
+ case 'ENOTDIR':
34
+ case 'EPERM':
35
+ case 'EACCES':
36
+ case 'EBUSY':
37
+ swapDirSync(stage, newDir);
38
+ return;
39
+ default:
40
+ throw err;
41
+ }
42
+ }
43
+ }
44
+ function copyStageAcrossDevicesSync(stage, newDir) {
45
+ fs.mkdirSync(path.dirname(newDir), { recursive: true });
46
+ const localStage = `${pathTemp(newDir)}_stage_${swapCounter++}`;
47
+ try {
48
+ fs.cpSync(stage, localStage, { recursive: true, verbatimSymlinks: true });
49
+ swapDirSync(localStage, newDir);
50
+ }
51
+ finally {
52
+ removeQuietly(localStage);
53
+ }
54
+ removeQuietly(stage);
55
+ }
56
+ function swapDirSync(stage, newDir) {
57
+ const aside = moveExistingTargetAside(newDir);
58
+ try {
59
+ fs.renameSync(stage, newDir);
60
+ }
61
+ catch (err) {
62
+ if (aside != null) {
63
+ restoreOrRemoveAside(aside, newDir);
64
+ }
65
+ throw err;
66
+ }
67
+ if (aside != null) {
68
+ removeQuietly(aside);
69
+ }
70
+ }
71
+ let swapCounter = 0;
72
+ function moveExistingTargetAside(newDir) {
73
+ const aside = `${pathTemp(newDir)}_swap_${swapCounter++}`;
74
+ try {
75
+ fs.renameSync(newDir, aside);
76
+ return aside;
77
+ }
78
+ catch (err) {
79
+ return backupAndRemoveTargetAfterFailedDisplace(newDir, aside, err);
80
+ }
81
+ }
82
+ function backupAndRemoveTargetAfterFailedDisplace(newDir, aside, err) {
83
+ if (!isError(err) || !('code' in err))
84
+ throw err;
85
+ switch (err.code) {
86
+ case 'ENOENT':
87
+ return undefined;
88
+ // Windows refuses to rename a directory a process runs from; copy a
89
+ // backup to `aside` before removing `newDir` in place so a failed
90
+ // stage rename can still restore the previous package.
91
+ case 'EPERM':
92
+ case 'EACCES':
93
+ case 'EBUSY':
94
+ try {
95
+ fs.cpSync(newDir, aside, { recursive: true, verbatimSymlinks: true });
96
+ rimrafSync(newDir);
97
+ return aside;
98
+ }
99
+ catch (fallbackErr) {
100
+ removeQuietly(aside);
101
+ throw fallbackErr;
102
+ }
103
+ default:
104
+ throw err;
105
+ }
106
+ }
107
+ function restoreOrRemoveAside(aside, newDir) {
108
+ try {
109
+ if (!fs.existsSync(newDir)) {
110
+ fs.renameSync(aside, newDir);
111
+ return;
112
+ }
113
+ }
114
+ catch { }
115
+ if (fs.existsSync(newDir)) {
116
+ removeQuietly(aside);
117
+ }
118
+ }
119
+ function removeQuietly(target) {
120
+ try {
121
+ rimrafSync(target);
122
+ }
123
+ catch { }
124
+ }
125
+ //# sourceMappingURL=overwriteDir.js.map
@@ -4,5 +4,11 @@ export declare function replaceFileIfDifferent(importFile: ImportFile, src: stri
4
4
  export declare function replaceSymlinkIfDifferent(target: string, dest: string): void;
5
5
  export declare function allFilesMatch(dir: string, filenames: Map<string, string>): boolean;
6
6
  export declare function allSymlinksMatch(dir: string, symlinks: Map<string, string> | undefined): boolean;
7
+ export declare function allImportedEntriesMatchExactly({ dir, filenames, symlinks, keepModulesDir, }: {
8
+ dir: string;
9
+ filenames: Map<string, string>;
10
+ symlinks: Map<string, string> | undefined;
11
+ keepModulesDir: boolean;
12
+ }): boolean;
7
13
  export declare function symlinkMatches(dest: string, target: string): boolean;
8
14
  export declare function mismatchReason(target: string, src: string): string | undefined;
@@ -4,6 +4,7 @@ import gfs, { renameFileWithRetry } from '@pnpm/fs.graceful-fs';
4
4
  import { globalInfo } from '@pnpm/logger';
5
5
  import { fastPathTemp as pathTemp } from 'path-temp';
6
6
  import { clearDirBlockingFile, clearDirentBlockingDir, makeFileMapDirs } from './dirUtils.js';
7
+ import { fileModeForImport, isLinkableStoreMode, storeEntryModeForSource } from './fileMode.js';
7
8
  import { filesHaveEqualContents } from './filesHaveEqualContents.js';
8
9
  import { filesHaveSameIdentity } from './hardlinkedDirMatches.js';
9
10
  export function repairIndexedDir({ importer, newDir, filenames, opts }) {
@@ -95,6 +96,45 @@ export function allSymlinksMatch(dir, symlinks) {
95
96
  }
96
97
  return true;
97
98
  }
99
+ export function allImportedEntriesMatchExactly({ dir, filenames, symlinks, keepModulesDir, }) {
100
+ if (!allFilesMatch(dir, filenames) || !allSymlinksMatch(dir, symlinks))
101
+ return false;
102
+ const expectedEntries = collectExpectedEntries(filenames, symlinks, keepModulesDir);
103
+ const actualEntries = new Set();
104
+ try {
105
+ collectDirectoryEntries({ root: dir, directory: dir, entries: actualEntries, preserveNodeModules: keepModulesDir });
106
+ }
107
+ catch {
108
+ return false;
109
+ }
110
+ return actualEntries.size === expectedEntries.size && [...expectedEntries].every(entry => actualEntries.has(entry));
111
+ }
112
+ function collectExpectedEntries(filenames, symlinks, preserveNodeModules) {
113
+ const entries = new Set();
114
+ for (const entry of [...filenames.keys(), ...(symlinks?.keys() ?? [])]) {
115
+ if (preserveNodeModules && (entry === 'node_modules' || entry.startsWith('node_modules/')))
116
+ continue;
117
+ entries.add(entry);
118
+ for (let parent = path.posix.dirname(entry); parent !== '.' && parent !== '/'; parent = path.posix.dirname(parent)) {
119
+ entries.add(parent);
120
+ }
121
+ }
122
+ return entries;
123
+ }
124
+ function collectDirectoryEntries({ root, directory, entries, preserveNodeModules, }) {
125
+ for (const dirent of fs.readdirSync(directory, { withFileTypes: true })) {
126
+ const entryPath = path.join(directory, dirent.name);
127
+ if (dirent.isDirectory()) {
128
+ if (preserveNodeModules && directory === root && dirent.name === 'node_modules')
129
+ continue;
130
+ entries.add(path.relative(root, entryPath).split(path.sep).join('/'));
131
+ collectDirectoryEntries({ root, directory: entryPath, entries, preserveNodeModules });
132
+ }
133
+ else {
134
+ entries.add(path.relative(root, entryPath).split(path.sep).join('/'));
135
+ }
136
+ }
137
+ }
98
138
  export function symlinkMatches(dest, target) {
99
139
  try {
100
140
  return fs.readlinkSync(dest) === target;
@@ -116,7 +156,14 @@ export function mismatchReason(target, src) {
116
156
  if (!targetStat.isFile())
117
157
  return 'is not a regular file';
118
158
  const srcStat = gfs.statSync(src, { bigint: true });
119
- if (filesHaveSameIdentity(targetStat, srcStat))
159
+ const storeMode = storeEntryModeForSource(src);
160
+ const expectedMode = fileModeForImport(srcStat.mode, storeMode);
161
+ const sameIdentity = filesHaveSameIdentity(targetStat, srcStat);
162
+ if (expectedMode !== undefined &&
163
+ (targetStat.mode & 511n) !== BigInt(expectedMode) &&
164
+ (!sameIdentity || storeMode === undefined || !isLinkableStoreMode(Number(targetStat.mode), expectedMode)))
165
+ return 'has a different mode';
166
+ if (sameIdentity)
120
167
  return undefined;
121
168
  if (targetStat.size !== srcStat.size)
122
169
  return 'has a different size';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/fs.indexed-pkg-importer",
3
- "version": "1100.0.34",
3
+ "version": "1100.0.35",
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.2",
40
+ "@pnpm/core-loggers": "1101.0.3",
41
41
  "@pnpm/error": "1100.2.2",
42
42
  "@pnpm/fs.graceful-fs": "1100.2.5",
43
- "@pnpm/store.controller-types": "1101.3.3",
43
+ "@pnpm/store.controller-types": "1101.3.4",
44
44
  "@pnpm/text.sanitize": "1100.0.1",
45
45
  "@reflink/reflink": "0.1.19",
46
46
  "@zkochan/rimraf": "^4.0.0",
@@ -56,9 +56,9 @@
56
56
  },
57
57
  "devDependencies": {
58
58
  "@jest/globals": "30.4.1",
59
- "@pnpm/fs.indexed-pkg-importer": "1100.0.34",
59
+ "@pnpm/fs.indexed-pkg-importer": "1100.0.35",
60
60
  "@pnpm/logger": "1100.0.0",
61
- "@pnpm/prepare": "1100.0.37",
61
+ "@pnpm/prepare": "1100.0.38",
62
62
  "@types/fs-extra": "^11.0.4"
63
63
  },
64
64
  "engines": {