We are watching for file changes using chokidar with nodejs on an Ubuntu server. It works pretty fine, but sometimes I think we have a problem with the way a file is saved when changed: in chokidar the "onchange" event is triggered, but when we read the file in the callback, it is empty or (rarely) not written completely. We don’t know if it is a problem caused by the text editor writing the file, the operating system on the client computer, the OS or filesystem (ext4) on the server or if it is some kind of bug in chokidar.
This empty file problem is now happening almost every time by file changes made from one computer (mac, latest osx version) where sublime 3 is used. In sublime we tried the setting “atomic_save” (creates a temp files and then overwrites the original file), but it did not solve our problem:
- With atomic_save set to “false” the file seems to be always empty in the onchange event and
- With atomic_save “true” it is sometimes empty and sometimes partly written.
We had this problem before with phpstorm, but after switching to ‘use “safe write” (save changes to a temporary file first)’ - so the same setting as atomic_save in sublime, that’s also why we tried atomic_save in sublime -, the onchange event was triggered correctly after the complete was written.
So our question is, is there a way to have the “onchange” triggered when the file is completely written somehow (in chokidar, in an OS, in sublime)? Or do we have to check the file size in the “onchange” event until hasn’t changed for some time (not so nice)? Or does our problem maybe occur because of something else?
We would appreciate any hints! Thanks in advance!