chokidar icon indicating copy to clipboard operation
chokidar copied to clipboard

`ready` event firing before some `add` events from the initial scan under certain circumstances

Open trevorhreed opened this issue 5 years ago • 5 comments

Describe the bug

The ready event is firing before all add events from the initial scan under certain circumstances.

Versions (please complete the following information):

  • Chokidar version v3.4.0
  • Node version v12.16.3 and v10.20.1
  • OS version: macOS 10.15.4 and Debian GNU/Linux 9 (stretch)

To Reproduce:

Repl.it: https://repl.it/@trevorhreed/chokidar-test-case Github Repo: https://github.com/trevorhreed/chokidar-test-case

Using the following code...

const chokidar = require('chokidar')

const globs = [
  'src/**/*.js',
  'src/does-not-exist'
]

chokidar
  .watch(globs)
  .on('ready', () => console.log('Ready.'))
  .on('add', path => console.log(path))

...and watching the following directory structure...

src/
  works.js
  foo/
    bar/
      baz/
        qux.js

...I get the following output...

Linux:

Ready.
src/works.js
src/foo/bar/baz/quz.js

MacOS:

src/works.js
Ready.
src/foo/bar/baz/quz.js

Expected behavior

Expected the ready event to occur after the initial add event, which should have produced the following output:

src/works.js
src/foo/bar/baz/quz.js
Ready.

trevorhreed avatar Jun 03 '20 23:06 trevorhreed

After some further testing and digging into the code, it appears that the code on line /lib/fsevents-handler.js:505 appears to be causing the issue, though it looks like it was thrown in for some other important reason.

https://github.com/paulmillr/chokidar/blob/3bc3eca8996c276e9e8fd529608d41d564a1a513/lib/fsevents-handler.js#L505

trevorhreed avatar Jun 05 '20 23:06 trevorhreed

fsevents-handler is used only on macos

paulmillr avatar Jun 06 '20 03:06 paulmillr

Huh. I can't account for this behavior on Linux, then.

trevorhreed avatar Jun 08 '20 18:06 trevorhreed

I am also running into this issue on Linux where ready is fired pretty much immediatelly before any files are reported to be watched. Also the getWatched() is empty in that moment.

danielkcz avatar Nov 02 '20 12:11 danielkcz

I tried to debug this a little. It's happening in here...

https://github.com/paulmillr/chokidar/blob/0d7287341c579a8b5f550e7c5350cd2f480db5ae/index.js#L445-L447

~~I am not sure how is the _addToNodeFs supposed to work, but it resolves sooner only considering paths and not resulting files.~~ Small debug output where I have 2 paths on input which results in 16 files.

image

It emits ready when input paths are reached and then the file discovery is happening asynchronously.

danielkcz avatar Nov 02 '20 13:11 danielkcz