@pnpm/fs.graceful-fs 1100.2.2 → 1100.2.3

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,13 @@
1
1
  # @pnpm/fs.graceful-fs
2
2
 
3
+ ## 1100.2.3
4
+
5
+ ### Patch Changes
6
+
7
+ - `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).
8
+
9
+ - On Windows, pnpm now retries saving `pnpm-lock.yaml` for up to a minute while another process holds the file open. The save used to fail at once with `EPERM`, `EBUSY`, or "Access is denied" [#9461](https://github.com/pnpm/pnpm/issues/9461).
10
+
3
11
  ## 1100.2.2
4
12
 
5
13
  ### Patch Changes
package/lib/index.d.ts CHANGED
@@ -22,6 +22,7 @@ declare const _default: {
22
22
  export default _default;
23
23
  /**
24
24
  * Renames `src` over `dest`, retrying Windows EBUSY errors for up to a minute.
25
+ * Windows drives mounted into WSL get the same retries.
25
26
  * EPERM and EACCES have a one-second budget because they can also indicate
26
27
  * permanent permission or destination conflicts. Other errors are thrown
27
28
  * right away.
@@ -31,6 +32,11 @@ export default _default;
31
32
  * that was there or the whole file replacing it.
32
33
  */
33
34
  export declare function renameFileWithRetry(src: string, dest: string): void;
35
+ /**
36
+ * Asynchronous {@link renameFileWithRetry}, which waits between attempts
37
+ * without blocking the event loop.
38
+ */
39
+ export declare function renameFileWithRetryAsync(src: string, dest: string): Promise<void>;
34
40
  /**
35
41
  * Reads `target`'s stats without following it, with the retry policy of
36
42
  * {@link renameFileWithRetry}.
@@ -45,3 +51,8 @@ export declare function lstatWithRetry(target: string): fs.Stats;
45
51
  * Removes a file, with the retry policy of {@link renameFileWithRetry}.
46
52
  */
47
53
  export declare function unlinkWithRetry(target: string): void;
54
+ /**
55
+ * Runs a filesystem operation with the retry policy of
56
+ * {@link renameFileWithRetry}.
57
+ */
58
+ export declare function withFileLockRetry<T>(operation: () => T): T;
package/lib/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import fs from 'node:fs';
2
+ import os from 'node:os';
2
3
  import util, { promisify } from 'node:util';
3
4
  import gfs from 'graceful-fs';
4
5
  const FILE_LOCK_RETRY_BUDGET_MS = 60_000;
@@ -48,6 +49,7 @@ function withEagainRetry(fn, maxRetries = 15) {
48
49
  }
49
50
  /**
50
51
  * Renames `src` over `dest`, retrying Windows EBUSY errors for up to a minute.
52
+ * Windows drives mounted into WSL get the same retries.
51
53
  * EPERM and EACCES have a one-second budget because they can also indicate
52
54
  * permanent permission or destination conflicts. Other errors are thrown
53
55
  * right away.
@@ -61,6 +63,13 @@ export function renameFileWithRetry(src, dest) {
61
63
  fs.renameSync(src, dest);
62
64
  });
63
65
  }
66
+ /**
67
+ * Asynchronous {@link renameFileWithRetry}, which waits between attempts
68
+ * without blocking the event loop.
69
+ */
70
+ export async function renameFileWithRetryAsync(src, dest) {
71
+ await withFileLockRetryAsync(() => fs.promises.rename(src, dest));
72
+ }
64
73
  /**
65
74
  * Reads `target`'s stats without following it, with the retry policy of
66
75
  * {@link renameFileWithRetry}.
@@ -81,15 +90,47 @@ export function unlinkWithRetry(target) {
81
90
  fs.unlinkSync(target);
82
91
  });
83
92
  }
84
- function withFileLockRetry(operation) {
85
- const startedAt = Date.now();
86
- let backoffMs = 0;
87
- let budgetMs = FILE_LOCK_RETRY_BUDGET_MS;
93
+ /**
94
+ * Runs a filesystem operation with the retry policy of
95
+ * {@link renameFileWithRetry}.
96
+ */
97
+ export function withFileLockRetry(operation) {
98
+ const retry = createFileLockRetry();
88
99
  for (;;) {
89
100
  try {
90
101
  return operation();
91
102
  }
92
103
  catch (err) {
104
+ const delayMs = retry.delayBeforeNextAttempt(err);
105
+ if (delayMs > 0)
106
+ Atomics.wait(fileLockRetrySleepBuffer, 0, 0, delayMs);
107
+ retry.checkBudgetAfterDelay(err);
108
+ }
109
+ }
110
+ }
111
+ async function withFileLockRetryAsync(operation) {
112
+ const retry = createFileLockRetry();
113
+ for (;;) {
114
+ try {
115
+ // eslint-disable-next-line no-await-in-loop
116
+ return await operation();
117
+ }
118
+ catch (err) {
119
+ const delayMs = retry.delayBeforeNextAttempt(err);
120
+ if (delayMs > 0) {
121
+ // eslint-disable-next-line no-await-in-loop
122
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
123
+ }
124
+ retry.checkBudgetAfterDelay(err);
125
+ }
126
+ }
127
+ }
128
+ function createFileLockRetry() {
129
+ const startedAt = Date.now();
130
+ let backoffMs = 0;
131
+ let budgetMs = FILE_LOCK_RETRY_BUDGET_MS;
132
+ return {
133
+ delayBeforeNextAttempt(err) {
93
134
  if (!isTransientFileLockError(err))
94
135
  throw err;
95
136
  if (err.code === 'EPERM' || err.code === 'EACCES')
@@ -97,18 +138,25 @@ function withFileLockRetry(operation) {
97
138
  const remainingMs = budgetMs - (Date.now() - startedAt);
98
139
  if (remainingMs <= 0)
99
140
  throw err;
100
- if (backoffMs > 0)
101
- Atomics.wait(fileLockRetrySleepBuffer, 0, 0, Math.min(backoffMs, remainingMs));
141
+ return Math.min(backoffMs, remainingMs);
142
+ },
143
+ checkBudgetAfterDelay(err) {
102
144
  if (Date.now() - startedAt >= budgetMs)
103
145
  throw err;
104
146
  backoffMs = Math.min(backoffMs + 10, FILE_LOCK_RETRY_BACKOFF_CAP_MS);
105
- }
106
- }
147
+ },
148
+ };
107
149
  }
108
150
  function isTransientFileLockError(err) {
109
- return process.platform === 'win32' &&
151
+ return (process.platform === 'win32' || isWsl()) &&
110
152
  util.types.isNativeError(err) &&
111
153
  'code' in err &&
112
154
  (err.code === 'EPERM' || err.code === 'EACCES' || err.code === 'EBUSY');
113
155
  }
156
+ // A Windows drive mounted into WSL (/mnt/c) keeps Windows file locking, so an
157
+ // antivirus or indexer handle fails a rename there with EACCES. WSL kernels
158
+ // carry "microsoft" in their release, e.g. 5.15.167.4-microsoft-standard-WSL2.
159
+ function isWsl() {
160
+ return process.platform === 'linux' && os.release().toLowerCase().includes('microsoft');
161
+ }
114
162
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/fs.graceful-fs",
3
- "version": "1100.2.2",
3
+ "version": "1100.2.3",
4
4
  "description": "Promisified graceful-fs",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "devDependencies": {
33
33
  "@jest/globals": "30.4.1",
34
- "@pnpm/fs.graceful-fs": "1100.2.2",
34
+ "@pnpm/fs.graceful-fs": "1100.2.3",
35
35
  "@types/graceful-fs": "^4.1.9"
36
36
  },
37
37
  "engines": {