Skip to content

fs.watch() doesn't notify content changes starting v10.16.0 #28882

Description

@d4vidi
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateIssues and PRs that are duplicates of other issues or PRs.fsIssues and PRs related to the fs subsystem / file system.macosIssues and PRs related to the macOS platform / OSX.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions