diff --git a/lib/internal/fs/watch/linux.js b/lib/internal/fs/watch/linux.js index 55c0e436f5a929..415e513bbf7408 100644 --- a/lib/internal/fs/watch/linux.js +++ b/lib/internal/fs/watch/linux.js @@ -23,25 +23,18 @@ function lazyLoadFsSync() { return internalSync; } -async function traverse(dir, parent = undefined, files = new SafeMap()) { - const { readdir, stat } = lazyLoadFsPromises(); +async function traverse(dir, files = new SafeMap()) { + const { readdir } = lazyLoadFsPromises(); const filenames = await readdir(dir, { withFileTypes: true }); - if (parent !== undefined) { - files.set(dir, parent); - } else { - // Optimization: Only call `stat` on the root element. - files.set(dir, await stat(dir)); - } - for await (const file of filenames) { const f = path.join(dir, file.name); files.set(f, file); if (file.isDirectory()) { - await traverse(f, file, files); + await traverse(f, files); } } @@ -168,9 +161,12 @@ class FSWatcher extends EventEmitter { } async [kFSWatchStart](filename) { + const { stat } = lazyLoadFsPromises(); + this.#rootPath = filename; this.#closed = false; this.#files = await traverse(filename); + this.#files.set(filename, await stat(filename)); for (const f of this.#files.keys()) { this.#watchFile(f); diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 6e0315ccce9197..209080d59c4a33 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -67,6 +67,7 @@ const expectedModules = new Set([ 'NativeModule internal/fs/read_file_context', 'NativeModule internal/fs/rimraf', 'NativeModule internal/fs/utils', + 'NativeModule internal/fs/watch/linux', 'NativeModule internal/fs/watchers', 'NativeModule internal/heap_utils', 'NativeModule internal/histogram',