nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
106.52k stars 29.03k forks source link

fs.watch emitting file change when file getting read after some time #50920

Open richterdennis opened 9 months ago

richterdennis commented 9 months ago

Version

v20.10.0

Platform

Microsoft Windows NT 10.0.22621.0 x64

Subsystem

No response

What steps will reproduce the bug?

Create file index.mjs in a random folder of your system (I am using Win 11) with files in it.

index.mjs content:

import { watch } from 'node:fs';

const watcher = watch('./', { recursive: true });
watcher.on('change', (eventType, fileName) => console.log(eventType, fileName));

Open a console and run node index.mjs

Then just browse through the folders and files and maybe open files with your IDE.

If I open a file from a random project I get for example:

change src\Config.js

Just by opening it.

fs.watch is also used in the node --watch implementation which is causing multiple reloads just because VS Code is reading some files.

How often does it reproduce? Is there a required condition?

The event occurs on the first read and then after some time. I don't know more details.

What is the expected behavior? Why is that the expected behavior?

The expected behavior is not triggering a change event when nothing changed.

What do you see instead?

Getting a change event triggered by opening a file..

Additional information

Sometimes there is also a

change null
belauzas commented 1 month ago

This behavior is truly random and I couldn't understand why... P.S. I am using v.18.16.1 on win10x64