@pnpm/fs.indexed-pkg-importer 1100.0.29 → 1100.0.31

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,41 @@
1
1
  # @pnpm/fs.indexed-pkg-importer
2
2
 
3
+ ## 1100.0.31
4
+
5
+ ### Patch Changes
6
+
7
+ - `pnpm install` no longer fails for an injected workspace dependency whose package publishes from a `publishConfig.directory` that its own `prepare` script builds. The injected copy now picks up that directory once `prepare` finishes building it. `pnpm install --frozen-lockfile` no longer reports the dependency as outdated while the directory has not been built yet. [pnpm/pnpm#7811](https://github.com/pnpm/pnpm/issues/7811)
8
+
9
+ - Files imported from the store now follow the umask of the install that writes them. Installing with a umask of `077` no longer leaves imported files readable by the group and others [#3807](https://github.com/pnpm/pnpm/issues/3807).
10
+
11
+ - The side-effects cache now restores the symlinks that a build script creates inside a package. A warm install used to replace each of them with a copy of its target [#12859](https://github.com/pnpm/pnpm/issues/12859).
12
+
13
+ After upgrading, every package with a build script is built once more.
14
+
15
+ - A fresh install reusing a warm global virtual store skips reimporting packages whose target directory is already complete [#11112](https://github.com/pnpm/pnpm/issues/11112).
16
+
17
+ - `pnpm install` now works in StackBlitz WebContainers on projects without a lockfile. It used to fail there with `ENOENT ... pnpm-lock.yaml`, because pnpm did not recognize the errors that WebContainers return from asynchronous file system calls [#15649](https://github.com/pnpm/pnpm/issues/15649).
18
+
19
+ - Updated dependencies:
20
+ - @pnpm/error@1100.2.1
21
+ - @pnpm/fs.graceful-fs@1100.2.4
22
+ - @pnpm/store.controller-types@1101.3.2
23
+
24
+ ## 1100.0.30
25
+
26
+ ### Patch Changes
27
+
28
+ - `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).
29
+
30
+ - `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).
31
+
32
+ - `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).
33
+
34
+ - Updated dependencies:
35
+ - @pnpm/core-loggers@1101.0.1
36
+ - @pnpm/fs.graceful-fs@1100.2.3
37
+ - @pnpm/store.controller-types@1101.3.1
38
+
3
39
  ## 1100.0.29
4
40
 
5
41
  ### Patch Changes
@@ -12,5 +12,7 @@ export interface ImportIndexedDirOptions {
12
12
  */
13
13
  safeToSkip?: boolean;
14
14
  resolvedFrom?: ResolvedFrom;
15
+ /** Symlinks to create in the package, keyed by their path relative to it, with their targets. */
16
+ symlinks?: Map<string, string>;
15
17
  }
16
18
  export declare function importIndexedDir(importer: Importer, newDir: string, filenames: Map<string, string>, opts: ImportIndexedDirOptions): void;
@@ -1,7 +1,7 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import util from 'node:util';
4
- import gfs, { lstatWithRetry, renameFileWithRetry, unlinkWithRetry } from '@pnpm/fs.graceful-fs';
3
+ import { isError } from '@pnpm/error';
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,10 @@ 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, {
38
+ links: symlinkDirs(opts, filenames, { writtenDir: newDir, finalDir: newDir }),
39
+ symlinks: opts.symlinks,
40
+ })) {
38
41
  return;
39
42
  }
40
43
  // Staging path: create in temp dir, then atomically rename.
@@ -43,7 +46,10 @@ export function importIndexedDir(importer, newDir, filenames, opts) {
43
46
  const stage = pathTemp(newDir);
44
47
  try {
45
48
  makeEmptyDirSync(stage, { recursive: true });
46
- tryImportIndexedDir({ importFile: importer.importFile, importFileAtomic: importer.importFile }, stage, filenames);
49
+ tryImportIndexedDir({ importFile: importer.importFile, importFileAtomic: importer.importFile }, stage, filenames, {
50
+ links: symlinkDirs(opts, filenames, { writtenDir: stage, finalDir: newDir }),
51
+ symlinks: opts.symlinks,
52
+ });
47
53
  if (opts.keepModulesDir) {
48
54
  // Keeping node_modules is needed only when the hoisted node linker is used.
49
55
  moveOrMergeModulesDirs(path.join(newDir, 'node_modules'), path.join(stage, 'node_modules'));
@@ -59,7 +65,11 @@ export function importIndexedDir(importer, newDir, filenames, opts) {
59
65
  throw err;
60
66
  }
61
67
  try {
62
- renameOverwriteSync(stage, newDir);
68
+ // rename-overwrite retries Windows lock errors (EPERM, EBUSY) but not the
69
+ // EACCES that the same locks produce on a Windows drive mounted into WSL.
70
+ withFileLockRetry(() => {
71
+ renameOverwriteSync(stage, newDir);
72
+ });
63
73
  }
64
74
  catch (renameErr) {
65
75
  try {
@@ -81,7 +91,7 @@ export function importIndexedDir(importer, newDir, filenames, opts) {
81
91
  // then puts the completion marker on top of it — after which the directory
82
92
  // looks finished to every later install and is never repaired.
83
93
  function importIntoSharedDir(dirImport) {
84
- const { importer, newDir, filenames } = dirImport;
94
+ const { importer, newDir, filenames, opts } = dirImport;
85
95
  fs.mkdirSync(path.dirname(newDir), { recursive: true });
86
96
  let created = false;
87
97
  try {
@@ -89,12 +99,12 @@ function importIntoSharedDir(dirImport) {
89
99
  created = true;
90
100
  }
91
101
  catch (err) {
92
- if (!util.types.isNativeError(err) || !('code' in err) || err.code !== 'EEXIST')
102
+ if (!isError(err) || !('code' in err) || err.code !== 'EEXIST')
93
103
  throw err;
94
104
  }
95
105
  if (created) {
96
106
  try {
97
- tryImportIndexedDir(importer, newDir, filenames);
107
+ tryImportIndexedDir(importer, newDir, filenames, { symlinks: opts.symlinks });
98
108
  return;
99
109
  }
100
110
  catch (err) {
@@ -105,7 +115,7 @@ function importIntoSharedDir(dirImport) {
105
115
  // replacement for it.
106
116
  }
107
117
  }
108
- if (allFilesMatch(newDir, filenames))
118
+ if (allFilesMatch(newDir, filenames) && allSymlinksMatch(newDir, opts.symlinks))
109
119
  return;
110
120
  try {
111
121
  repairIndexedDir(dirImport);
@@ -120,7 +130,7 @@ function importIntoSharedDir(dirImport) {
120
130
  // entry. Files the package does not declare are left alone: a build output
121
131
  // belongs to whoever put it there, and a slot other installs are reading is
122
132
  // not somewhere to delete from speculatively.
123
- function repairIndexedDir({ importer, newDir, filenames }) {
133
+ function repairIndexedDir({ importer, newDir, filenames, opts }) {
124
134
  makeFileMapDirs(newDir, filenames, { clearBlockers: true });
125
135
  let packageJsonSrc;
126
136
  for (const [f, src] of filenames) {
@@ -130,6 +140,12 @@ function repairIndexedDir({ importer, newDir, filenames }) {
130
140
  }
131
141
  replaceFileIfDifferent(importer.importFile, src, path.join(newDir, f));
132
142
  }
143
+ for (const [f, target] of opts.symlinks ?? []) {
144
+ const dir = path.posix.dirname(f);
145
+ if (dir !== '.')
146
+ clearDirentBlockingDir(newDir, dir);
147
+ replaceSymlinkIfDifferent(target, path.join(newDir, f));
148
+ }
133
149
  if (packageJsonSrc !== undefined) {
134
150
  replaceFileIfDifferent(importer.importFile, packageJsonSrc, path.join(newDir, 'package.json'));
135
151
  }
@@ -165,6 +181,26 @@ function replaceFileIfDifferent(importFile, src, dest) {
165
181
  throw err;
166
182
  }
167
183
  }
184
+ function replaceSymlinkIfDifferent(target, dest) {
185
+ if (symlinkMatches(dest, target))
186
+ return;
187
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
188
+ const tmp = pathTemp(dest);
189
+ fs.symlinkSync(target, tmp);
190
+ try {
191
+ clearDirBlockingFile(dest);
192
+ renameFileWithRetry(tmp, dest);
193
+ }
194
+ catch (err) {
195
+ try {
196
+ fs.unlinkSync(tmp);
197
+ }
198
+ catch { } // eslint-disable-line:no-empty
199
+ if (symlinkMatches(dest, target))
200
+ return;
201
+ throw err;
202
+ }
203
+ }
168
204
  // Whether a package directory can go at `dir` now: nothing is there, or
169
205
  // what is there is already a directory.
170
206
  //
@@ -178,7 +214,7 @@ function dirFitsAt(dir) {
178
214
  return lstatWithRetry(dir).isDirectory();
179
215
  }
180
216
  catch (err) {
181
- return util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT';
217
+ return isError(err) && 'code' in err && err.code === 'ENOENT';
182
218
  }
183
219
  }
184
220
  // A rename cannot put a file where a directory is (EISDIR), so one standing in
@@ -193,7 +229,7 @@ function clearDirBlockingFile(dest) {
193
229
  stats = lstatWithRetry(dest);
194
230
  }
195
231
  catch (err) {
196
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
232
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
197
233
  return;
198
234
  throw err;
199
235
  }
@@ -214,7 +250,7 @@ function clearDirentBlockingDir(newDir, relativeDir) {
214
250
  stats = lstatWithRetry(dir);
215
251
  }
216
252
  catch (err) {
217
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
253
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
218
254
  return;
219
255
  throw err;
220
256
  }
@@ -237,7 +273,7 @@ function clearDirentBlockingDir(newDir, relativeDir) {
237
273
  // failure.
238
274
  function retryWithFixedFileMap(err, dirImport) {
239
275
  const { importer, newDir, filenames, opts } = dirImport;
240
- if (!util.types.isNativeError(err) || !('code' in err))
276
+ if (!isError(err) || !('code' in err))
241
277
  return false;
242
278
  if (err.code === 'EEXIST') {
243
279
  const { uniqueFileMap, conflictingFileNames } = getUniqueFileMap(filenames);
@@ -271,13 +307,13 @@ function retryWithFixedFileMap(err, dirImport) {
271
307
  // otherwise leave a partial package behind: if the surviving files include the
272
308
  // package.json completion marker, every later install treats the broken
273
309
  // directory as complete and never repairs it.
274
- function tryExclusiveImport(importer, newDir, filenames) {
310
+ function tryExclusiveImport(importer, newDir, filenames, entries) {
275
311
  fs.mkdirSync(path.dirname(newDir), { recursive: true });
276
312
  try {
277
313
  fs.mkdirSync(newDir);
278
314
  }
279
315
  catch (err) {
280
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST')
316
+ if (isError(err) && 'code' in err && err.code === 'EEXIST')
281
317
  return false;
282
318
  throw err;
283
319
  }
@@ -287,7 +323,7 @@ function tryExclusiveImport(importer, newDir, filenames) {
287
323
  // back to staging, so the next attempt — including this process's own method
288
324
  // fallbacks (clone → hardlink → copy) — can fast-path again.
289
325
  try {
290
- tryImportIndexedDir(importer, newDir, filenames);
326
+ tryImportIndexedDir(importer, newDir, filenames, entries);
291
327
  return true;
292
328
  }
293
329
  catch {
@@ -313,6 +349,23 @@ function allFilesMatch(dir, filenames) {
313
349
  }
314
350
  return true;
315
351
  }
352
+ function allSymlinksMatch(dir, symlinks) {
353
+ for (const [f, target] of symlinks ?? []) {
354
+ if (!symlinkMatches(path.join(dir, f), target)) {
355
+ globalInfo(`Re-importing "${dir}" because symlink "${f}" does not point to "${target}"`);
356
+ return false;
357
+ }
358
+ }
359
+ return true;
360
+ }
361
+ function symlinkMatches(dest, target) {
362
+ try {
363
+ return fs.readlinkSync(dest) === target;
364
+ }
365
+ catch {
366
+ return false;
367
+ }
368
+ }
316
369
  function fileMatches(dir, f, src) {
317
370
  const reason = mismatchReason(path.join(dir, f), src);
318
371
  if (reason === undefined)
@@ -394,7 +447,7 @@ function sanitizeFilenames(filenames) {
394
447
  }
395
448
  return { sanitizedFilenames, invalidFilenames };
396
449
  }
397
- function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames) {
450
+ function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames, { links, symlinks } = {}) {
398
451
  makeFileMapDirs(newDir, filenames);
399
452
  // Write package.json last so it acts as a completion marker.
400
453
  // pkgExistsAtTargetDir() checks for package.json to decide if a package
@@ -406,12 +459,95 @@ function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames
406
459
  packageJsonSrc = src;
407
460
  continue;
408
461
  }
409
- importFile(src, path.join(newDir, f));
462
+ importEntry(importFile, src, path.join(newDir, f), links);
463
+ }
464
+ for (const [f, target] of symlinks ?? []) {
465
+ const dest = path.join(newDir, f);
466
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
467
+ fs.symlinkSync(target, dest);
410
468
  }
411
469
  if (packageJsonSrc !== undefined) {
412
- importFileAtomic(packageJsonSrc, path.join(newDir, 'package.json'));
470
+ importEntry(importFileAtomic, packageJsonSrc, path.join(newDir, 'package.json'), links);
413
471
  }
414
472
  }
473
+ // Only a local directory has symlinks to preserve. The store holds regular
474
+ // files, so other imports skip the lstat per file.
475
+ function symlinkDirs(opts, filenames, dirs) {
476
+ if (opts.resolvedFrom !== 'local-dir')
477
+ return undefined;
478
+ const imported = new Set(['']);
479
+ for (const f of filenames.keys()) {
480
+ for (let entry = f; entry !== '' && !imported.has(entry); entry = path.posix.dirname(entry).replace(/^\.$/, '')) {
481
+ imported.add(entry);
482
+ }
483
+ }
484
+ return { ...dirs, imported };
485
+ }
486
+ function importEntry(importFile, src, dest, links) {
487
+ if (links != null && fs.lstatSync(src).isSymbolicLink() && copyInternalSymlink(src, dest, links))
488
+ return;
489
+ importFile(src, dest);
490
+ }
491
+ // Recreate the symlink at src as dest when its target stays inside the
492
+ // package, and report whether the entry is handled. A link that points outside
493
+ // the package, or at a file the import leaves out, is imported as the file it
494
+ // points to. A link to a directory the import leaves out is left out too.
495
+ function copyInternalSymlink(src, dest, links) {
496
+ let target = fs.readlinkSync(src);
497
+ if (path.isAbsolute(target)) {
498
+ // The link text can reach the package through another path than src,
499
+ // such as a symlinked workspace, so compare real paths.
500
+ target = path.relative(realpathOrSelf(path.dirname(src)), realpathOrSelf(target));
501
+ }
502
+ const resolved = path.resolve(path.dirname(dest), target);
503
+ if (escapesDir(links.writtenDir, resolved))
504
+ return false;
505
+ if (!links.imported.has(path.relative(links.writtenDir, resolved).split(path.sep).join('/'))) {
506
+ return isDirectory(src);
507
+ }
508
+ if (process.platform !== 'win32') {
509
+ fs.symlinkSync(target, dest);
510
+ return true;
511
+ }
512
+ const isDir = isDirectory(src);
513
+ try {
514
+ fs.symlinkSync(target, dest, isDir ? 'dir' : 'file');
515
+ }
516
+ catch (err) {
517
+ // Creating a symlink on Windows needs a privilege that a junction does
518
+ // not, and a file has no junction to fall back to.
519
+ if (!isError(err) || !('code' in err) || err.code !== 'EPERM')
520
+ throw err;
521
+ if (!isDir)
522
+ return false;
523
+ fs.symlinkSync(path.join(links.finalDir, path.relative(links.writtenDir, resolved)), dest, 'junction');
524
+ }
525
+ return true;
526
+ }
527
+ function realpathOrSelf(file) {
528
+ try {
529
+ return fs.realpathSync(file);
530
+ }
531
+ catch (err) {
532
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
533
+ return file;
534
+ throw err;
535
+ }
536
+ }
537
+ function isDirectory(file) {
538
+ try {
539
+ return fs.statSync(file).isDirectory();
540
+ }
541
+ catch (err) {
542
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
543
+ return false;
544
+ throw err;
545
+ }
546
+ }
547
+ function escapesDir(rootDir, targetPath) {
548
+ const rel = path.relative(rootDir, targetPath);
549
+ return rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel);
550
+ }
415
551
  // Sorting shortest-first means the recursive mkdir for a deeper directory
416
552
  // always finds its ancestor already on disk.
417
553
  function makeFileMapDirs(newDir, filenames, opts) {
@@ -452,7 +588,7 @@ function moveOrMergeModulesDirs(src, dest) {
452
588
  renameEvenAcrossDevices(src, dest);
453
589
  }
454
590
  catch (err) {
455
- switch (util.types.isNativeError(err) && 'code' in err && err.code) {
591
+ switch (isError(err) && 'code' in err && err.code) {
456
592
  case 'ENOENT':
457
593
  // If src directory doesn't exist, there is nothing to do
458
594
  return;
@@ -471,7 +607,7 @@ function renameEvenAcrossDevices(src, dest) {
471
607
  gfs.renameSync(src, dest);
472
608
  }
473
609
  catch (err) {
474
- if (!(util.types.isNativeError(err) && 'code' in err && err.code === 'EXDEV'))
610
+ if (!(isError(err) && 'code' in err && err.code === 'EXDEV'))
475
611
  throw err;
476
612
  fsx.copySync(src, dest);
477
613
  }
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
@@ -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,11 +10,16 @@ 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
+ const BASE_FILE_MODE = 0o666;
14
+ const EXEC_FILE_MODE = 0o755;
15
+ const CAFS_EXEC_SUFFIX = '-exec';
16
+ const CAFS_DIGEST_MIN_LENGTH = 40;
17
+ export function createIndexedPkgImporter(packageImportMethod, opts) {
18
+ const importPackage = createImportPackage(packageImportMethod, opts);
15
19
  return importPackage;
16
20
  }
17
- function createImportPackage(packageImportMethod) {
21
+ function createImportPackage(packageImportMethod, opts) {
22
+ const disableLogging = opts?.disableLogging;
18
23
  // this works in the following way:
19
24
  // - hardlink: hardlink the packages, no fallback
20
25
  // - clone: clone the packages, no fallback
@@ -22,24 +27,27 @@ function createImportPackage(packageImportMethod) {
22
27
  // - copy: copy the packages, do not try to link them first
23
28
  switch (packageImportMethod ?? 'auto') {
24
29
  case 'clone':
25
- packageImportMethodLogger.debug({ method: 'clone' });
30
+ if (!disableLogging)
31
+ packageImportMethodLogger.debug({ method: 'clone' });
26
32
  return createClonePkg();
27
33
  case 'hardlink':
28
- packageImportMethodLogger.debug({ method: 'hardlink' });
34
+ if (!disableLogging)
35
+ packageImportMethodLogger.debug({ method: 'hardlink' });
29
36
  return hardlinkPkg.bind(null, linkOrCopy);
30
37
  case 'auto': {
31
- return createAutoImporter();
38
+ return createAutoImporter(opts);
32
39
  }
33
40
  case 'clone-or-copy':
34
- return createCloneOrCopyImporter();
41
+ return createCloneOrCopyImporter(opts);
35
42
  case 'copy':
36
- packageImportMethodLogger.debug({ method: 'copy' });
43
+ if (!disableLogging)
44
+ packageImportMethodLogger.debug({ method: 'copy' });
37
45
  return copyPkg;
38
46
  default:
39
47
  throw new Error(`Unknown package import method ${packageImportMethod}`);
40
48
  }
41
49
  }
42
- function createAutoImporter() {
50
+ function createAutoImporter(createOpts) {
43
51
  let auto = initialAuto;
44
52
  return (to, opts) => auto(to, opts);
45
53
  function initialAuto(to, opts) {
@@ -56,7 +64,8 @@ function createAutoImporter() {
56
64
  // during heavy parallel I/O) for all subsequent packages.
57
65
  if (!tryClonePkg(to, opts))
58
66
  return undefined;
59
- packageImportMethodLogger.debug({ method: 'clone' });
67
+ if (!createOpts?.disableLogging)
68
+ packageImportMethodLogger.debug({ method: 'clone' });
60
69
  auto = createClonePkg();
61
70
  return 'clone';
62
71
  }
@@ -65,47 +74,58 @@ function createAutoImporter() {
65
74
  }
66
75
  }
67
76
  try {
68
- if (!hardlinkPkg(fs.linkSync, to, opts))
77
+ if (!hardlinkPkg(linkOrThrowOnLinkFailure, to, opts))
69
78
  return undefined;
70
- packageImportMethodLogger.debug({ method: 'hardlink' });
79
+ if (!createOpts?.disableLogging)
80
+ packageImportMethodLogger.debug({ method: 'hardlink' });
71
81
  auto = hardlinkPkg.bind(null, linkOrCopy);
72
82
  return 'hardlink';
73
83
  }
74
84
  catch (err) {
75
- assert(util.types.isNativeError(err));
85
+ assert(isError(err));
76
86
  if (err.message.startsWith('EXDEV: cross-device link not permitted')) {
77
87
  globalWarn(err.message);
78
88
  globalInfo('Falling back to copying packages from store');
79
- packageImportMethodLogger.debug({ method: 'copy' });
89
+ if (!createOpts?.disableLogging)
90
+ packageImportMethodLogger.debug({ method: 'copy' });
80
91
  auto = copyPkg;
81
92
  return auto(to, opts);
82
93
  }
83
94
  // We still choose hard linking that will fall back to copying in edge cases.
84
- packageImportMethodLogger.debug({ method: 'hardlink' });
95
+ if (!createOpts?.disableLogging)
96
+ packageImportMethodLogger.debug({ method: 'hardlink' });
85
97
  auto = hardlinkPkg.bind(null, linkOrCopy);
86
98
  return auto(to, opts);
87
99
  }
88
100
  }
89
101
  }
90
- function createCloneOrCopyImporter() {
102
+ function createCloneOrCopyImporter(createOpts) {
91
103
  let auto = initialAuto;
92
104
  return (to, opts) => auto(to, opts);
93
105
  function initialAuto(to, opts) {
94
106
  try {
95
107
  if (!tryClonePkg(to, opts))
96
108
  return undefined;
97
- packageImportMethodLogger.debug({ method: 'clone' });
109
+ if (!createOpts?.disableLogging)
110
+ packageImportMethodLogger.debug({ method: 'clone' });
98
111
  auto = createClonePkg();
99
112
  return 'clone';
100
113
  }
101
114
  catch {
102
115
  // ignore
103
116
  }
104
- packageImportMethodLogger.debug({ method: 'copy' });
117
+ if (!createOpts?.disableLogging)
118
+ packageImportMethodLogger.debug({ method: 'copy' });
105
119
  auto = copyPkg;
106
120
  return auto(to, opts);
107
121
  }
108
122
  }
123
+ function alignedClone(clone) {
124
+ return (src, dest) => {
125
+ clone(src, dest);
126
+ alignStoreFileMode(src, dest);
127
+ };
128
+ }
109
129
  /**
110
130
  * Import a single package using a raw clone function (no ENOTSUP fallback).
111
131
  * Used by auto-mode to probe whether the filesystem supports cloning.
@@ -113,8 +133,8 @@ function createCloneOrCopyImporter() {
113
133
  * through to a faster method (e.g. hardlinks).
114
134
  */
115
135
  function tryClonePkg(to, opts) {
116
- if (opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap)) {
117
- const clone = createCloneFunction();
136
+ if (shouldImportPkg(to, opts)) {
137
+ const clone = alignedClone(createCloneFunction());
118
138
  importIndexedDir({ importFile: clone, importFileAtomic: clone }, to, opts.filesMap, opts);
119
139
  removeQuarantineFromNativeBinaries(to, opts);
120
140
  return 'clone';
@@ -130,13 +150,13 @@ function tryClonePkg(to, opts) {
130
150
  * atomic.
131
151
  */
132
152
  function createClonePkg() {
133
- const clone = createCloneFunction();
153
+ const clone = alignedClone(createCloneFunction());
134
154
  const withFallback = (fallback) => (src, dest) => {
135
155
  try {
136
156
  clone(src, dest);
137
157
  }
138
158
  catch (err) {
139
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOTSUP') {
159
+ if (isError(err) && 'code' in err && err.code === 'ENOTSUP') {
140
160
  fallback(src, dest);
141
161
  return;
142
162
  }
@@ -148,7 +168,7 @@ function createClonePkg() {
148
168
  importFileAtomic: withFallback(atomicCopyFileSync),
149
169
  };
150
170
  return (to, opts) => {
151
- if (opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap)) {
171
+ if (shouldImportPkg(to, opts)) {
152
172
  importIndexedDir(importer, to, opts.filesMap, opts);
153
173
  removeQuarantineFromNativeBinaries(to, opts);
154
174
  return 'clone';
@@ -156,6 +176,18 @@ function createClonePkg() {
156
176
  return undefined;
157
177
  };
158
178
  }
179
+ // Whether to (re)import via clone or copy. A directory dependency's source
180
+ // can change without the lockfile changing, so these methods import it
181
+ // unconditionally by default, the same way `shouldRelinkPkg` does for the
182
+ // hardlink path below, including the same guard against wiping an
183
+ // already-materialized target when a `publishConfig.directory` source has
184
+ // not been built yet.
185
+ function shouldImportPkg(to, opts) {
186
+ if (opts.resolvedFrom === 'local-dir' && opts.sourceExists === false) {
187
+ return targetHasNothingToLose(to);
188
+ }
189
+ return opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap);
190
+ }
159
191
  function pkgExistsAtTargetDir(targetDir, filesMap) {
160
192
  return existsSync(path.join(targetDir, pickFileFromFilesMap(filesMap)));
161
193
  }
@@ -188,7 +220,7 @@ function createCloneFunction() {
188
220
  // If the file already exists, then we just proceed.
189
221
  // This will probably only happen if the package's index file contains the same file twice.
190
222
  // For instance: { "index.js": "hash", "./index.js": "hash" }
191
- if (!util.types.isNativeError(err) || !('code' in err) || err.code !== 'EEXIST')
223
+ if (!isError(err) || !('code' in err) || err.code !== 'EEXIST')
192
224
  throw err;
193
225
  }
194
226
  };
@@ -199,7 +231,7 @@ function createCloneFunction() {
199
231
  fs.copyFileSync(src, dest, constants.COPYFILE_FICLONE_FORCE);
200
232
  }
201
233
  catch (err) {
202
- if (!(util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST'))
234
+ if (!(isError(err) && 'code' in err && err.code === 'EEXIST'))
203
235
  throw err;
204
236
  }
205
237
  };
@@ -207,6 +239,10 @@ function createCloneFunction() {
207
239
  return _cloneFunction;
208
240
  }
209
241
  function hardlinkPkg(importFile, to, opts) {
242
+ // Checked ahead of `opts.force`: a caller-requested force must not be able
243
+ // to bypass the missing-source preservation below.
244
+ if (missingSourceHasSomethingToPreserve(to, opts))
245
+ return undefined;
210
246
  if (opts.force || shouldRelinkPkg(to, opts)) {
211
247
  importIndexedDir({ importFile, importFileAtomic: importFile }, to, opts.filesMap, opts);
212
248
  removeQuarantineFromNativeBinaries(to, opts);
@@ -216,24 +252,100 @@ function hardlinkPkg(importFile, to, opts) {
216
252
  }
217
253
  function shouldRelinkPkg(to, opts) {
218
254
  if (opts.disableRelinkLocalDirDeps && opts.resolvedFrom === 'local-dir') {
219
- try {
220
- const files = fs.readdirSync(to);
221
- return files.length === 0 || files.length === 1 && files[0] === 'node_modules';
222
- }
223
- catch {
224
- return true;
225
- }
255
+ return targetHasNothingToLose(to);
256
+ }
257
+ // A directory dependency relinks on every install by default, since its
258
+ // source can change without the lockfile changing. But when the source is
259
+ // a `publishConfig.directory` its own `prepare` script has not (re)built
260
+ // yet, the fetcher tolerates the missing directory and comes back with an
261
+ // empty `filesMap`; relinking from that would wipe an already-materialized
262
+ // target. Only skip the relink when there is something in the target
263
+ // worth preserving — an empty or missing target has nothing to lose, and
264
+ // still needs the relink to create it so the post-build resync has
265
+ // somewhere to write into. `hardlinkPkg` checks this same condition ahead
266
+ // of its own `opts.force`, so this only runs once that has already passed.
267
+ if (opts.resolvedFrom === 'local-dir' && opts.sourceExists === false) {
268
+ return targetHasNothingToLose(to);
226
269
  }
227
270
  return opts.resolvedFrom !== 'store' || !pkgLinkedToStore(opts.filesMap, to);
228
271
  }
272
+ // See `shouldRelinkPkg`'s comment on the missing-source case. Pulled out so
273
+ // `hardlinkPkg` can apply the same guard ahead of its `opts.force` check,
274
+ // which would otherwise bypass it.
275
+ function missingSourceHasSomethingToPreserve(to, opts) {
276
+ return opts.resolvedFrom === 'local-dir' && opts.sourceExists === false && !targetHasNothingToLose(to);
277
+ }
278
+ function targetHasNothingToLose(to) {
279
+ try {
280
+ const files = fs.readdirSync(to);
281
+ return files.length === 0 || files.length === 1 && files[0] === 'node_modules';
282
+ }
283
+ catch {
284
+ return true;
285
+ }
286
+ }
287
+ // The CLI never changes its own umask and the import path asks for it once
288
+ // per file, so read it once.
289
+ let umask;
290
+ function storeEntryMode(executable) {
291
+ umask ??= process.umask();
292
+ return ((executable ? EXEC_FILE_MODE : BASE_FILE_MODE) & ~umask) & 0o777;
293
+ }
294
+ // The exact inverse of the CAFS layout `<store>/v<N>/files/<2 hex digits>/<digest>[-exec]`.
295
+ // This importer also materializes local-directory dependencies, whose files keep
296
+ // the modes their project gives them, so only a real store entry is re-derived.
297
+ function isCafsFile(src) {
298
+ const nameSep = src.lastIndexOf(path.sep);
299
+ if (nameSep < 0)
300
+ return false;
301
+ const shardSep = src.lastIndexOf(path.sep, nameSep - 1);
302
+ if (shardSep < 0 || nameSep - shardSep - 1 !== 2 || !isLowerHex(src, shardSep + 1, nameSep))
303
+ return false;
304
+ const filesSep = src.lastIndexOf(path.sep, shardSep - 1);
305
+ if (filesSep < 0 || shardSep - filesSep - 1 !== 'files'.length || !src.startsWith('files', filesSep + 1))
306
+ return false;
307
+ const digestEnd = src.endsWith(CAFS_EXEC_SUFFIX) ? src.length - CAFS_EXEC_SUFFIX.length : src.length;
308
+ return digestEnd - nameSep - 1 >= CAFS_DIGEST_MIN_LENGTH && isLowerHex(src, nameSep + 1, digestEnd);
309
+ }
310
+ function isLowerHex(value, from, to) {
311
+ for (let i = from; i < to; i++) {
312
+ const code = value.charCodeAt(i);
313
+ if (!((code >= 0x30 && code <= 0x39) || (code >= 0x61 && code <= 0x66)))
314
+ return false;
315
+ }
316
+ return true;
317
+ }
318
+ // A store file carries the umask that was active when it was written, so
319
+ // importing has to apply the current one as though the entry were just
320
+ // written (pnpm/pnpm#3807). POSIX mode bits do not apply on Windows.
321
+ function storeEntryModeForSource(src) {
322
+ if (process.platform === 'win32')
323
+ return undefined;
324
+ return isCafsFile(src) ? storeEntryMode(src.endsWith(CAFS_EXEC_SUFFIX)) : undefined;
325
+ }
326
+ function alignStoreFileMode(src, dest) {
327
+ const mode = storeEntryModeForSource(src);
328
+ if (mode === undefined)
329
+ return;
330
+ chmodSync(dest, mode);
331
+ }
332
+ function storeModeDiffers(existingPath) {
333
+ const storeMode = storeEntryModeForSource(existingPath);
334
+ return storeMode !== undefined && (fs.statSync(existingPath).mode & 0o777) !== storeMode;
335
+ }
229
336
  function linkOrCopy(existingPath, newPath) {
337
+ // A hardlink would pin the store-population mode onto the import.
338
+ if (storeModeDiffers(existingPath)) {
339
+ resilientCopyFileSync(existingPath, newPath);
340
+ return;
341
+ }
230
342
  try {
231
343
  fs.linkSync(existingPath, newPath);
232
344
  }
233
345
  catch (err) {
234
346
  // If a hard link to the same file already exists
235
347
  // then trying to copy it will make an empty file from it.
236
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST')
348
+ if (isError(err) && 'code' in err && err.code === 'EEXIST')
237
349
  return;
238
350
  // In some VERY rare cases (1 in a thousand), hard-link creation fails on Windows.
239
351
  // In that case, we just fall back to copying.
@@ -241,16 +353,27 @@ function linkOrCopy(existingPath, newPath) {
241
353
  resilientCopyFileSync(existingPath, newPath);
242
354
  }
243
355
  }
356
+ // The auto-mode hardlink probe: a filesystem that cannot hardlink must keep
357
+ // failing here, so auto backs off to copy.
358
+ function linkOrThrowOnLinkFailure(existingPath, newPath) {
359
+ if (storeModeDiffers(existingPath)) {
360
+ resilientCopyFileSync(existingPath, newPath);
361
+ return;
362
+ }
363
+ fs.linkSync(existingPath, newPath);
364
+ }
244
365
  // On Linux CI, the kernel's copy_file_range/sendfile can transiently fail
245
366
  // with ENOTSUP under heavy parallel I/O on the same store files.
246
367
  // Fall back to manual read+write which uses plain read/write syscalls.
247
368
  function resilientCopyFileSync(src, dest) {
248
369
  try {
249
370
  fs.copyFileSync(src, dest);
371
+ alignStoreFileMode(src, dest);
250
372
  }
251
373
  catch (err) {
252
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOTSUP') {
253
- const srcMode = fs.statSync(src).mode;
374
+ if (isError(err) && 'code' in err && err.code === 'ENOTSUP') {
375
+ const storeMode = storeEntryModeForSource(src);
376
+ const srcMode = storeMode ?? fs.statSync(src).mode;
254
377
  fs.writeFileSync(dest, fs.readFileSync(src), { mode: srcMode });
255
378
  }
256
379
  else {
@@ -266,7 +389,7 @@ function pkgLinkedToStore(filesMap, linkedPkgDir) {
266
389
  stats0 = fs.statSync(linkedFile);
267
390
  }
268
391
  catch (err) {
269
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
392
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
270
393
  return false;
271
394
  }
272
395
  const stats1 = fs.statSync(filesMap.get(filename));
@@ -276,7 +399,7 @@ function pkgLinkedToStore(filesMap, linkedPkgDir) {
276
399
  return false;
277
400
  }
278
401
  export function copyPkg(to, opts) {
279
- if (opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap)) {
402
+ if (shouldImportPkg(to, opts)) {
280
403
  // copyFileSync is not atomic on non-COW filesystems: a crash mid-copy
281
404
  // can leave a partially-written file. package.json is the completion
282
405
  // marker, so it must be written atomically via temp file + rename.
@@ -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
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.31",
4
4
  "description": "Replicates indexed directories using hard links, copies, or cloning",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -37,12 +37,13 @@
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/error": "1100.2.1",
42
+ "@pnpm/fs.graceful-fs": "1100.2.4",
43
+ "@pnpm/store.controller-types": "1101.3.2",
43
44
  "@reflink/reflink": "0.1.19",
44
45
  "@zkochan/rimraf": "^4.0.0",
45
- "fs-extra": "^11.4.0",
46
+ "fs-extra": "^11.4.1",
46
47
  "make-empty-dir": "^4.0.0",
47
48
  "p-limit": "^7.3.3",
48
49
  "path-temp": "^3.0.0",
@@ -54,9 +55,9 @@
54
55
  },
55
56
  "devDependencies": {
56
57
  "@jest/globals": "30.4.1",
57
- "@pnpm/fs.indexed-pkg-importer": "1100.0.29",
58
+ "@pnpm/fs.indexed-pkg-importer": "1100.0.31",
58
59
  "@pnpm/logger": "1100.0.0",
59
- "@pnpm/prepare": "1100.0.31",
60
+ "@pnpm/prepare": "1100.0.33",
60
61
  "@types/fs-extra": "^11.0.4"
61
62
  },
62
63
  "engines": {