Strange behavior with `fs.watch` on file with JSON stringified -> first update isn't emitted if too fast after `fs.watch`
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- JavaScript
- Star
- 122k
- Fork
- 37.3k
- Merge trung bình
- 4 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 283
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với bản tái hiện tối thiểu bằng cách sử dụng fs.watch và lệnh gọi fs.writeFileSync ngay lập tức trên Darwin. So sánh JSON-stringified write với plain-text write dài hơn và điều tra hành vi về thời điểm của sự kiện đầu tiên. Được coi là hoàn tất khi change event mong đợi được phát ra một cách đáng tin cậy cho JSON write mà không làm hỏng hành vi được tài liệu hóa của fs.watch.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, node.js
- Lĩnh vực
- operating-systems
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100