chokidar
chokidar copied to clipboard
`ready` event firing before some `add` events from the initial scan under certain circumstances
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.3andv10.20.1 - OS version:
macOS 10.15.4andDebian 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.
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
fsevents-handler is used only on macos
Huh. I can't account for this behavior on Linux, then.
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.
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.

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