paulmillr / chokidar

Minimal and efficient cross-platform file watching library
https://paulmillr.com
MIT License
11.04k stars 586 forks source link

Re-watching a path does not honour `ignoreInitial` #1386

Open TobyEalden opened 1 week ago

TobyEalden commented 1 week ago

Describe the bug This is a duplicate of https://github.com/paulmillr/chokidar/issues/1185, which has been closed, however the test case still fails.

With ignoreInitial set to false, when first adding a directory all the initial files are emitted. If the directory is un-watched and then watched again at some point in the future, the initial files are not emitted.

Versions (please complete the following information):

To Reproduce:

import chokidar from "chokidar";
const directory = ".";
const options = {
  disableGlobbing: true,
  depth: 0,
};
let watcher = chokidar.watch(directory, options);
watcher.on("all", handleAll);
function handleAll(event, path) {
  console.log(`${event}: ${path}`);
}
watcher.on("ready", handleReady);
async function handleReady() {
  console.log("Ready!");
  await watcher.unwatch(directory);
  console.log("Unwatched!");
  watcher.add(directory);
  console.log("Re-watched!");
}

Expected behavior Un-watching and then re-watching a directory should cause the initial files to be emitted if ignoreInitial is false.

43081j commented 1 week ago

Seems to make sense, assuming that is the intended use of "initial files".

If we define "initial" as any time you start watching a directory, we should fix this

Good to get the opinion of @paulmillr here since it's from before me

paulmillr commented 1 week ago

Idk how would we track these files

43081j commented 1 week ago

Presumably we currently read the directory and emit all the children. Then when we unwatch and watch it, we won't read through it a second time

Maybe it's as simple as always reading the directory when you watch one?

So we don't need to track anything extra for it. I might be missing something though in my understanding

TobyEalden commented 1 week ago

It looks like _handleRead already deals with the initial load, but it uses the previous directory entries to determine if it needs to emit or not

The pull request #1388 handles this by closing the descendants as well as the unwatched path in closePath, but I'm not sure of the function of (or interaction with) ignoredPaths...

43081j commented 1 week ago

feels like it makes sense to me. when you remove a watched path, clearing all of its children

we just need to decide if this is a breaking change or not and if its the behaviour we're all agreed on

as it means we will start emitting events which we didn't used to emit