Skip to content

fs.watch recursive throws on file removal #49995

Closed as not planned
Closed as not planned

Description

@ShaMan123

Details

Using fs.watch to recursively watch a directory.
After deleting a file under the directory the watcher throws:

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: ENOENT: no such file or directory, open 'C:\Users\DELL\Desktop\DEV\fabric.js\e2e\tests\template\index.ts'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\DELL\\Desktop\\DEV\\fabric.js\\e2e\\tests\\template\\index.ts'
}

Node.js v18.17.0

Expected it not to throw but to call the watcher callback with the path of the deleted file with event type removed or something of the likes

Node.js version

18.17.0

Example code

const watcher = watch(
  src,
  { recursive: true, persistent: true },
  (type, filename) => {
    console.log({ filename });
    const file = path.join(src, filename);
  }
);

Operating system

Windows 10 64 bit

Scope

code

Module and version

fs

Activity

  1. transferred this issue fromnodejs/helpon Oct 1, 2023
  2. benjamingr commented on Oct 1, 2023

    @benjamingr
    Member

    Hey I moved it here since it seems like a bug report.

    cc @nodejs/fs

  3. added
    fsIssues and PRs related to file-system APIs and the fs module.
    windowsIssues and PRs related to the Windows platform.
    on Oct 1, 2023
  4. ShaMan123 commented on Oct 1, 2023

    @ShaMan123
    Author

    Thanks

  5. nathannorthcutt commented on Apr 17, 2024

    @nathannorthcutt

    I get the same issue both on my local WSL (5.15.146.1-microsoft-standard-WSL2) as well as CI runner (Linux). Looks like the problem stems from

    https://github.com/nodejs/node/blob/main/lib/internal/fs/recursive_watch.js#L160
    const currentStats = statSync(file);

    statSync by default throws an error if there is no entry provided
    https://github.com/nodejs/node/blob/main/lib/fs.js#L1654

    function statSync(path, options = { bigint: false, throwIfNoEntry: true }) {

    Still digging into the difference between this and the non-recursive file system watcher but seems to affect multiple OS versions and not just Windows. Not in a good place to build and test node locally to verify atm but thought I'd throw that out there in case someone else can get to it first.

  6. benjamingr commented on Apr 17, 2024

    @benjamingr
    Member

    @redyetidev this seems reasonably scoped

  7. avivkeller commented on Apr 17, 2024

    @avivkeller
    Member

    @redyetidev this seems reasonably scoped

    Thanks! I'll take a look!

  8. avivkeller commented on Apr 17, 2024

    @avivkeller
    Member

    Yes! I see the issue, I'll figure out why fs stat is being called

  9. MoLow commented on Apr 18, 2024

    @MoLow
    Member

    Closing as duplicate of #52018

  10. reopened this on Apr 18, 2024
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