Skip to content

Commit ec553d9

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 50ms initial delay after write on Windows 3. Verify file accessibility with up to 5 retries with increasing delays (20ms, 40ms, 60ms, 80ms, 100ms) 4. Add 10ms stabilization delay after successful access 5. Total potential wait time: 50ms + (20+40+60+80+100)ms + 10ms = 360ms 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 significantly slower than local development environments. This fixes all intermittent test failures in Windows CI runners.
1 parent 1fb817e commit ec553d9

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/json/edit.ts

Lines changed: 34 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,50 @@ 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 are significantly slower 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(50)
70+
// Verify the file is actually readable with retries
71+
let accessRetries = 0
72+
const maxAccessRetries = 5
73+
while (accessRetries < maxAccessRetries) {
74+
try {
75+
// eslint-disable-next-line no-await-in-loop
76+
await fsPromises.access(filepath)
77+
// Small final delay to ensure stability
78+
// eslint-disable-next-line no-await-in-loop
79+
await sleep(10)
80+
break
81+
} catch {
82+
// If file isn't accessible yet, wait with increasing delays
83+
const delay = 20 * (accessRetries + 1)
84+
// eslint-disable-next-line no-await-in-loop
85+
await sleep(delay)
86+
accessRetries++
87+
}
88+
}
89+
}
6190
return
6291
} catch (err) {
6392
const isLastAttempt = attempt === retries
64-
const isEperm =
93+
const isRetriableError =
6594
err instanceof Error &&
6695
'code' in err &&
67-
(err.code === 'EPERM' || err.code === 'EBUSY')
96+
(err.code === 'EPERM' || err.code === 'EBUSY' || err.code === 'ENOENT')
6897

69-
// Only retry on Windows EPERM/EBUSY errors, and not on the last attempt
70-
if (!isEperm || isLastAttempt) {
98+
// Only retry on Windows file system errors (EPERM/EBUSY/ENOENT), and not on the last attempt
99+
if (!isRetriableError || isLastAttempt) {
71100
throw err
72101
}
73102

74103
// Exponential backoff: 10ms, 20ms, 40ms
75104
const delay = baseDelay * 2 ** attempt
76105
// eslint-disable-next-line no-await-in-loop
77-
await new Promise(resolve => setTimeout(resolve, delay))
106+
await sleep(delay)
78107
}
79108
}
80109
}

0 commit comments

Comments
 (0)