-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
fs: fix nonNativeWatcher
watching folder with existing files
#45500
Conversation
d76e5e4
to
4a4b599
Compare
Co-authored-by: Yagiz Nizipli <yagiz@nizipli.com>
CC @nodejs/fs |
@anonrig I was able to find the inconsistency on macOS: const fs = require('fs');
const path = require('path');
setInterval(() => fs.writeFileSync(__filename, fs.readFileSync(__filename)), 2000);
const watcher = fs.watch(__dirname, { recursive: true });
watcher.on('change', (event, filename) => {
if (filename === path.basename(__filename))
console.log(event, Date.now() - fs.statSync(filename).birthtimeMs);
}); outputs: ./node a.js
rename 3191.91748046875
rename 5191.91748046875
rename 7192.91748046875
rename 9192.91748046875
rename 11193.91748046875
rename 13194.91748046875
rename 15195.91748046875
rename 17195.91748046875
rename 19197.91748046875
rename 21198.91748046875
change 23199.91748046875
change 25200.91748046875
change 27201.91748046875 |
lib/internal/fs/recursive_watch.js
Outdated
@@ -202,6 +205,10 @@ class FSWatcher extends EventEmitter { | |||
this.emit('change', 'rename', pathRelative(this.#rootPath, file)); | |||
} else if (currentStats.isDirectory()) { | |||
this.#watchFolder(file); | |||
} else { | |||
// Watching a directory will trigger a change event for child files) | |||
const event = DateNow() - currentStats.birthtimeMs < NEW_FILES_AGE ? 'rename' : 'change'; |
There was a problem hiding this comment.
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
@MoLow This code does not produce
You are using |
@MoLow I dug into the Node code, as it seems if when the file is newly created, it triggers a I looked into both Meanwhile, I recommend, only emitting PS: My thoughts are: Let's keep the test as it is right now. |
@anonrig I have reverted the condition, can you approve? |
Landed in 147d810 |
PR-URL: nodejs#45500 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
PR-URL: #45500 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
PR-URL: #45500 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
@MoLow, same here - the test couldn't be landed in v18.x-staging. Do you mind creating a backport PR here too? |
PR-URL: nodejs#45500 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
PR-URL: nodejs#45500 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
a simple case that has not worked before this fix: