chokidar icon indicating copy to clipboard operation
chokidar copied to clipboard

Can't watch for file creation inside of non-yet-existing directory

Open pixelastic opened this issue 5 years ago • 3 comments

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.

pixelastic avatar Jul 30 '19 12:07 pixelastic

FWIW, I tested on chokidar 3.2.0 and still having the same issue

pixelastic avatar Oct 10 '19 15:10 pixelastic

Have you tried my pull request?

@pixelastic

exx8 avatar Oct 10 '19 16:10 exx8

This isn't a problem in filespy, an alternative to chokidar.

aleclarson avatar Mar 16 '21 20:03 aleclarson