@pnpm/fs.indexed-pkg-importer 1100.0.28 → 1100.0.30

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,31 @@
1
1
  # @pnpm/fs.indexed-pkg-importer
2
2
 
3
+ ## 1100.0.30
4
+
5
+ ### Patch Changes
6
+
7
+ - `pnpm deploy` now respects `--package-import-method` passed on the command line and reports the package import method correctly [pnpm/pnpm#7593](https://github.com/pnpm/pnpm/issues/7593).
8
+
9
+ - `pnpm install` in WSL now waits out Windows file locks on a Windows drive such as `/mnt/c`, as it already does on Windows. Before, an antivirus or indexer scan holding a file open could fail the install with `EACCES` [pnpm/pnpm#6155](https://github.com/pnpm/pnpm/issues/6155).
10
+
11
+ - `pnpm pack`, `pnpm deploy`, and installs of local directory dependencies now keep symlinks that point to files or directories included in the package. `pnpm pack` leaves out symlinks that point outside the package [#8208](https://github.com/pnpm/pnpm/issues/8208).
12
+
13
+ - Updated dependencies:
14
+ - @pnpm/core-loggers@1101.0.1
15
+ - @pnpm/fs.graceful-fs@1100.2.3
16
+ - @pnpm/store.controller-types@1101.3.1
17
+
18
+ ## 1100.0.29
19
+
20
+ ### Patch Changes
21
+
22
+ - Installs in different projects that share a global virtual store no longer fail on Windows with `Access is denied` while repairing the same slot [#15114](https://github.com/pnpm/pnpm/issues/15114).
23
+
24
+ - Updated dependencies:
25
+ - @pnpm/core-loggers@1101.0.0
26
+ - @pnpm/fs.graceful-fs@1100.2.2
27
+ - @pnpm/store.controller-types@1101.3.0
28
+
3
29
  ## 1100.0.28
4
30
 
5
31
  ### Patch Changes
@@ -1,7 +1,7 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import util from 'node:util';
4
- import gfs, { renameFileWithRetry } from '@pnpm/fs.graceful-fs';
4
+ import gfs, { lstatWithRetry, renameFileWithRetry, unlinkWithRetry, withFileLockRetry } from '@pnpm/fs.graceful-fs';
5
5
  import { globalInfo, globalWarn, logger } from '@pnpm/logger';
6
6
  import { rimrafSync } from '@zkochan/rimraf';
7
7
  import fsx from 'fs-extra';
@@ -34,7 +34,7 @@ export function importIndexedDir(importer, newDir, filenames, opts) {
34
34
  // handling (EEXIST dedup, ENOENT sanitized-filename retry, etc.) and
35
35
  // atomically swaps in a complete directory.
36
36
  // keepModulesDir needs the staging path to preserve the existing node_modules.
37
- if (!opts.keepModulesDir && tryExclusiveImport(importer, newDir, filenames)) {
37
+ if (!opts.keepModulesDir && tryExclusiveImport(importer, newDir, filenames, symlinkDirs(opts, filenames, { writtenDir: newDir, finalDir: newDir }))) {
38
38
  return;
39
39
  }
40
40
  // Staging path: create in temp dir, then atomically rename.
@@ -43,7 +43,7 @@ export function importIndexedDir(importer, newDir, filenames, opts) {
43
43
  const stage = pathTemp(newDir);
44
44
  try {
45
45
  makeEmptyDirSync(stage, { recursive: true });
46
- tryImportIndexedDir({ importFile: importer.importFile, importFileAtomic: importer.importFile }, stage, filenames);
46
+ tryImportIndexedDir({ importFile: importer.importFile, importFileAtomic: importer.importFile }, stage, filenames, symlinkDirs(opts, filenames, { writtenDir: stage, finalDir: newDir }));
47
47
  if (opts.keepModulesDir) {
48
48
  // Keeping node_modules is needed only when the hoisted node linker is used.
49
49
  moveOrMergeModulesDirs(path.join(newDir, 'node_modules'), path.join(stage, 'node_modules'));
@@ -59,7 +59,11 @@ export function importIndexedDir(importer, newDir, filenames, opts) {
59
59
  throw err;
60
60
  }
61
61
  try {
62
- renameOverwriteSync(stage, newDir);
62
+ // rename-overwrite retries Windows lock errors (EPERM, EBUSY) but not the
63
+ // EACCES that the same locks produce on a Windows drive mounted into WSL.
64
+ withFileLockRetry(() => {
65
+ renameOverwriteSync(stage, newDir);
66
+ });
63
67
  }
64
68
  catch (renameErr) {
65
69
  try {
@@ -165,12 +169,32 @@ function replaceFileIfDifferent(importFile, src, dest) {
165
169
  throw err;
166
170
  }
167
171
  }
172
+ // Whether a package directory can go at `dir` now: nothing is there, or
173
+ // what is there is already a directory.
174
+ //
175
+ // A failed removal is not a failed clearing when this holds. The installers
176
+ // healing a slot together race over these paths, and one that finishes the
177
+ // same work first leaves exactly what the removal was for, whether it merely
178
+ // unlinked the blocker or replaced it outright. The inspection retries for
179
+ // the same reason the others here do.
180
+ function dirFitsAt(dir) {
181
+ try {
182
+ return lstatWithRetry(dir).isDirectory();
183
+ }
184
+ catch (err) {
185
+ return util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT';
186
+ }
187
+ }
168
188
  // A rename cannot put a file where a directory is (EISDIR), so one standing in
169
189
  // the way has to go first. Only a damaged tree has one.
190
+ //
191
+ // The inspection retries, because installs in different projects heal one
192
+ // shared slot at the same time and Windows reports a dirent one of them has
193
+ // just unlinked as inaccessible until the unlink lands.
170
194
  function clearDirBlockingFile(dest) {
171
195
  let stats;
172
196
  try {
173
- stats = fs.lstatSync(dest);
197
+ stats = lstatWithRetry(dest);
174
198
  }
175
199
  catch (err) {
176
200
  if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
@@ -191,7 +215,7 @@ function clearDirentBlockingDir(newDir, relativeDir) {
191
215
  dir = path.join(dir, segment);
192
216
  let stats;
193
217
  try {
194
- stats = fs.lstatSync(dir);
218
+ stats = lstatWithRetry(dir);
195
219
  }
196
220
  catch (err) {
197
221
  if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
@@ -200,7 +224,13 @@ function clearDirentBlockingDir(newDir, relativeDir) {
200
224
  }
201
225
  if (stats.isDirectory())
202
226
  continue;
203
- fs.unlinkSync(dir);
227
+ try {
228
+ unlinkWithRetry(dir);
229
+ }
230
+ catch (err) {
231
+ if (!dirFitsAt(dir))
232
+ throw err;
233
+ }
204
234
  return;
205
235
  }
206
236
  }
@@ -245,7 +275,7 @@ function retryWithFixedFileMap(err, dirImport) {
245
275
  // otherwise leave a partial package behind: if the surviving files include the
246
276
  // package.json completion marker, every later install treats the broken
247
277
  // directory as complete and never repairs it.
248
- function tryExclusiveImport(importer, newDir, filenames) {
278
+ function tryExclusiveImport(importer, newDir, filenames, links) {
249
279
  fs.mkdirSync(path.dirname(newDir), { recursive: true });
250
280
  try {
251
281
  fs.mkdirSync(newDir);
@@ -261,7 +291,7 @@ function tryExclusiveImport(importer, newDir, filenames) {
261
291
  // back to staging, so the next attempt — including this process's own method
262
292
  // fallbacks (clone → hardlink → copy) — can fast-path again.
263
293
  try {
264
- tryImportIndexedDir(importer, newDir, filenames);
294
+ tryImportIndexedDir(importer, newDir, filenames, links);
265
295
  return true;
266
296
  }
267
297
  catch {
@@ -368,7 +398,7 @@ function sanitizeFilenames(filenames) {
368
398
  }
369
399
  return { sanitizedFilenames, invalidFilenames };
370
400
  }
371
- function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames) {
401
+ function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames, links) {
372
402
  makeFileMapDirs(newDir, filenames);
373
403
  // Write package.json last so it acts as a completion marker.
374
404
  // pkgExistsAtTargetDir() checks for package.json to decide if a package
@@ -380,11 +410,89 @@ function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames
380
410
  packageJsonSrc = src;
381
411
  continue;
382
412
  }
383
- importFile(src, path.join(newDir, f));
413
+ importEntry(importFile, src, path.join(newDir, f), links);
384
414
  }
385
415
  if (packageJsonSrc !== undefined) {
386
- importFileAtomic(packageJsonSrc, path.join(newDir, 'package.json'));
416
+ importEntry(importFileAtomic, packageJsonSrc, path.join(newDir, 'package.json'), links);
417
+ }
418
+ }
419
+ // Only a local directory has symlinks to preserve. The store holds regular
420
+ // files, so other imports skip the lstat per file.
421
+ function symlinkDirs(opts, filenames, dirs) {
422
+ if (opts.resolvedFrom !== 'local-dir')
423
+ return undefined;
424
+ const imported = new Set(['']);
425
+ for (const f of filenames.keys()) {
426
+ for (let entry = f; entry !== '' && !imported.has(entry); entry = path.posix.dirname(entry).replace(/^\.$/, '')) {
427
+ imported.add(entry);
428
+ }
429
+ }
430
+ return { ...dirs, imported };
431
+ }
432
+ function importEntry(importFile, src, dest, links) {
433
+ if (links != null && fs.lstatSync(src).isSymbolicLink() && copyInternalSymlink(src, dest, links))
434
+ return;
435
+ importFile(src, dest);
436
+ }
437
+ // Recreate the symlink at src as dest when its target stays inside the
438
+ // package, and report whether the entry is handled. A link that points outside
439
+ // the package, or at a file the import leaves out, is imported as the file it
440
+ // points to. A link to a directory the import leaves out is left out too.
441
+ function copyInternalSymlink(src, dest, links) {
442
+ let target = fs.readlinkSync(src);
443
+ if (path.isAbsolute(target)) {
444
+ // The link text can reach the package through another path than src,
445
+ // such as a symlinked workspace, so compare real paths.
446
+ target = path.relative(realpathOrSelf(path.dirname(src)), realpathOrSelf(target));
447
+ }
448
+ const resolved = path.resolve(path.dirname(dest), target);
449
+ if (escapesDir(links.writtenDir, resolved))
450
+ return false;
451
+ if (!links.imported.has(path.relative(links.writtenDir, resolved).split(path.sep).join('/'))) {
452
+ return isDirectory(src);
453
+ }
454
+ if (process.platform !== 'win32') {
455
+ fs.symlinkSync(target, dest);
456
+ return true;
457
+ }
458
+ const isDir = isDirectory(src);
459
+ try {
460
+ fs.symlinkSync(target, dest, isDir ? 'dir' : 'file');
461
+ }
462
+ catch (err) {
463
+ // Creating a symlink on Windows needs a privilege that a junction does
464
+ // not, and a file has no junction to fall back to.
465
+ if (!util.types.isNativeError(err) || !('code' in err) || err.code !== 'EPERM')
466
+ throw err;
467
+ if (!isDir)
468
+ return false;
469
+ fs.symlinkSync(path.join(links.finalDir, path.relative(links.writtenDir, resolved)), dest, 'junction');
470
+ }
471
+ return true;
472
+ }
473
+ function realpathOrSelf(file) {
474
+ try {
475
+ return fs.realpathSync(file);
476
+ }
477
+ catch (err) {
478
+ if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
479
+ return file;
480
+ throw err;
481
+ }
482
+ }
483
+ function isDirectory(file) {
484
+ try {
485
+ return fs.statSync(file).isDirectory();
387
486
  }
487
+ catch (err) {
488
+ if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
489
+ return false;
490
+ throw err;
491
+ }
492
+ }
493
+ function escapesDir(rootDir, targetPath) {
494
+ const rel = path.relative(rootDir, targetPath);
495
+ return rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel);
388
496
  }
389
497
  // Sorting shortest-first means the recursive mkdir for a deeper directory
390
498
  // always finds its ancestor already on disk.
package/lib/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import type { FilesMap, ImportIndexedPackage, ImportOptions } from '@pnpm/store.controller-types';
2
2
  export { type FilesMap, type ImportIndexedPackage, type ImportOptions };
3
3
  export type PackageImportMethod = 'auto' | 'hardlink' | 'copy' | 'clone' | 'clone-or-copy';
4
- export declare function createIndexedPkgImporter(packageImportMethod?: PackageImportMethod): ImportIndexedPackage;
4
+ export interface CreateIndexedPkgImporterOptions {
5
+ disableLogging?: boolean;
6
+ }
7
+ export declare function createIndexedPkgImporter(packageImportMethod?: PackageImportMethod, opts?: CreateIndexedPkgImporterOptions): ImportIndexedPackage;
5
8
  export declare function copyPkg(to: string, opts: ImportOptions): 'copy' | undefined;
package/lib/index.js CHANGED
@@ -10,11 +10,12 @@ import { renameOverwriteSync } from 'rename-overwrite';
10
10
  import { importIndexedDir } from './importIndexedDir.js';
11
11
  import { isNativeBinary, removeQuarantine } from './removeQuarantine.js';
12
12
  export {};
13
- export function createIndexedPkgImporter(packageImportMethod) {
14
- const importPackage = createImportPackage(packageImportMethod);
13
+ export function createIndexedPkgImporter(packageImportMethod, opts) {
14
+ const importPackage = createImportPackage(packageImportMethod, opts);
15
15
  return importPackage;
16
16
  }
17
- function createImportPackage(packageImportMethod) {
17
+ function createImportPackage(packageImportMethod, opts) {
18
+ const disableLogging = opts?.disableLogging;
18
19
  // this works in the following way:
19
20
  // - hardlink: hardlink the packages, no fallback
20
21
  // - clone: clone the packages, no fallback
@@ -22,24 +23,27 @@ function createImportPackage(packageImportMethod) {
22
23
  // - copy: copy the packages, do not try to link them first
23
24
  switch (packageImportMethod ?? 'auto') {
24
25
  case 'clone':
25
- packageImportMethodLogger.debug({ method: 'clone' });
26
+ if (!disableLogging)
27
+ packageImportMethodLogger.debug({ method: 'clone' });
26
28
  return createClonePkg();
27
29
  case 'hardlink':
28
- packageImportMethodLogger.debug({ method: 'hardlink' });
30
+ if (!disableLogging)
31
+ packageImportMethodLogger.debug({ method: 'hardlink' });
29
32
  return hardlinkPkg.bind(null, linkOrCopy);
30
33
  case 'auto': {
31
- return createAutoImporter();
34
+ return createAutoImporter(opts);
32
35
  }
33
36
  case 'clone-or-copy':
34
- return createCloneOrCopyImporter();
37
+ return createCloneOrCopyImporter(opts);
35
38
  case 'copy':
36
- packageImportMethodLogger.debug({ method: 'copy' });
39
+ if (!disableLogging)
40
+ packageImportMethodLogger.debug({ method: 'copy' });
37
41
  return copyPkg;
38
42
  default:
39
43
  throw new Error(`Unknown package import method ${packageImportMethod}`);
40
44
  }
41
45
  }
42
- function createAutoImporter() {
46
+ function createAutoImporter(createOpts) {
43
47
  let auto = initialAuto;
44
48
  return (to, opts) => auto(to, opts);
45
49
  function initialAuto(to, opts) {
@@ -56,7 +60,8 @@ function createAutoImporter() {
56
60
  // during heavy parallel I/O) for all subsequent packages.
57
61
  if (!tryClonePkg(to, opts))
58
62
  return undefined;
59
- packageImportMethodLogger.debug({ method: 'clone' });
63
+ if (!createOpts?.disableLogging)
64
+ packageImportMethodLogger.debug({ method: 'clone' });
60
65
  auto = createClonePkg();
61
66
  return 'clone';
62
67
  }
@@ -67,7 +72,8 @@ function createAutoImporter() {
67
72
  try {
68
73
  if (!hardlinkPkg(fs.linkSync, to, opts))
69
74
  return undefined;
70
- packageImportMethodLogger.debug({ method: 'hardlink' });
75
+ if (!createOpts?.disableLogging)
76
+ packageImportMethodLogger.debug({ method: 'hardlink' });
71
77
  auto = hardlinkPkg.bind(null, linkOrCopy);
72
78
  return 'hardlink';
73
79
  }
@@ -76,32 +82,36 @@ function createAutoImporter() {
76
82
  if (err.message.startsWith('EXDEV: cross-device link not permitted')) {
77
83
  globalWarn(err.message);
78
84
  globalInfo('Falling back to copying packages from store');
79
- packageImportMethodLogger.debug({ method: 'copy' });
85
+ if (!createOpts?.disableLogging)
86
+ packageImportMethodLogger.debug({ method: 'copy' });
80
87
  auto = copyPkg;
81
88
  return auto(to, opts);
82
89
  }
83
90
  // We still choose hard linking that will fall back to copying in edge cases.
84
- packageImportMethodLogger.debug({ method: 'hardlink' });
91
+ if (!createOpts?.disableLogging)
92
+ packageImportMethodLogger.debug({ method: 'hardlink' });
85
93
  auto = hardlinkPkg.bind(null, linkOrCopy);
86
94
  return auto(to, opts);
87
95
  }
88
96
  }
89
97
  }
90
- function createCloneOrCopyImporter() {
98
+ function createCloneOrCopyImporter(createOpts) {
91
99
  let auto = initialAuto;
92
100
  return (to, opts) => auto(to, opts);
93
101
  function initialAuto(to, opts) {
94
102
  try {
95
103
  if (!tryClonePkg(to, opts))
96
104
  return undefined;
97
- packageImportMethodLogger.debug({ method: 'clone' });
105
+ if (!createOpts?.disableLogging)
106
+ packageImportMethodLogger.debug({ method: 'clone' });
98
107
  auto = createClonePkg();
99
108
  return 'clone';
100
109
  }
101
110
  catch {
102
111
  // ignore
103
112
  }
104
- packageImportMethodLogger.debug({ method: 'copy' });
113
+ if (!createOpts?.disableLogging)
114
+ packageImportMethodLogger.debug({ method: 'copy' });
105
115
  auto = copyPkg;
106
116
  return auto(to, opts);
107
117
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/fs.indexed-pkg-importer",
3
- "version": "1100.0.28",
3
+ "version": "1100.0.30",
4
4
  "description": "Replicates indexed directories using hard links, copies, or cloning",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -37,14 +37,14 @@
37
37
  "test": "test"
38
38
  },
39
39
  "dependencies": {
40
- "@pnpm/core-loggers": "1100.3.4",
41
- "@pnpm/fs.graceful-fs": "1100.2.1",
42
- "@pnpm/store.controller-types": "1101.2.0",
40
+ "@pnpm/core-loggers": "1101.0.1",
41
+ "@pnpm/fs.graceful-fs": "1100.2.3",
42
+ "@pnpm/store.controller-types": "1101.3.1",
43
43
  "@reflink/reflink": "0.1.19",
44
44
  "@zkochan/rimraf": "^4.0.0",
45
- "fs-extra": "^11.4.0",
45
+ "fs-extra": "^11.4.1",
46
46
  "make-empty-dir": "^4.0.0",
47
- "p-limit": "^7.3.2",
47
+ "p-limit": "^7.3.3",
48
48
  "path-temp": "^3.0.0",
49
49
  "rename-overwrite": "^7.0.1",
50
50
  "sanitize-filename": "^1.6.4"
@@ -54,9 +54,9 @@
54
54
  },
55
55
  "devDependencies": {
56
56
  "@jest/globals": "30.4.1",
57
- "@pnpm/fs.indexed-pkg-importer": "1100.0.28",
57
+ "@pnpm/fs.indexed-pkg-importer": "1100.0.30",
58
58
  "@pnpm/logger": "1100.0.0",
59
- "@pnpm/prepare": "1100.0.30",
59
+ "@pnpm/prepare": "1100.0.32",
60
60
  "@types/fs-extra": "^11.0.4"
61
61
  },
62
62
  "engines": {