@pnpm/fs.indexed-pkg-importer 1100.0.10 → 1100.0.12

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.
@@ -14,30 +14,31 @@ export function importIndexedDir(importer, newDir, filenames, opts) {
14
14
  // Fast path: import directly without staging. Callers already verified
15
15
  // the target package is missing (pkgExistsAtTargetDir / pkgLinkedToStore),
16
16
  // so we can write straight into newDir and skip the temp dir + rename.
17
- // On any error, clean up and fall through to the staging path which has
18
- // full error handling (EEXIST dedup, ENOENT sanitized-filename retry, etc.).
17
+ // On failure we fall through to the staging path, which has full error
18
+ // handling (EEXIST dedup, ENOENT sanitized-filename retry, etc.) and
19
+ // atomically swaps in a complete directory.
19
20
  // keepModulesDir needs the staging path to preserve the existing node_modules.
20
- if (!opts.keepModulesDir)
21
- try {
22
- // For safeToSkip (content-addressed GVS), use non-destructive mkdirSync
23
- // so concurrent importers don't wipe each other's files.
24
- if (opts.safeToSkip) {
21
+ if (!opts.keepModulesDir) {
22
+ if (opts.safeToSkip) {
23
+ // Content-addressed target (e.g. global virtual store): the path is
24
+ // shared across projects, so concurrent importers are expected. Use a
25
+ // non-destructive mkdir and let importFile dedup via EEXIST. If another
26
+ // importer already completed the directory, there's nothing to do.
27
+ try {
25
28
  fs.mkdirSync(newDir, { recursive: true });
29
+ tryImportIndexedDir(importer, newDir, filenames);
30
+ return;
26
31
  }
27
- else {
28
- makeEmptyDirSync(newDir, { recursive: true });
29
- }
30
- tryImportIndexedDir(importer, newDir, filenames);
31
- return;
32
- }
33
- catch (err) {
34
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST') {
35
- // A concurrent importer may have completed the directory.
36
- // If all files match, there's nothing left to do.
37
- if (allFilesMatch(newDir, filenames))
32
+ catch (err) {
33
+ if (util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST' && allFilesMatch(newDir, filenames)) {
38
34
  return;
35
+ }
39
36
  }
40
37
  }
38
+ else if (tryExclusiveImport(importer, newDir, filenames)) {
39
+ return;
40
+ }
41
+ }
41
42
  // Staging path: create in temp dir, then atomically rename.
42
43
  // The dir rename is itself atomic, so individual file atomicity is not
43
44
  // needed here — use importFile for everything.
@@ -110,6 +111,45 @@ export function importIndexedDir(importer, newDir, filenames, opts) {
110
111
  throw renameErr;
111
112
  }
112
113
  }
114
+ // Fast path for the regular virtual store: write directly into newDir, but
115
+ // only when we can create it exclusively. A successful exclusive mkdir proves
116
+ // no other process is importing the same package concurrently, so the direct
117
+ // (non-atomic) write is safe. If newDir already exists — a concurrent importer
118
+ // or a stale partial directory from an interrupted import — this returns false
119
+ // so the caller falls back to the staging path, which builds a complete
120
+ // directory in a private temp dir and atomically renames it into place.
121
+ //
122
+ // Destructively emptying a directory another process may be populating could
123
+ // otherwise leave a partial package behind: if the surviving files include the
124
+ // package.json completion marker, every later install treats the broken
125
+ // directory as complete and never repairs it.
126
+ function tryExclusiveImport(importer, newDir, filenames) {
127
+ fs.mkdirSync(path.dirname(newDir), { recursive: true });
128
+ try {
129
+ fs.mkdirSync(newDir);
130
+ }
131
+ catch (err) {
132
+ if (util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST')
133
+ return false;
134
+ throw err;
135
+ }
136
+ // We exclusively created newDir, so no other process writes into it directly
137
+ // (concurrent importers see EEXIST above and take the staging path). The
138
+ // directory is ours: on failure we remove our partial result before falling
139
+ // back to staging, so the next attempt — including this process's own method
140
+ // fallbacks (clone → hardlink → copy) — can fast-path again.
141
+ try {
142
+ tryImportIndexedDir(importer, newDir, filenames);
143
+ return true;
144
+ }
145
+ catch {
146
+ try {
147
+ rimrafSync(newDir);
148
+ }
149
+ catch { } // eslint-disable-line:no-empty
150
+ return false;
151
+ }
152
+ }
113
153
  function allFilesMatch(dir, filenames) {
114
154
  for (const [f, src] of filenames) {
115
155
  const target = path.join(dir, f);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/fs.indexed-pkg-importer",
3
- "version": "1100.0.10",
3
+ "version": "1100.0.12",
4
4
  "description": "Replicates indexed directories using hard links, copies, or cloning",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -15,7 +15,10 @@
15
15
  ],
16
16
  "license": "MIT",
17
17
  "funding": "https://opencollective.com/pnpm",
18
- "repository": "https://github.com/pnpm/pnpm/tree/main/fs/indexed-pkg-importer",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/pnpm/pnpm/tree/main/fs/indexed-pkg-importer"
21
+ },
19
22
  "homepage": "https://github.com/pnpm/pnpm/tree/main/fs/indexed-pkg-importer#readme",
20
23
  "bugs": {
21
24
  "url": "https://github.com/pnpm/pnpm/issues"
@@ -42,8 +45,8 @@
42
45
  "path-temp": "^3.0.0",
43
46
  "rename-overwrite": "^7.0.1",
44
47
  "sanitize-filename": "^1.6.4",
45
- "@pnpm/store.controller-types": "1100.1.2",
46
- "@pnpm/core-loggers": "1100.1.2",
48
+ "@pnpm/store.controller-types": "1100.1.4",
49
+ "@pnpm/core-loggers": "1100.1.4",
47
50
  "@pnpm/fs.graceful-fs": "1100.1.0"
48
51
  },
49
52
  "peerDependencies": {
@@ -52,9 +55,9 @@
52
55
  "devDependencies": {
53
56
  "@jest/globals": "30.3.0",
54
57
  "@types/fs-extra": "^11.0.4",
55
- "@pnpm/logger": "1100.0.0",
56
- "@pnpm/prepare": "1100.0.11",
57
- "@pnpm/fs.indexed-pkg-importer": "1100.0.10"
58
+ "@pnpm/fs.indexed-pkg-importer": "1100.0.12",
59
+ "@pnpm/prepare": "1100.0.14",
60
+ "@pnpm/logger": "1100.0.0"
58
61
  },
59
62
  "engines": {
60
63
  "node": ">=22.13"