chokidar icon indicating copy to clipboard operation
chokidar copied to clipboard

ready event does not fire if directory contains symlink and persistent is false

Open jichu4n opened this issue 5 years ago • 2 comments
trafficstars

Describe the bug

When running in Node 10.x or 12.x and created with persistent: false, chokidar does not fire the ready event if the watched directory contains a symlink. The ready event is fired correctly when running in Node 8.x.

Versions (please complete the following information):

  • Chokidar version 3.3.0
  • Node version 10.17.0, 12.13.1
  • OS version: macOS Mojave 10.14.6

To Reproduce:

Simple script (index.js) that should print out a directory's contents and then print out "READY!!":

#!/usr/bin/env node
const chokidar = require('chokidar');
const watchedDirPath = process.argv[2] || '/tmp';
const chokidarInstance = chokidar.watch(watchedDirPath, {
  persistent: false,
  cwd: watchedDirPath,
});
chokidarInstance.on('add', filePath => console.log(filePath));
chokidarInstance.on('ready', () => console.log('READY!!'));
chokidarInstance.on('error', console.error);

Let's create a directory d1 containing a text file, and a directory d2 containing a symlink:

$ mkdir d1 d2
$ cd d1 && echo hello > hello.txt && cd ..
$ cd d2 && ln -s ../d1/hello.txt && cd ..
$ ls -l d1 d2
d1:
total 4
-rw-r--r-- 1 chuan staff 6 Dec 12 15:11 hello.txt

d2:
total 0
lrwxr-xr-x 1 chuan staff 15 Dec 12 15:12 hello.txt -> ../d1/hello.txt

The script works as expected on d1:

$ ./index.js $PWD/d1
hello.txt
READY!!

The ready event is NOT fired on d2:

$ ./index.js $PWD/d2
hello.txt

However, if we change persistent: false to persistent: true in the script, then the ready event is fired for both directories:

$ ./index.js $PWD/d1
hello.txt
READY!!
^C
$ ./index.js $PWD/d2
hello.txt
READY!!
^C

We have been able to repro this issue in both Node 10.17.0 and 12.13.1. However, in Node 8.x, the ready event DOES fire correctly for d2 even with persistent: false.

Additional context

As a workaround, we currently set persistent: true when watching, but then call chokidarInstance.close() in the ready event handler to prevent blocking the Node process from exiting.

jichu4n avatar Dec 12 '19 23:12 jichu4n

Chokidar should not be used to list files. Use readdirp.

paulmillr avatar Dec 13 '19 07:12 paulmillr

@paulmillr Thanks for looking (and for this awesome library)!

Yes, I agree with your point re use case - the script above is just an artificial example to repro the issue.

jichu4n avatar Dec 13 '19 17:12 jichu4n