Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
richterdennis opened this issue Nov 26, 2023 · 15 comments
Open
Labels
libuv Issues and PRs related to the libuv dependency or the uv binding. watch-mode Issues and PRs related to watch mode

Comments

@richterdennis
Copy link

richterdennis commented Nov 26, 2023

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
Copy link

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

@huseyinacacak-janea
Copy link
Contributor

I created a new empty folder and created a file and a folder inside of it. I got the following events.

>node index.mjs
rename New folder
rename New Text Document.txt
rename New folder\New Text Document.txt
change New folder

It seems that all the events are correct. In addition, I opened the parent folder using Visual Studio Code and clicked on all the files and folders there. No event was generated. As a result, I was not able to reproduce the issue with the latest versions of Node.js (23.0.0, 22.9.0, 20.18.0, and 18.20.4). Do you think this issue can be closed now?

@RedYetiDev
Copy link
Member

@richterdennis can you retry the behavior from @huseyinacacak-janea?

@richterdennis
Copy link
Author

richterdennis commented Oct 23, 2024

@RedYetiDev It is still the same behavior like I reported in the beginning. I opened my code folder created an index.mjs like in the original post then exec node index.mjs in the console. After opening a file in vs code I get the following output:

PS D:\Code> node index.mjs
change projectA\.jshintrc
change projectA\package.json
change projectA\.git\HEAD
change projectA\.git
change projectA\.gitignore

Even if I open files in other editors like the windows default text editor and without git involved it fires a change event for this file.

I am using node v20.10.0 on Win11 23H2

@RedYetiDev
Copy link
Member

What about 20.18.0?

@richterdennis
Copy link
Author

I updated my node version real quick and yes it is still the same behavior.

PS D:\Code\Web> node -v
v20.18.0
PS D:\Code\Web> node index.mjs
change private\ocr\index.html

Just by opening the file. Maybe it is relevant: I was not in this folder before or read a file from it. When I reopen the file no event is triggered.

@RedYetiDev RedYetiDev added windows Issues and PRs related to the Windows platform. watch-mode Issues and PRs related to watch mode labels Oct 23, 2024
@huseyinacacak-janea
Copy link
Contributor

After further investigation, I discovered that this situation happens when you open a project folder in Visual Studio Code. For example, I added index.mjs to the folder where Node.js was cloned. Then, I opened the folder in the VS Code and started to see a lot of change events. I clicked on the properties of one of the changed files and saw that its last access time had changed. Therefore, it seems that Node.js is triggering a change event because of this change in the last access time.

Note that this doesn't happen when you create a new folder and open it in the VS Code. For some reason, the VS Code doesn't change its last access time. It's probably related to the way the VS Code is implemented.

Given these findings, this problem is most likely related to the VS Code, not Node.js.

@richterdennis
Copy link
Author

@huseyinacacak-janea It is not only vs code. It also happens with other editors like the Windows TextEditor. Even the img preview of windows explorer triggering the event. I put the index.mjs in my images folder and just opened a sub folder and get a change event for every image:

PS D:\Images> node index.mjs
change Bench
change Bench\C.R23_First.png
change Bench\FireStrike.png
change Bench\Screenshot 2023-03-02 200924.png
change Bench\TimeSpy.png

To make it clear, I was not browsing in the subfolder before. I checkt the meta information of the files and the last access time was updated. But this should not trigger the change event of node watch imo, because the file it self did not changed

@RedYetiDev
Copy link
Member

Do you know if the edited date on the file properties changed?

@richterdennis
Copy link
Author

richterdennis commented Oct 24, 2024 via email

@huseyinacacak-janea
Copy link
Contributor

Node.js subscribes to the events stated here.

FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_SECURITY,

Could you please check the access times of the files that trigger the change event?

@richterdennis
Copy link
Author

@huseyinacacak-janea yes the last access time of the files that triggering the event got updated which makes sense because they got accessed. Thank you for sharing the part where the magic happens. I guess removing FILE_NOTIFY_CHANGE_LAST_ACCESS would be the fix. Maybe FILE_NOTIFY_CHANGE_ATTRIBUTES could also be a part of the problem. The flags also occurring later in the file. I don't know if this is also a thing.

FILE_NOTIFY_CHANGE_LAST_ACCESS |

@RedYetiDev
Copy link
Member

If those dates changed, which they did, this is not an issue that is actionable from Node.js.

If anything, it would be an libuv issue.

@richterdennis
Copy link
Author

You guys know better about what the next steps should be. Unfortunately, node --watch in its current state isn't usable for larger projects on Windows. I'm sure you understand—it's not great when the project restarts hundreds of times in a split second in the middle of a process just because GIT or the IDE are reading files. However, since we have a working solution with nodemon, it's not the end of the world.

@RedYetiDev
Copy link
Member

If you think this is an issue, you should report it to libuv, which handles which events are watched.

@RedYetiDev RedYetiDev added libuv Issues and PRs related to the libuv dependency or the uv binding. and removed windows Issues and PRs related to the Windows platform. labels Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libuv Issues and PRs related to the libuv dependency or the uv binding. watch-mode Issues and PRs related to watch mode
Projects
None yet
Development

No branches or pull requests

4 participants