Strange behavior with `fs.watch` on file with JSON stringified -> first update isn't emitted if too fast after `fs.watch`
未关闭
还没有人认领这个 Issue。
confirmed-bug
fs
macos
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.3k
- 平均合并
- 4 天 2 小时
- 30 天内合并 PR
- 283
描述
Node.js Version
v18.17.1
NPM Version
9.6.7
Operating System
23.2.0 Darwin
Subsystem
fs
Description
I would like to watch changes on a file storing JSON stringified data but the watcher created with fs.watch is not emitted if the change happen to 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...
Minimal Reproduction
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...");
Output
Expected
Event type is: change
Filename provided: test.json
Received
Nothing
Before You Submit
- I have looked for issues that already exist before submitting this
- My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先使用 fs.watch 和在 Darwin 上立即调用 fs.writeFileSync 来创建最小复现。将 JSON-stringified write 与更长的 plain-text write 进行比较,并调查第一个事件的时序行为。当 JSON write 能够可靠地发出预期的 change event,且不破坏 fs.watch 已记录的行为时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, node.js
- 领域
- operating-systems
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100