Node: fs.watch() consistently claims a file in a similarly named directory also changed

Created on 31 Dec 2018  ·  5Comments  ·  Source: nodejs/node

  • Version: Latest versions of Current and LTS releases: v11.6.0, v10.15.0, v8.15.0, v6.16.0.
  • Platform: macOS
  • Subsystem: fs

Description

When using fs.watch() to watch several directories, and changing a file with either fs.writeFileSync or fs.appendFileSync, an unrelated FSWatcher for a similarly named directory is getting an event.

Test case

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

Build output at https://travis-ci.com/Krinkle/sandbox/jobs/167573963.

In brief:

  1. The file system looks as follows:
    - home/
          - a/
             - file1
          - b/
             - file1
             - file2
          - bb/
             - file1
  1. Using fs.watch( .., { encoding: 'utf8', recursive: false }) an FSWatcher is created for home/a, home/b and home/bb.

  2. A modification is made to home/bb/file1.

Expected

An event relating to home/bb/file1 (file) and/or home/bb (directory).

Actual

Two events:

  • change home/b/file1
  • change home/bb/file1
... modifying home/bb/file1
home/b [change: UNEXPECTED] rename /file1
home/bb [change: expected] rename file1

It is consistently happening in this way on both Travis CI and for myself locally. It has confused me for a quite a while, and I am at a loss as to why this is happening. It seems to specifically result in an event for the same name in a similarly named directory.

It never fires for a or for file2. Rather, bb/file1 always misfires for b/file1.

fs libuv macos

Most helpful comment

I'm > 50% confident that https://github.com/libuv/libuv/pull/2082 fixes this.

All 5 comments

Any chance you've tried running this on Linux or Windows or anything else on Travis CI or elsewhere? I'm curious if it's macOS-only or not.

Yep, I've got 11 sibling jobs on the same Travis build, for other environments 😃

https://travis-ci.com/Krinkle/sandbox/builds/95971463

os:
 - linux
 - osx
 - windows
node_js:
 - 11
 - 10
 - 8
 - 6

The behaviour is the same across Node.js versions.

Linux:

  • event for home/bb/file1

Windows:

  • event for home/bb
  • event for home/bb/file1

macOS:

  • event for home/b/file1 ❓
  • event for home/bb/file1

It appears specific to macOS, and not specific to Travis CI; as I'm seeing the same problem on my workstation (MacBook, Node.js 10 from Homebrew, nonvirtualised).

@nodejs/fs @nodejs/platform-macos

I'm > 50% confident that https://github.com/libuv/libuv/pull/2082 fixes this.

I'm closing this as fixed in libuv 1.25.0. Please reopen if it's not the case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dfahlander picture dfahlander  ·  3Comments

addaleax picture addaleax  ·  3Comments

ksushilmaurya picture ksushilmaurya  ·  3Comments

srl295 picture srl295  ·  3Comments

fanjunzhi picture fanjunzhi  ·  3Comments