chokidar
chokidar copied to clipboard
Can't watch for file creation inside of non-yet-existing directory
Describe the bug
Watching ./foo/bar.txt
when ./foo/
does not yet exists won't trigger any event when ./foo/bar.txt
is finally created.
Having ./foo/
existing when calling chokidar.watch
or watching ./*/bar.txt
works correctly, though.
Versions (please complete the following information):
- Chokidar version: 3.0.2
- Node version: v12.6.0
- OS version: Ubuntu 18.04.2 LTS
To Reproduce
const chokidar = require('chokidar');
const fs = require('fs');
// Make sure no ./foo directory currently exists
chokidar.watch('foo/test*', {}).on('all', () => {
console.log('This will never be called');
});
chokidar.watch('*/test*', {}).on('all', () => {
console.log('This will be called');
});
fs.mkdirSync('foo');
fs.writeFileSync('foo/test.txt', 'testing 1');
// Both events should fire, but only the second watcher is triggered
From reading the tests it seems to be expected behavior (the tests explicitly create directories before watching files), but I can't find any documentation on the subject nor any way to bypass it.
Expected behavior I would expect events to be fired when files are created inside of non-yet-existing directories, no matter how I define my pattern (with a glob, or a static string).
Additional context
I'm currently using a hack to force chokidar to treat my static string as a glob by using (foo|$*)/test*
instead of foo/test*
. $*
will never match anything so both foo
and (foo|$*)
should match the same things, but it seems to somehow trigger chokidar into the right behavior.
FWIW, I tested on chokidar 3.2.0 and still having the same issue
Have you tried my pull request?
@pixelastic
This isn't a problem in filespy, an alternative to chokidar.