@pnpm/fs.indexed-pkg-importer 1100.0.33 → 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,27 @@
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
+
15
+ ## 1100.0.34
16
+
17
+ ### Patch Changes
18
+
19
+ - `pnpm install` hardlinks files from a group-writable or world-writable store again. Before this fix, pnpm copied every file from such a store into `node_modules` [#16677](https://github.com/pnpm/pnpm/issues/16677).
20
+
21
+ - Sped up frozen installs of unchanged injected workspace packages that use hardlinks.
22
+
23
+ - The warning about renamed package files with invalid names no longer prints control characters from those file names.
24
+
3
25
  ## 1100.0.33
4
26
 
5
27
  ### 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
@@ -0,0 +1,3 @@
1
+ import { type BigIntStats } from 'node:fs';
2
+ export declare function hardlinkedDirMatches(dir: string, files: Map<string, string>, keepModulesDir: boolean): boolean;
3
+ export declare function filesHaveSameIdentity(target: BigIntStats, source: BigIntStats): boolean;
@@ -0,0 +1,77 @@
1
+ import fs, {} from 'node:fs';
2
+ import path from 'node:path';
3
+ export function hardlinkedDirMatches(dir, files, keepModulesDir) {
4
+ if (files.size === 0)
5
+ return false;
6
+ const directories = expectedDirectories(files);
7
+ if (!directories)
8
+ return false;
9
+ try {
10
+ const rootStat = fs.lstatSync(dir);
11
+ if (!rootStat.isDirectory() || rootStat.isSymbolicLink())
12
+ return false;
13
+ return treeMatches(dir, { files, directories, keepModulesDir });
14
+ }
15
+ catch {
16
+ return false;
17
+ }
18
+ }
19
+ function expectedDirectories(files) {
20
+ const directories = new Set();
21
+ for (const relative of files.keys()) {
22
+ if (!isReusableFilePath(relative))
23
+ return undefined;
24
+ for (let parent = path.posix.dirname(relative); parent !== '.'; parent = path.posix.dirname(parent))
25
+ directories.add(parent);
26
+ }
27
+ return directories;
28
+ }
29
+ function isReusableFilePath(relative) {
30
+ if (relative === 'node_modules' || relative.startsWith('node_modules/'))
31
+ return false;
32
+ if (path.isAbsolute(relative) || relative.includes('\\'))
33
+ return false;
34
+ return relative.split('/').every(part => part !== '' && part !== '.' && part !== '..');
35
+ }
36
+ function treeMatches(root, expected) {
37
+ const pending = [''];
38
+ let matched = 0;
39
+ for (let relative = pending.pop(); relative !== undefined; relative = pending.pop()) {
40
+ const count = matchingFilesInDirectory(root, relative, { expected, pending });
41
+ if (count === undefined)
42
+ return false;
43
+ matched += count;
44
+ }
45
+ return matched === expected.files.size;
46
+ }
47
+ function matchingFilesInDirectory(root, relative, { expected, pending }) {
48
+ let matched = 0;
49
+ for (const entry of fs.readdirSync(path.join(root, relative), { withFileTypes: true })) {
50
+ const entryPath = relative ? `${relative}/${entry.name}` : entry.name;
51
+ const kind = matchingEntryKind(path.join(root, entryPath), entryPath, expected);
52
+ if (!kind)
53
+ return undefined;
54
+ if (kind === 'directory')
55
+ pending.push(entryPath);
56
+ if (kind === 'file')
57
+ matched++;
58
+ }
59
+ return matched;
60
+ }
61
+ function matchingEntryKind(targetPath, relative, expected) {
62
+ const target = fs.lstatSync(targetPath, { bigint: true });
63
+ if (target.isSymbolicLink())
64
+ return undefined;
65
+ if (target.isDirectory()) {
66
+ if (expected.keepModulesDir && relative === 'node_modules')
67
+ return 'preserved';
68
+ return expected.directories.has(relative) ? 'directory' : undefined;
69
+ }
70
+ const source = expected.files.get(relative);
71
+ return source && filesHaveSameIdentity(target, fs.lstatSync(source, { bigint: true })) ? 'file' : undefined;
72
+ }
73
+ export function filesHaveSameIdentity(target, source) {
74
+ return target.isFile() && source.isFile() && target.ino !== 0n && target.dev !== 0n &&
75
+ target.ino === source.ino && target.dev === source.dev;
76
+ }
77
+ //# sourceMappingURL=hardlinkedDirMatches.js.map
@@ -3,15 +3,19 @@ import path from 'node:path';
3
3
  import { isError } from '@pnpm/error';
4
4
  import { withFileLockRetry } from '@pnpm/fs.graceful-fs';
5
5
  import { globalWarn, logger } from '@pnpm/logger';
6
+ import { sanitizeInline } from '@pnpm/text.sanitize';
6
7
  import { rimrafSync } from '@zkochan/rimraf';
7
8
  import { makeEmptyDirSync } from 'make-empty-dir';
8
9
  import { fastPathTemp as pathTemp } from 'path-temp';
9
- import { renameOverwriteSync } from 'rename-overwrite';
10
10
  import { copyInternalSymlink } from './copyInternalSymlink.js';
11
11
  import { makeFileMapDirs } from './dirUtils.js';
12
12
  import { getUniqueFileMap, moveOrMergeModulesDirs, sanitizeFilenames, } from './fileMapUtils.js';
13
- import { allFilesMatch, allSymlinksMatch, repairIndexedDir, } from './repairIndexedDir.js';
13
+ import { overwriteDirSync } from './overwriteDir.js';
14
+ import { allFilesMatch, allImportedEntriesMatchExactly, allSymlinksMatch, repairIndexedDir, } from './repairIndexedDir.js';
14
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;
15
19
  export function importIndexedDir(importer, newDir, filenames, opts) {
16
20
  const dirImport = { importer, newDir, filenames, opts };
17
21
  if (opts.safeToSkip && opts.resolvedFrom !== 'local-dir') {
@@ -45,19 +49,51 @@ function importWithStaging(dirImport) {
45
49
  return;
46
50
  throw err;
47
51
  }
48
- commitStagedDir(stage, newDir);
52
+ commitStagedDir(stage, dirImport);
49
53
  }
50
- 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 };
51
60
  try {
61
+ let attempt = 0;
52
62
  withFileLockRetry(() => {
53
- renameOverwriteSync(stage, newDir);
63
+ if (attempt++ > 0 && allImportedEntriesMatchExactly(entries)) {
64
+ acceptExistingImport(stage, newDir, keepModulesDir);
65
+ return;
66
+ }
67
+ overwriteDirSync(stage, newDir);
54
68
  });
55
69
  }
56
70
  catch (renameErr) {
71
+ if (destinationHoldsThisImport(entries)) {
72
+ acceptExistingImport(stage, newDir, keepModulesDir);
73
+ return;
74
+ }
57
75
  cleanStageDir(stage);
58
76
  throw renameErr;
59
77
  }
60
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
+ }
61
97
  function cleanStageDir(stage) {
62
98
  try {
63
99
  rimrafSync(stage);
@@ -130,25 +166,47 @@ function retryWithFixedFileMap(err, dirImport) {
130
166
  function tryExclusiveImport(importer, newDir, filenames, entries) {
131
167
  if (!tryExclusiveMkdir(newDir))
132
168
  return false;
169
+ const createdStat = safeLstat(newDir);
133
170
  try {
134
171
  tryImportIndexedDir(importer, newDir, filenames, entries);
135
172
  return true;
136
173
  }
137
174
  catch {
138
- try {
139
- rimrafSync(newDir);
175
+ if (sameCreatedDirectory(newDir, createdStat)) {
176
+ try {
177
+ rimrafSync(newDir);
178
+ }
179
+ catch { }
140
180
  }
141
- catch { }
142
181
  return false;
143
182
  }
144
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
+ }
145
203
  function retryWithSanitizedFilenames({ importer, newDir, filenames, opts }) {
146
204
  const { sanitizedFilenames, invalidFilenames } = sanitizeFilenames(filenames);
147
205
  if (invalidFilenames.length === 0)
148
206
  return false;
149
207
  globalWarn(`\
150
- The package linked to "${path.relative(process.cwd(), newDir)}" had \
151
- files with invalid names: ${invalidFilenames.join(', ')}. \
208
+ The package linked to "${sanitizeInline(path.relative(process.cwd(), newDir))}" had \
209
+ files with invalid names: ${invalidFilenames.map((filename) => sanitizeInline(filename)).join(', ')}. \
152
210
  They were renamed.`);
153
211
  importIndexedDir(importer, newDir, sanitizedFilenames, opts);
154
212
  return true;
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,13 +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';
11
+ import { hardlinkedDirMatches } from './hardlinkedDirMatches.js';
10
12
  import { importIndexedDir } from './importIndexedDir.js';
11
13
  import { isNativeBinary, removeQuarantine } from './removeQuarantine.js';
12
14
  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;
15
+ export { copyInternalSymlink, internalSymlinkTarget } from './copyInternalSymlink.js';
17
16
  export function createIndexedPkgImporter(packageImportMethod, opts) {
18
17
  const importPackage = createImportPackage(packageImportMethod, opts);
19
18
  return importPackage;
@@ -248,7 +247,11 @@ function hardlinkPkg(importFile, to, opts) {
248
247
  if (missingSourceHasSomethingToPreserve(to, opts))
249
248
  return undefined;
250
249
  if (opts.force || shouldRelinkPkg(to, opts)) {
251
- importIndexedDir({ importFile, importFileAtomic: importFile }, to, opts.filesMap, opts);
250
+ const reuseHardlinks = !opts.force && opts.resolvedFrom === 'local-dir' && !opts.symlinks?.size &&
251
+ hardlinkedDirMatches(to, opts.filesMap, opts.keepModulesDir ?? false);
252
+ if (!reuseHardlinks) {
253
+ importIndexedDir({ importFile, importFileAtomic: importFile }, to, opts.filesMap, opts);
254
+ }
252
255
  removeQuarantineFromNativeBinaries(to, opts);
253
256
  return 'hardlink';
254
257
  }
@@ -285,45 +288,6 @@ function targetHasNothingToLose(to) {
285
288
  return true;
286
289
  }
287
290
  }
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
291
  function alignStoreFileMode(src, dest) {
328
292
  const mode = storeEntryModeForSource(src);
329
293
  if (mode === undefined)
@@ -332,7 +296,7 @@ function alignStoreFileMode(src, dest) {
332
296
  }
333
297
  function storeModeDiffers(existingPath) {
334
298
  const storeMode = storeEntryModeForSource(existingPath);
335
- return storeMode !== undefined && (fs.statSync(existingPath).mode & 0o777) !== storeMode;
299
+ return storeMode !== undefined && !isLinkableStoreMode(fs.statSync(existingPath).mode, storeMode);
336
300
  }
337
301
  function linkOrCopy(existingPath, newPath) {
338
302
  // A hardlink would pin the store-population mode onto the import.
@@ -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,7 +4,9 @@ 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';
9
+ import { filesHaveSameIdentity } from './hardlinkedDirMatches.js';
8
10
  export function repairIndexedDir({ importer, newDir, filenames, opts }) {
9
11
  makeFileMapDirs(newDir, filenames, { clearBlockers: true });
10
12
  let packageJsonSrc;
@@ -94,6 +96,45 @@ export function allSymlinksMatch(dir, symlinks) {
94
96
  }
95
97
  return true;
96
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
+ }
97
138
  export function symlinkMatches(dest, target) {
98
139
  try {
99
140
  return fs.readlinkSync(dest) === target;
@@ -115,11 +156,14 @@ export function mismatchReason(target, src) {
115
156
  if (!targetStat.isFile())
116
157
  return 'is not a regular file';
117
158
  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)
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)
123
167
  return undefined;
124
168
  if (targetStat.size !== srcStat.size)
125
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.33",
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,11 @@
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
+ "@pnpm/text.sanitize": "1100.0.1",
44
45
  "@reflink/reflink": "0.1.19",
45
46
  "@zkochan/rimraf": "^4.0.0",
46
47
  "fs-extra": "^11.4.1",
@@ -55,9 +56,9 @@
55
56
  },
56
57
  "devDependencies": {
57
58
  "@jest/globals": "30.4.1",
58
- "@pnpm/fs.indexed-pkg-importer": "1100.0.33",
59
+ "@pnpm/fs.indexed-pkg-importer": "1100.0.35",
59
60
  "@pnpm/logger": "1100.0.0",
60
- "@pnpm/prepare": "1100.0.36",
61
+ "@pnpm/prepare": "1100.0.38",
61
62
  "@types/fs-extra": "^11.0.4"
62
63
  },
63
64
  "engines": {