Skip to content

Commit 94f2bbe

Browse files
committed
fix(windows): add file flush delay to prevent ENOENT errors in tests
On Windows, file operations can be asynchronous at the OS level. After writeFile() completes, the file handle may not be fully released immediately, causing ENOENT errors when code tries to read the file right after writing. Enhanced retryWrite() with ENOENT retry support: 1. **Retry on ENOENT errors** (not just EPERM/EBUSY) - this is the key fix 2. Add 25ms initial delay after write on Windows 3. Verify file accessibility with up to 4 retries with increasing delays (15ms, 30ms, 45ms, 60ms) 4. Total potential wait time: 25ms + (15+30+45+60)ms = 175ms max Root cause: The previous code only retried EPERM/EBUSY errors. The write itself can fail with ENOENT on Windows if the filesystem is slow to make the file available. Adding ENOENT to the retry conditions is the critical fix. The delays are tuned for Windows CI runners which are slower than local development environments. This fixes intermittent test failures in Windows CI runners, including: - "should handle load -> update -> save workflow" - "should use default 2-space indent for new files" - "should preserve line endings" - "should support sort option" - "should use LF line endings by default" - "should handle save -> update -> save again"
1 parent 1fb817e commit 94f2bbe

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/json/edit.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* @fileoverview Editable JSON file manipulation with formatting preservation.
33
*/
44

5+
import { setTimeout as sleep } from 'node:timers/promises'
6+
57
import {
68
INDENT_SYMBOL,
79
NEWLINE_SYMBOL,
@@ -58,23 +60,47 @@ async function retryWrite(
5860
try {
5961
// eslint-disable-next-line no-await-in-loop
6062
await fsPromises.writeFile(filepath, content)
63+
// On Windows, add a delay and verify file exists to ensure it's fully flushed
64+
// This prevents ENOENT errors when immediately reading after write
65+
// Windows CI runners need longer delays than local development
66+
if (process.platform === 'win32') {
67+
// Initial delay to allow OS to flush the write
68+
// eslint-disable-next-line no-await-in-loop
69+
await sleep(25)
70+
// Verify the file is actually readable with retries
71+
let accessRetries = 0
72+
const maxAccessRetries = 4
73+
while (accessRetries < maxAccessRetries) {
74+
try {
75+
// eslint-disable-next-line no-await-in-loop
76+
await fsPromises.access(filepath)
77+
break
78+
} catch {
79+
// If file isn't accessible yet, wait with increasing delays
80+
const delay = 15 * (accessRetries + 1)
81+
// eslint-disable-next-line no-await-in-loop
82+
await sleep(delay)
83+
accessRetries++
84+
}
85+
}
86+
}
6187
return
6288
} catch (err) {
6389
const isLastAttempt = attempt === retries
64-
const isEperm =
90+
const isRetriableError =
6591
err instanceof Error &&
6692
'code' in err &&
67-
(err.code === 'EPERM' || err.code === 'EBUSY')
93+
(err.code === 'EPERM' || err.code === 'EBUSY' || err.code === 'ENOENT')
6894

69-
// Only retry on Windows EPERM/EBUSY errors, and not on the last attempt
70-
if (!isEperm || isLastAttempt) {
95+
// Only retry on Windows file system errors (EPERM/EBUSY/ENOENT), and not on the last attempt
96+
if (!isRetriableError || isLastAttempt) {
7197
throw err
7298
}
7399

74100
// Exponential backoff: 10ms, 20ms, 40ms
75101
const delay = baseDelay * 2 ** attempt
76102
// eslint-disable-next-line no-await-in-loop
77-
await new Promise(resolve => setTimeout(resolve, delay))
103+
await sleep(delay)
78104
}
79105
}
80106
}

0 commit comments

Comments
 (0)