Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: fix nonNativeWatcher watching folder with existing files #45500

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ class FSWatcher extends EventEmitter {
this.emit('change', 'rename', pathRelative(this.#rootPath, file));
} else if (currentStats.isDirectory()) {
this.#watchFolder(file);
} else {
anonrig marked this conversation as resolved.
Show resolved Hide resolved
// Watching a dircetory will trigger a change event for child files
MoLow marked this conversation as resolved.
Show resolved Hide resolved
this.emit('change', 'change', pathRelative(this.#rootPath, file));
anonrig marked this conversation as resolved.
Show resolved Hide resolved
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-fs-watch-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,37 @@ const tmpdir = require('../common/tmpdir');
const testDir = tmpdir.path;
tmpdir.refresh();

(async () => {
// Watch a folder and update an already existing file in it.

const rootDirectory = fs.mkdtempSync(testDir + path.sep);
const testDirectory = path.join(rootDirectory, 'test-0');
fs.mkdirSync(testDirectory);

const testFile = path.join(testDirectory, 'file-1.txt');
fs.writeFileSync(testFile, 'hello');

const watcher = fs.watch(testDirectory, { recursive: true });
let watcherClosed = false;
watcher.on('change', common.mustCallAtLeast(function(event, filename) {
anonrig marked this conversation as resolved.
Show resolved Hide resolved
// Libuv inconsistenly emits a rename event for the file we are watching
MoLow marked this conversation as resolved.
Show resolved Hide resolved
assert.ok(event === 'change' || event === 'rename');
anonrig marked this conversation as resolved.
Show resolved Hide resolved

if (filename === path.basename(testFile)) {
watcher.close();
watcherClosed = true;
}
}));

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(testFile, 'hello');

process.once('exit', function() {
assert(watcherClosed, 'watcher Object was not closed');
});
})().then(common.mustCall());


(async () => {
// Add a file to already watching folder

Expand Down