@pnpm/fs.indexed-pkg-importer 1100.0.29 → 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,20 @@
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
+
3
18
  ## 1100.0.29
4
19
 
5
20
  ### 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, { lstatWithRetry, renameFileWithRetry, unlinkWithRetry } 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 {
@@ -271,7 +275,7 @@ function retryWithFixedFileMap(err, dirImport) {
271
275
  // otherwise leave a partial package behind: if the surviving files include the
272
276
  // package.json completion marker, every later install treats the broken
273
277
  // directory as complete and never repairs it.
274
- function tryExclusiveImport(importer, newDir, filenames) {
278
+ function tryExclusiveImport(importer, newDir, filenames, links) {
275
279
  fs.mkdirSync(path.dirname(newDir), { recursive: true });
276
280
  try {
277
281
  fs.mkdirSync(newDir);
@@ -287,7 +291,7 @@ function tryExclusiveImport(importer, newDir, filenames) {
287
291
  // back to staging, so the next attempt — including this process's own method
288
292
  // fallbacks (clone → hardlink → copy) — can fast-path again.
289
293
  try {
290
- tryImportIndexedDir(importer, newDir, filenames);
294
+ tryImportIndexedDir(importer, newDir, filenames, links);
291
295
  return true;
292
296
  }
293
297
  catch {
@@ -394,7 +398,7 @@ function sanitizeFilenames(filenames) {
394
398
  }
395
399
  return { sanitizedFilenames, invalidFilenames };
396
400
  }
397
- function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames) {
401
+ function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames, links) {
398
402
  makeFileMapDirs(newDir, filenames);
399
403
  // Write package.json last so it acts as a completion marker.
400
404
  // pkgExistsAtTargetDir() checks for package.json to decide if a package
@@ -406,12 +410,90 @@ function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames
406
410
  packageJsonSrc = src;
407
411
  continue;
408
412
  }
409
- importFile(src, path.join(newDir, f));
413
+ importEntry(importFile, src, path.join(newDir, f), links);
410
414
  }
411
415
  if (packageJsonSrc !== undefined) {
412
- 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;
413
481
  }
414
482
  }
483
+ function isDirectory(file) {
484
+ try {
485
+ return fs.statSync(file).isDirectory();
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);
496
+ }
415
497
  // Sorting shortest-first means the recursive mkdir for a deeper directory
416
498
  // always finds its ancestor already on disk.
417
499
  function makeFileMapDirs(newDir, filenames, opts) {
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.29",
3
+ "version": "1100.0.30",
4
4
  "description": "Replicates indexed directories using hard links, copies, or cloning",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -37,12 +37,12 @@
37
37
  "test": "test"
38
38
  },
39
39
  "dependencies": {
40
- "@pnpm/core-loggers": "1101.0.0",
41
- "@pnpm/fs.graceful-fs": "1100.2.2",
42
- "@pnpm/store.controller-types": "1101.3.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
47
  "p-limit": "^7.3.3",
48
48
  "path-temp": "^3.0.0",
@@ -54,9 +54,9 @@
54
54
  },
55
55
  "devDependencies": {
56
56
  "@jest/globals": "30.4.1",
57
- "@pnpm/fs.indexed-pkg-importer": "1100.0.29",
57
+ "@pnpm/fs.indexed-pkg-importer": "1100.0.30",
58
58
  "@pnpm/logger": "1100.0.0",
59
- "@pnpm/prepare": "1100.0.31",
59
+ "@pnpm/prepare": "1100.0.32",
60
60
  "@types/fs-extra": "^11.0.4"
61
61
  },
62
62
  "engines": {