- Version: v10.16.0
- Platform: Darwin abc-d-mac15-111.local 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64
- Subsystem:
fs
In versions 10.16.0 and newer, fs.watch() does not notify change events when watching a specific file -- at least not when fs.write() is used in either the running or a different process. The change event is triggered in version 10.15.3 and older in such a use case, nevertheless.
Simple reproduction code:
watch.js
Watches a file called test.txt using fs.watch():
const fs = require('fs');
const filename = 'test.txt';
const fd = fs.openSync(filename, 'w');
fs.watch(filename, {}, (event) => {
console.log(event);
});
setTimeout(() => {
fs.closeSync(fd);
process.exit(0);
}, 300000);
write.js
Repeatedly writes text buffers onto test.txt:
const fs = require('fs');
const filename = 'test.txt';
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function run() {
const fd = fs.openSync(filename, 'a');
fs.writeSync(fd, '123\n')
await sleep(1000)
fs.writeSync(fd, '456\n')
await sleep(1000)
fs.writeSync(fd, '789\n')
await sleep(1000)
fs.writeSync(fd, 'abc\n')
await sleep(1000)
fs.writeSync(fd, 'def\n')
await sleep(1000)
fs.writeSync(fd, 'ghi\n')
await sleep(1000)
fs.closeSync(fd);
process.exit(0);
}
run();
Final output using node 10.16.0:
$ node watch.js
change
change
(i.e. change is only reported when test.txt is created and upon fd closing).
Final output using node 10.15.3:
$ node watch.js
change
change
change
change
change
change
(i.e. reported per each write)
Notes:
- A
tail -f test.txt running simultaneously vividly shows that the writes do take place properly even when the events are not reported by fs.watch().
- Appending content to the watched file through the shell (i.e. using
echo something >> test.txt) does trigger a change event in node 10.16.0 or higher.
fsIn versions
10.16.0and newer,fs.watch()does not notifychangeevents when watching a specific file -- at least not whenfs.write()is used in either the running or a different process. Thechangeevent is triggered in version10.15.3and older in such a use case, nevertheless.Simple reproduction code:
watch.js
Watches a file called
test.txtusingfs.watch():write.js
Repeatedly writes text buffers onto
test.txt:Final output using node
10.16.0:(i.e. change is only reported when
test.txtis created and upon fd closing).Final output using node
10.15.3:(i.e. reported per each write)
Notes:
tail -f test.txtrunning simultaneously vividly shows that the writes do take place properly even when the events are not reported byfs.watch().echo something >> test.txt) does trigger a change event in node10.16.0or higher.