Skip to content

fs.watch() reports removed directory as subdirectory of itself #25301

Closed

Description

@Krinkle
  • Affected versions: Latest release of Current and all LTSes - 11.6.0, 10.15.0, 8.15.0, and 6.16.0.
  • Affected platform: Linux (ok on macOS/Windows)
  • Subsystem: fs

Description

When using fs.watch() to watch a directory, and removing that very directory itself, a change event is emitted with a filename matching the directory itself.

To my knowledge, the standard way to obtain an absolute path of the affected file, is to path.join() the filename with the directory being watched.

As such, the emitted event is interpreted as if a subdirectory changed, which is incorrect.

Test case

Code at https://github.com/Krinkle/sandbox/blob/node-fs-watch-removedir/test.js.

Build output:

In brief:

  1. Given a directory structure as follows:
    - home/
          - a/
             - file1
          - b/
             - file1
             - file2
          - c/
  1. And creating an fs.FSWatcher for home/ and home/c, via fs.watch(dir, { encoding: 'utf8', recursive: false }).
  2. And removing home/c/ via fs.rmdirSync(dir).

Expected

Change event for home/ or home/c/. Specifically, one or more of:

  • An event on home/ with a filename of 'c', '/c', or no filename.
  • An event on home/c/ with a filename of '' (empty string),'.' (empty string), '/', or no filename.

Actual

On macOS, gets a change event for home/c/ as expected:

  1. FSWatcher for home/ receives change event; eventType=rename, filename=c

On macOS, gets a change event for home/c/ as expected:

  1. FSWatcher for home/ receives change event; eventType=rename, filename=c

On Linux, gets change events for /home/c/c/: ❌

  1. FSWatcher for home/c/ receives change event; eventType=rename, filename=c ❌
  2. FSWatcher for home/c/ receives change event; eventType=rename, filename=c ❌
  3. FSWatcher for home/ receives change event; eventType=rename, filename=c ✅

Activity

  1. added
    confirmed-bugIssues and PRs for confirmed bugs.
    fsIssues and PRs related to file-system APIs and the fs module.
    on Jan 8, 2019
  2. bnoordhuis commented on Jan 8, 2019

    @bnoordhuis
    Member

    I can confirm the behavior you're seeing and while I agree it's inconvenient, I don't think this is really fixable. Libuv bubbles up the events it receives from the operating system mostly unfiltered.

    On macos, it receives just one event. On linux, it receives three. I tried a couple of things but filtering out the first two introduces more issues than it fixes.

  3. bnoordhuis commented on Jan 23, 2019

    @bnoordhuis
    Member

    There's been no further discussion in the last two weeks so I'll take the liberty of closing this out. Thanks anyway for the bug report.

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

    confirmed-bugIssues and PRs for confirmed bugs.fsIssues and PRs related to file-system APIs and the fs module.wontfixIssues that will not be fixed.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions