paulmillr / chokidar

Minimal and efficient cross-platform file watching library

Home Page:https://paulmillr.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ignoreInitial option does not affect symlinks encountered during the initial scan

inga-lovinde opened this issue · comments

commented

Describe the bug

EV_ADD (add) event is emitted for symlinks encountered inside watched directories during the initial scan, regardless of ignoreInitial option.

Versions (please complete the following information):

  • Chokidar version 3.5.3
  • Node version 18.12.1
  • OS version: Alpine Edge.

To Reproduce:

const chokidar = require('chokidar');
const fs = require('fs/promises');

const main = async () => {
    await fs.rm('./chokidar-sample', { recursive: true, force: true });
    await fs.mkdir('./chokidar-sample');
    await fs.mkdir('./chokidar-sample/watched');
    await fs.mkdir('./chokidar-sample/external');
    await fs.mkdir('./chokidar-sample/watched/internal');
    await fs.symlink('../../external', './chokidar-sample/watched/internal/external');

    const watcher = chokidar.watch('./chokidar-sample/watched', {
        followSymlinks: false,
        ignoreInitial: true,
        persistent: true
    });

    watcher
        .on('add', path => console.log(`File ${path} has been added`))
        .on('ready', () => console.log('Initial scan complete. Ready for changes'))
}

main();

Expected behavior
Expected output:

Initial scan complete. Ready for changes

Actual output:

File chokidar-sample/watched/internal/external has been added
Initial scan complete. Ready for changes

Additional context
chokidar only checks ignoreInitial flag in _handleFile method (in nodefs-handler.js).
This event was emitted by _handleSymlink method here.
Adding initialAdd parameter to _handleSymlink, and using similar code to the one in _handleFile to emit EV_ADD solves this specific issue (but might introduce some more bugs).

I've encountered this bug while I was debugging weird pm2 behavior: one of our apps was always restarting immediately after being started. Turns out it was caused by one of the watched directories containing symlinks.
pm2 starts to listen to all events immediately after creating a chokidar instance, not waiting for ready event, apparently to improve startup performance (so that the app can be launched without waiting for chokidar to scan all watched directories). So it gets that add event from chokidar during chokidar's initial scan, and triggers the reload of the app (thinking that something have changed).