You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fs.watch emitting file change when file getting read after some time #50920
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 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:
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.
@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:
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
@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.
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.
I agree with the previous comments stating that this is a libuv issue (if thought of as an issue to begin with) and I also understand the effects it has on big projects. I'd suggest opening an issue in libuv and discussing with the people there first. I'll close this one, but we can reopen it if needed.
Activity
belauzas commentedon Jul 21, 2024
This behavior is truly random and I couldn't understand why...
P.S. I am using v.18.16.1 on win10x64
huseyinacacak-janea commentedon Oct 23, 2024
I created a new empty folder and created a file and a folder inside of it. I got the following events.
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?
avivkeller commentedon Oct 23, 2024
@richterdennis can you retry the behavior from @huseyinacacak-janea?
richterdennis commentedon 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.mjsin the console. After opening a file in vs code I get the following output: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
avivkeller commentedon Oct 23, 2024
What about 20.18.0?
richterdennis commentedon Oct 23, 2024
I updated my node version real quick and yes it is still the same behavior.
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.
huseyinacacak-janea commentedon Oct 24, 2024
After further investigation, I discovered that this situation happens when you open a project folder in Visual Studio Code. For example, I added
index.mjsto the folder where Node.js was cloned. Then, I opened the folder in the VS Code and started to see a lot ofchangeevents. I clicked on thepropertiesof one of the changed files and saw that its last access time had changed. Therefore, it seems that Node.js is triggering achangeevent 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 commentedon Oct 24, 2024
@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:
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
avivkeller commentedon Oct 24, 2024
Do you know if the edited date on the file properties changed?
richterdennis commentedon Oct 24, 2024
huseyinacacak-janea commentedon Oct 25, 2024
Node.js subscribes to the events stated here.
node/deps/uv/src/win/fs-event.c
Lines 47 to 54 in 7ddd2c2
Could you please check the access times of the files that trigger the change event?
richterdennis commentedon Oct 25, 2024
@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_ACCESSwould be the fix. MaybeFILE_NOTIFY_CHANGE_ATTRIBUTEScould 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.node/deps/uv/src/win/fs-event.c
Line 307 in 7ddd2c2
avivkeller commentedon Oct 25, 2024
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 commentedon Oct 25, 2024
You guys know better about what the next steps should be. Unfortunately,
node --watchin 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.avivkeller commentedon Oct 25, 2024
If you think this is an issue, you should report it to libuv, which handles which events are watched.
StefanStojanovic commentedon Nov 14, 2024
I agree with the previous comments stating that this is a libuv issue (if thought of as an issue to begin with) and I also understand the effects it has on big projects. I'd suggest opening an issue in libuv and discussing with the people there first. I'll close this one, but we can reopen it if needed.