chokidar
chokidar copied to clipboard
Cannot watch specific files inside ignored path
Describe the bug
If you exclude a folder with ignored: ['**/node_modules/**'], it appears to be impossible to get chokidar to watch a specific subdirectory underneath this.
If you use ignored: ['**/node_modules/**', '!**/node_modules/foo/**'] then node_modules/foo remains ignored. It appears chokidar sees the first exclude pattern and doesn't even bother looking inside node_modules after that.
If you undo the negation and try to exclude it with a negative lookahead regex (ugh) then it seems to take the negated glob to include everything and doesn't bother with the regex: ignored: ['**/node_modules/**', '!**/node_modules/**', /node_modules\/(?!foo\/)/].
Versions:
- Chokidar version: 2.1.8 and 3.5.3
- Node version: 16.15.1
- OS version: Linux Mint 20.3
To Reproduce:
Example program (just npm i chokidar and run this):
const chokidar = require('chokidar');
const fs = require('fs');
fs.mkdirSync('node_modules/foo', { recursive: true });
chokidar.watch(process.cwd(), {
ignored: [
'**/node_modules/**',
'!**/node_modules/foo/**',
],
}).on('all', (event, path) => {
console.log(event, path);
});
setTimeout(() => {
fs.writeFile('node_modules/foo/test.txt', 'hello', () => {});
}, 500);
// Should output 'change node_modules/foo/test.txt'
// Should not output add/change for other modules like 'node_modules/chokidar'
Expected behavior
There should be an easy way to exclude a folder like node_modules but then override this to include specific subdirectories within it, without having to use mad things like negative lookahead regexes or individually exclude every other node_module.
Additional context
ViteJS adds ignored: ['**/node_modules/**'] by default to exclude files in node_modules and provides a server.watch.ignored option that allows appending extra entries to the ignored list. Vite's own documentation has an example that adds '!**/node_modules/your-package-name/**' to watch particular packages, but this does not work because of the above issue.
Most build tools (babel, webpack, vite, etc) get this completely wrong in their own include/exclude rules. Most build tools also use chokidar for watching. I think the chokidar documentation needs a section that clearly explains what build tools should be doing with their default config and user options so that node_modules can be excluded by default but easily and sanely overridden to include specific subfolders.