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

Avoid emitting 'change' when file is saved without any change #1283

Closed
jsines opened this issue Jul 11, 2023 · 1 comment
Closed

Avoid emitting 'change' when file is saved without any change #1283

jsines opened this issue Jul 11, 2023 · 1 comment

Comments

@jsines
Copy link

jsines commented Jul 11, 2023

Describe the bug

When watching a directory for changes an event is emitted when a watched file is saved, even if there were no changes.

This was reported back in 2018 but was closed: #673

This would be really helpful to fix. It's possible to work around the issue (I intend to) but I think it makes more sense being an option or the default behavior in the library itself.

The reasoning behind the original closure:

Can’t suppress the event. That’s not how these signals work.

You could hold onto before and after stats to see if the size changed (which will be imperfect, but maybe good enough), or you could read and hold onto entire file contents to compare whether any actual changes happened.

You could massively reduce the memory use of this approach by keeping a hash of the files instead. Add them all to a map when they're initialized and only emit the event if the new hash is different.

Versions (please complete the following information):

  • Chokidar version: 3.5.3
  • Node version 19.4.0
  • OS version: Ventura 13.4.1

To Reproduce:

const chokidar = require('chokidar');
const watcher = chokidar.watch('.');

watcher.on('change'), () => console.log('changed!'));
// 'changed!' is logged even if the saved file was not changed
@paulmillr
Copy link
Owner

You could massively reduce the memory use of this approach by keeping a hash of the files instead. Add them all to a map when they're initialized and only emit the event if the new hash is different.

Calculating hash can easily take a few seconds for a large file. If you have 300,000 files, calculating hashes will continue endlessly.

We won't implement it. If you need it, you can do this by yourself, by writing a thin wrapper on top of chokidar.

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

No branches or pull requests

2 participants