paulmillr / chokidar

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

change event triggers even if file didnt change. #1292

Closed DawitAskabe closed 9 months ago

DawitAskabe commented 9 months ago

Describe the bug Out of habit i press command+S (save) more than once when i save a file. Chokidar triggers build for each save regardless the file content changed or not. I expected only one Change event to be triggered - the first one.

File src/../index.ts has been changed. Rebuilding...
⏳ Building ...

File src/../index.ts has been changed. Rebuilding...
⏳ Building ...

File src/../index.ts has been changed. Rebuilding...
⏳ Building...

Versions (please complete the following information):

To Reproduce:

Steps to reproduce the behavior. Include filename and chokidar config.

 chokidar.watch('src/**/*.ts').on('change', (path) => {
        console.log(`File ${path} has been changed. Rebuilding...`);
        try {
          buildAndCopy(fileList);
        } catch (error) {
          console.log(`esbuild failed:`, JSON.stringify(error));
        }
      });

Expected behavior should run build only once

Additional context I am not sure if its the OS that is we writing the file regardless change or not. is there a way to delay the trigger of the change event?

peteygao commented 9 months ago

This is not a bug, chokidar is not content aware!

What you need is a debounce function if you have trigger happy fingers (Lodash provides _.debounce if you don't want to write your own).

How the chokidar works under the hood: Every time you press Cmd + S, the file is written to, and thus it is "changed" as far as the filesystem is concerned. chokidar listens for filesystem level change events that are emitted, not check the actual content of the file to see if it has "changed" in the literal sense.

DawitAskabe commented 9 months ago

yah i figured. I didnt think about using debounce. will give it a try.