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 4 commits
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
7 changes: 7 additions & 0 deletions lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ArrayPrototypePush,
DateNow,
SafePromiseAllReturnVoid,
Promise,
PromisePrototypeThen,
Expand Down Expand Up @@ -33,6 +34,8 @@ const {
let internalSync;
let internalPromises;

const NEW_FILES_AGE = 20_000;

function lazyLoadFsPromises() {
internalPromises ??= require('fs/promises');
return internalPromises;
Expand Down Expand Up @@ -202,6 +205,10 @@ 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 directory will trigger a change event for child files)
const event = DateNow() - currentStats.birthtimeMs < NEW_FILES_AGE ? 'rename' : 'change';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this boolean expression better for detecting the creation of files?

currentStats.birthTimeMs !== 0 && previousStats.birthtimeMs === 0

this.emit('change', event, pathRelative(this.#rootPath, file));
}
});
}
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