Skip to content

fs.watch not emitting file change when file is in use by another app on Windows #36888

Closed

Description

@MasterIO02
  • Version: v15.5.1 but using 14.15.4 LTS is the same
  • Platform: Microsoft Windows NT 10.0.17763.0 x64

What steps will reproduce the bug?

Hi! I'm trying to use fs.watch on Windows to check for file change of the latest.log file of Minecraft. On Linux there is no problems, every time using this code it detects file change made by Minecraft and then I can do whatever I want with the file knowing that is has been modified. On Windows, however, it detects file change only when I open it with notepad or right-click it. It never emits a file change when Minecraft writes in it.

const fs = require('fs');
const fileLocation = 'C:/Users/username/AppData/Roaming/.minecraft/logs/latest.log';

let fsWait = false;
fs.watch(fileLocation, (event, filename) => {
    if (filename) {
        if (fsWait) return;
        fsWait = setTimeout(() => {
            fsWait = false;
        }, 10);
        console.log(`${filename} file Changed`);
    }
});

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

When the file is in use by Minecraft/Java.

What is the expected behavior?

Getting logged that the file changed every time 'latest.log' get written to.

What do you see instead?

I'm getting logged that 'latest.log' gets written to only when I open it with notepad or when I right-click it.

Additional information

Using fs.watchFile works ok but I can't use it because of the CPU usage.
On Minecraft startup a file change is detected because the game deletes the latest.log and recreates it. But it should log a file modification each time the game adds a line in the log.
Sorry if this issue represents only the bug using Minecraft but I can't think of another realistic way.

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    windowsIssues and PRs related to the Windows platform.
    on Jan 12, 2021
  2. GrosSacASac commented on Jan 14, 2021

    @GrosSacASac
    Contributor

    Thanks for the report.

    In the meantime you could try https://github.com/paulmillr/chokidar

  3. MasterIO02 commented on Jan 26, 2021

    @MasterIO02
    Author

    I need to check for the file every .5ms, fs.watchFile and Chokidar only allows up to 1ms checks (need to be an integer). fs.watch can check every .5ms, so this is what I need but it doesn't work on Windows.

  4. jasnell commented on Mar 5, 2021

    @jasnell
    Member

    Hmm... this could be more of a libuv problem than a Node.js problem... @nodejs/libuv ... any thoughts?

  5. lucassilvas1 commented on Apr 11, 2024

    @lucassilvas1

    Any update on this? I'm facing this exact problem right now and want to avoid polling at all costs.

  6. StefanStojanovic commented on Aug 26, 2024

    @StefanStojanovic
    Contributor

    This issue has been stale for some time, but more importantly, there is another issue similar to this one with much more details on this topic. With that in mind, I'll close this one in favor of the new one and discussion can continue there.

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

    fsIssues and PRs related to file-system APIs and the fs module.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions