Repository navigation
test-fs-watch-recursive-add-* tests fail intermittently on macOS #55592
Description
Activity
- addedtestIssues and PRs related to Node.js core tests and test infrastructure.Issues and PRs related to Node.js core tests and test infrastructure.flaky-testIssues and PRs involving tests that fail intermittently in CI.Issues and PRs involving tests that fail intermittently in CI.fsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.
on Oct 29, 2024 Take
test-fs-watch-recursive-add-file-with-url.jsfor example, it seems instead of emitting justrenameforfile-8.txt,fs.watchalso emitschangefortest-5first. I am not sure if this is expected behavior, if it is expected that adding files would also incur a change event for the directory, then I think it's the tests that should be updated. cc @mcollina @anonrig- addedmacosIssues and PRs related to the macOS platform.Issues and PRs related to the macOS platform.
on Oct 29, 2024 I also reported it on Slack: https://openjs-foundation.slack.com/archives/C019Y2T6STH/p1728922765557729
@santigimeno Suggested to try this patch (I didn't have the time to test it):
diff --git a/test/parallel/test-fs-watch-recursive-add-file.js b/test/parallel/test-fs-watch-recursive-add-file.js index 27b933871c..68c17890fc 100644 --- a/test/parallel/test-fs-watch-recursive-add-file.js +++ b/test/parallel/test-fs-watch-recursive-add-file.js @@ -28,22 +28,32 @@ fs.mkdirSync(testDirectory); const testFile = path.join(testDirectory, 'file-1.txt'); -const watcher = fs.watch(testDirectory, { recursive: true }); -let watcherClosed = false; -watcher.on('change', function(event, filename) { - assert.strictEqual(event, 'rename'); - - if (filename === path.basename(testFile)) { - watcher.close(); - watcherClosed = true; - } -}); - -// Do the write with a delay to ensure that the OS is ready to notify us. -setTimeout(() => { - fs.writeFileSync(testFile, 'world'); -}, common.platformTimeout(200)); - -process.once('exit', function() { - assert(watcherClosed, 'watcher Object was not closed'); -}); +function doWatch() { + const watcher = fs.watch(testDirectory, { recursive: true }); + let watcherClosed = false; + watcher.on('change', function(event, filename) { + assert.strictEqual(event, 'rename'); + + if (filename === path.basename(testFile)) { + watcher.close(); + watcherClosed = true; + } + }); + + // Do the write with a delay to ensure that the OS is ready to notify us. + setTimeout(() => { + fs.writeFileSync(testFile, 'world'); + }, common.platformTimeout(200)); + + process.once('exit', function() { + assert(watcherClosed, 'watcher Object was not closed'); + }); +} + +if (common.isMacOS) { + // On macOS delay watcher start to avoid leaking previous events. + // Refs: https://github.com/libuv/libuv/pull/4503 + setTimeout(doWatch, common.platformTimeout(100)); +} else { + doWatch(); +}
If it's working as expected and it's the test that needs to be fixed, #55592 (comment) still doesn't seem very reliable with the 200 timeout, it could just be adjusted to tolerate that change event emitted for the directory instead of only expecting one single rename event emitted for the changed file.
- added a commit that references this issue
on Jun 20, 2026
I am not sure exactly when but at least since this week I am seeing intermittent failures of
test-fs-watch-recursive-add-*tests on the main branch. Trying to run with-j1or simply re-running them directly doesn't seem to make a difference either.