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

chokidar fires "add" and "addDir" events on first watching a directory but not on re-watching a directory. #1185

Closed
george-thomas-hill opened this issue Dec 24, 2021 · 2 comments

Comments

@george-thomas-hill
Copy link

Describe the bug

When chokidar first watches a directory, it very helpfully emits "add" and "addDir" events for each file and folder in that directory.

However, if you unwatch that directory and then re-watch it, chokidar does not emit the expected "add" and "addDir" events for the files and folders in that directory.

Versions (please complete the following information):

  • Chokidar version 3.5.2
  • Node version 16.11.0
  • OS version: Kubuntu 21.04

To Reproduce:

index.js:

let chokidar = require('chokidar');
const directory = "/home/georgehill/Dropbox/test1";
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!");
}

Simulated console output:

$ ls /home/georgehill/Dropbox/test1/
 folder1  'New Folder'      'New Folder (2)'  'New Folder (4)'     'Text File.txt'
 folder2  'New Folder (1)'  'New Folder (3)'  'Text File (1).txt'

$ node index.js 
addDir: /home/georgehill/Dropbox/test1
add: /home/georgehill/Dropbox/test1/Text File (1).txt
add: /home/georgehill/Dropbox/test1/Text File.txt
addDir: /home/georgehill/Dropbox/test1/New Folder
addDir: /home/georgehill/Dropbox/test1/New Folder (1)
addDir: /home/georgehill/Dropbox/test1/New Folder (2)
addDir: /home/georgehill/Dropbox/test1/New Folder (3)
addDir: /home/georgehill/Dropbox/test1/New Folder (4)
addDir: /home/georgehill/Dropbox/test1/folder1
addDir: /home/georgehill/Dropbox/test1/folder2
Ready!
Unwatched!
Re-watched!
addDir: /home/georgehill/Dropbox/test1
^C

Expected behavior

I expected that, upon re-watching the directory, chokidar would emit an "add" or an "addDir" event for each file/folder in the directory.

Instead, chokidar only emitted a single "addDir" event corresponding to the directory being rewatched.

@gavr123456789
Copy link

gavr123456789 commented Feb 8, 2022

Peek.2022-02-08.03-46.mp4

I ran into the same problem when I tried to write a file browser. The re-watch of the directory does not trigger the addition events that I rely on when adding items to the list.
Linux: arch
chokidar: @3.5.3
node: v17.3.0

PS duplicates of the folder that was unwatched are also added

@KrazyTako
Copy link

I am also seeing this issue. My work around at the moment is recreating the entire file system watcher in order to properly trigger the initial watch event. I have a similar UI to @gavr123456789 where the user is able to remove/add directories to watch. If they remove an existing directory and then re-add it, it will not trigger the initial file add events.

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

No branches or pull requests

4 participants