Skip to content

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

Closed as not planned

Description

@richterdennis

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

Activity

  1. belauzas commented on Jul 21, 2024

    @belauzas

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

  2. huseyinacacak-janea commented on Oct 23, 2024

    @huseyinacacak-janea
    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?

  3. avivkeller commented on Oct 23, 2024

    @avivkeller
    Member

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

  4. richterdennis commented on Oct 23, 2024

    @richterdennis
    Author

    @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

  5. avivkeller commented on Oct 23, 2024

    @avivkeller
    Member

    What about 20.18.0?

  6. richterdennis commented on Oct 23, 2024

    @richterdennis
    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.

  7. added
    windowsIssues and PRs related to the Windows platform.
    watch-modeIssues and PRs related to watch mode.
    on Oct 23, 2024
  8. huseyinacacak-janea commented on Oct 24, 2024

    @huseyinacacak-janea
    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.

  9. richterdennis commented on Oct 24, 2024

    @richterdennis
    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

  10. avivkeller commented on Oct 24, 2024

    @avivkeller
    Member

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

  11. richterdennis commented on Oct 24, 2024

    @richterdennis
    Author
  12. huseyinacacak-janea commented on Oct 25, 2024

    @huseyinacacak-janea
    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?

  13. richterdennis commented on Oct 25, 2024

    @richterdennis
    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 |

  14. avivkeller commented on Oct 25, 2024

    @avivkeller
    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.

  15. richterdennis commented on Oct 25, 2024

    @richterdennis
    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.

  16. avivkeller commented on Oct 25, 2024

    @avivkeller
    Member

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

  17. added
    libuvIssues and PRs related to the libuv dependency or the uv binding.
    and removed
    windowsIssues and PRs related to the Windows platform.
    on Oct 25, 2024
  18. StefanStojanovic commented on Nov 14, 2024

    @StefanStojanovic
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    libuvIssues and PRs related to the libuv dependency or the uv binding.watch-modeIssues and PRs related to watch mode.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions