From 1cd6b2c113eaf1937af4bd8d8c57313d34096f4a Mon Sep 17 00:00:00 2001 From: Yohannes Desta Date: Tue, 24 Nov 2026 15:18:48 +0300 Subject: [PATCH] fs: keep watching directories recreated under a recursive watch On Linux, recursive fs.watch() keeps one inotify watcher per directory. When a directory below the root was removed and created again before its events were handled, the parent's event found a directory at the same path, and the old watcher, which no longer reports anything, was kept. Changes inside the new directory were then never reported, which also stopped `node --watch-path` from restarting. Handle the event the old watcher emits about the directory itself: if the path now holds a different directory, or nothing, drop the old watcher and start over with what is there. Inotify reports metadata changes to the directory in the same way, and the new directory often reuses the inode number, so directories are compared by inode number and birth time. Fixes: https://github.com/nodejs/node/issues/67592 Signed-off-by: Yohannes Desta --- lib/internal/fs/recursive_watch.js | 36 +++++++++++++ ...atch-recursive-linux-recreate-directory.js | 51 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 test/parallel/test-fs-watch-recursive-linux-recreate-directory.js diff --git a/lib/internal/fs/recursive_watch.js b/lib/internal/fs/recursive_watch.js index 85d84f9dd8f3..d9d2ea7f6d95 100644 --- a/lib/internal/fs/recursive_watch.js +++ b/lib/internal/fs/recursive_watch.js @@ -61,6 +61,13 @@ function isPathMissingForWatch(error, file) { // elsewhere every file keeps a watcher of its own as well. const kDirectoryWatchReportsEntries = process.platform === 'linux'; +// Inotify reports a change to a directory's own metadata the same way as its +// removal, and a directory created in place of a removed one often gets the +// same inode number, so the birth time is compared as well. +function directoryIdentity(stats) { + return stats === undefined ? undefined : `${stats.ino}:${stats.birthtimeNs}`; +} + class FSWatcher extends EventEmitter { #options = null; #closed = false; @@ -70,6 +77,9 @@ class FSWatcher extends EventEmitter { // directory watch does not report its entries). #watchers = new SafeMap(); #symbolicLinks = new SafeSet(); + // The identity of each directory watched below the root (see + // directoryIdentity()), where the directory watch reports its entries. + #directories = new SafeMap(); #rootPath = pathResolve(); #ignoreMatcher = null; @@ -126,6 +136,7 @@ class FSWatcher extends EventEmitter { this.#watchers.clear(); this.#entries.clear(); this.#symbolicLinks.clear(); + this.#directories.clear(); this.emit('close'); } @@ -141,12 +152,14 @@ class FSWatcher extends EventEmitter { } this.#entries.delete(file); this.#symbolicLinks.delete(file); + this.#directories.delete(file); const childPrefix = file + pathSep; for (const entry of this.#entries) { if (StringPrototypeStartsWith(entry, childPrefix)) { this.#entries.delete(entry); this.#symbolicLinks.delete(entry); + this.#directories.delete(entry); const childWatcher = this.#watchers.get(entry); if (childWatcher !== undefined) { childWatcher.close(); @@ -188,6 +201,12 @@ class FSWatcher extends EventEmitter { this.#forget(folder); return; } + if (kDirectoryWatchReportsEntries && folder !== this.#rootPath && + !this.#directories.has(folder)) { + const { lstatSync } = lazyLoadFsSync(); + this.#directories.set(folder, directoryIdentity( + lstatSync(folder, { bigint: true, throwIfNoEntry: false }))); + } const { readdirSync } = lazyLoadFsSync(); let entries; @@ -271,6 +290,23 @@ class FSWatcher extends EventEmitter { } else if (folder === this.#rootPath && statSync(folder, { throwIfNoEntry: false }) === undefined) { this.#emit('rename', folder); this.#forget(folder); + } else if (folder !== this.#rootPath && filename === pathBasename(folder)) { + // An event about `folder` itself: its metadata changed, or it was + // removed or moved away. In the latter case its watcher reports nothing + // more, and something may already exist under the same name (e.g. the + // directory was removed and created again before this event was + // handled), so start over with that. + const current = lstatSync(folder, { bigint: true, throwIfNoEntry: false }); + if (current?.isDirectory() && + directoryIdentity(current) === this.#directories.get(folder)) { + return; + } + this.#forget(folder); + if (current !== undefined) { + this.#addEntry(folder, current, false); + } else { + this.#emit('rename', folder); + } } return; } diff --git a/test/parallel/test-fs-watch-recursive-linux-recreate-directory.js b/test/parallel/test-fs-watch-recursive-linux-recreate-directory.js new file mode 100644 index 000000000000..17a47a38ca3d --- /dev/null +++ b/test/parallel/test-fs-watch-recursive-linux-recreate-directory.js @@ -0,0 +1,51 @@ +'use strict'; +const common = require('../common'); + +// A directory below a recursive fs.watch() root that is removed and created +// again before its events are handled must still be watched afterwards. +// Refs: https://github.com/nodejs/node/issues/67592 + +if (!common.isLinux) + common.skip('the recursive watcher is native on this platform'); + +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const tmpdir = require('../common/tmpdir'); + +tmpdir.refresh(); + +const root = tmpdir.resolve('root'); +const sub = path.join(root, 'sub'); +fs.mkdirSync(sub, { recursive: true }); + +const watcher = fs.watch(root, { recursive: true }); +let step = 0; + +function recreate(name) { + fs.rmSync(sub, { recursive: true, force: true }); + fs.mkdirSync(sub); + fs.writeFileSync(path.join(sub, name), name); +} + +watcher.on('change', common.mustCallAtLeast((eventType, filename) => { + if (step === 0 && filename === path.join('sub', 'a.txt')) { + step = 1; + setTimeout(() => recreate('b.txt'), common.platformTimeout(100)); + } else if (step === 1 && filename === path.join('sub', 'b.txt')) { + step = 2; + setTimeout(() => { + fs.writeFileSync(path.join(sub, 'c.txt'), 'c'); + }, common.platformTimeout(100)); + } else if (step === 2 && filename === path.join('sub', 'c.txt')) { + assert.strictEqual(eventType, 'rename'); + step = 3; + watcher.close(); + } +})); + +setTimeout(() => recreate('a.txt'), common.platformTimeout(100)); + +process.on('exit', () => { + assert.strictEqual(step, 3); +});