Issue involves deep interaction between Node.js and libuv, requiring specialized knowledge.
The issue involves `fs.watch` not detecting changes to JSON files when changes occur quickly, potentially due to a libuv issue. The problem requires investigation into the interaction between Node.js and libuv, and possibly changes to libuv itself. This is a complex issue requiring deep domain knowledge.
v20.11.0
23.2.0 Darwin
node:fs
const fs = require('fs');
const path = require('path');
const filePath = path.join(__dirname, 'test.json');
// Create the file if it doesn't exist
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, "{ }");
}
fs.watch(filePath, (eventType, filename) => {
console.log(`Event type is: ${eventType}`);
if (filename) {
console.log(`Filename provided: ${filename}`);
} else {
console.log('Filename not provided');
}
});
fs.writeFileSync(filePath, "{ \"test\": \"test\", \"test2\": \"test2\" }"); // same with JSON.stringify(...)
// Listener above is called if we replace line above with:
// fs.writeFileSync(filePath, "very ok or not but I need a longer text to test something...");
With the code above it happens all the time. But if you change the content of the file to normal string (not JSON stringified) it seems to happen only few times but I don't know why...
Something to keep in mind is that if we add a delay before updating the file, it will always be detected (JSON stringified or not)
Event type is: change
Filename provided: test.json
Nothing
I wanted to watch changes on a file storing JSON stringified data but the listener passed in fs.watch is not called if the change happen too quickly after the call of fs.watch. The strange thing is that if the content is only a text (without JSON format) the watcher emit the event correctly...
I have tested this behavior on different version of node :
Before creating this issue here, I created a help request ( https://github.com/nodejs/node/issues/531
Claim this issue to let others know you're working on it. You'll earn 40 points when you complete it!