Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignoreInitial option does not affect symlinks encountered during the initial scan #1270

Open
inga-lovinde opened this issue Feb 9, 2023 · 0 comments

Comments

@inga-lovinde
Copy link

inga-lovinde commented Feb 9, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant