paulmillr / chokidar

Minimal and efficient cross-platform file watching library
https://paulmillr.com
MIT License
10.99k stars 582 forks source link

Folder rename is not reported when using a pattern #1285

Closed Janpot closed 4 months ago

Janpot commented 1 year ago

Describe the bug

When a folder is renamed and the original and new folder is part of a path that is covered by a glob that chokidar is watching, no events are fired.

Versions:

To Reproduce:

  1. create a file ./hello/foo/file.txt
  2. run

    const path = require('path');
    const chokidar = require('chokidar');
    
    chokidar.watch(path.resolve(__dirname, './hello/*/file.txt')).on('all', (event, filePath) => {
      console.log(event, filePath);
    });
  3. run mv ./hello/foo ./hello/bar
  4. no events are being fired

Expected behavior

an unlink and add event is fired for the rename. (This does happen when using usePolling)

AlynxZhou commented 7 months ago

I also reproduced this problem with @Janpot 's example but a little bit differently, I could get add event when moving a dir, but no unlink event.

denysdovhan commented 6 months ago

We experience a very similar problem, but with folder deletion. We use chokidar like this:

chokidar.watch([
  'src/foo/**/file.json',
  'src/bar/**/file.json'
], {
  useFsEvents: false, // <-- THIS SOLVES THE PROBLEM
  ignoreInitial: true,
}).on('all', (eventName, path) => console.log({eventName, path}))

When we delete foo or bar folders, neither unlink nor unlinkDir are emitted. The reason for that seems to be this line:

https://github.com/paulmillr/chokidar/blob/7c50e25d10a497ce4409f6e52eb630f0d7647b97/lib/fsevents-handler.js#L316

When a file patter contains a glob, it gets returned, so update is not reported.

The solution for us is to set useFsEvents: false. It also seems to be used widely by other various libraries.