nodejs / nodejs/node

Strange behavior with `fs.watch` on file with JSON stringified -> first update isn't emitted if too fast after `fs.watch`

オープン
#53,111 コメント 14 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

confirmed-bug fs macos
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Darwin で fs.watch と直後の 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

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。