diff --git a/lib/internal/fs/recursive_watch.js b/lib/internal/fs/recursive_watch.js index 85d84f9dd8f3..e65d3844fc8d 100644 --- a/lib/internal/fs/recursive_watch.js +++ b/lib/internal/fs/recursive_watch.js @@ -305,7 +305,7 @@ class FSWatcher extends EventEmitter { return; } if (statSync(file, { throwIfNoEntry: false }) === undefined) { - this.#emit('rename', file); + this.emit('change', 'rename', pathBasename(file)); this.#forget(file); } else { this.emit('change', 'change', pathBasename(file)); diff --git a/test/parallel/test-fs-watch-recursive-linux-directory-watchers.js b/test/parallel/test-fs-watch-recursive-linux-directory-watchers.js index bb68893f2d40..516b02c7cc4b 100644 --- a/test/parallel/test-fs-watch-recursive-linux-directory-watchers.js +++ b/test/parallel/test-fs-watch-recursive-linux-directory-watchers.js @@ -89,11 +89,10 @@ function watchUntil(target, done) { } { - // So is removing a watched root that is a file (with an empty filename, - // as before). + // Removing a watched root that is a file is reported. const lone = tmpdir.resolve('lone.txt'); fs.writeFileSync(lone, 'x'); - const events = watchUntil(lone, (seen) => seen.includes('rename ')); + const events = watchUntil(lone, (seen) => seen.includes('rename lone.txt')); await delay(); fs.rmSync(lone); await events; diff --git a/test/parallel/test-fs-watch-recursive-root-file.js b/test/parallel/test-fs-watch-recursive-root-file.js new file mode 100644 index 000000000000..aa7b14e43b83 --- /dev/null +++ b/test/parallel/test-fs-watch-recursive-root-file.js @@ -0,0 +1,34 @@ +'use strict'; + +const common = require('../common'); + +if (!common.isLinux) + common.skip('the recursive watcher is native on this platform'); + +const assert = require('assert'); +const fs = require('fs'); +const fsPromises = require('fs/promises'); +const path = require('path'); +const tmpdir = require('../common/tmpdir'); + +tmpdir.refresh(); + +(async () => { + const filename = 'root-file.txt'; + const file = tmpdir.resolve(filename); + fs.writeFileSync(file, 'content'); + + const watcher = fsPromises.watch(file, { recursive: true }); + const event = watcher.next(); + + process.nextTick(common.mustCall(() => fs.rmSync(file))); + + try { + const { value, done } = await event; + assert.strictEqual(done, false); + assert.strictEqual(value.eventType, 'rename'); + assert.strictEqual(value.filename, path.basename(file)); + } finally { + await watcher.return(); + } +})().then(common.mustCall());