gforceg / node-hound

Cross platform directory tree watcher that works, even on Windows
Other
96 stars 9 forks source link

Throws errors when filesystem changes during Hound.prototype.watch #5

Open cspotcode opened 11 years ago

cspotcode commented 11 years ago

If a file is created and then deleted before Hound.prototype.watch has a chance to execute, then Hound will attempt to stat a non-existent file. All other fs operations run by Hound can also fail under similar circumstances.

I was able to throw in some try-catches to fix the problem but a proper solution depends on what kind of guarantees you want to make about Hound's behavior.

If Hound must report all FS changes, however temporary, then you've got a problem. If a file is created and then deleted before Hound can stat the file, it'll have to fire a 'create' event without any stat data, followed by a 'delete' event.

It's easier if you only require Hound to fire enough events that an observer arrives at a valid view of the eventual steady-state of the filesystem. In other words, if a file is created and then immediately deleted, Hound doesn't need to fire any events. "oops, it was too fast." Of course, if a file is created and remains extant then Hound is required to fire a 'created' event. If the file is modified twice in quick succession, Hound can get away with firing only one 'change' event, provided that event fires after the second file change has completed. For my personal use-case (automatically rebuilding templates & recompiling code as I edit them) this is totally fine.

beefsack commented 11 years ago

Hi @cspotcode, sorry about the delay getting back to you, I'll look at this one this week.

huttj commented 9 years ago

Any updates on this?

I, too, managed to work around it with a couple of try/catch blocks--one around the body of the watch method, and one around the create event on hound.js:70.

I needed those because fs was throwing errors when WebStorm would very rapidly create and delete temporary files with ___jb_bak___ and ___jb_old___ suffixes, which it couldn't catch in time. I was using it to watch a directory.

cspotcode commented 9 years ago

IIRC, it was the exact same situation for me: WebStorm / IntelliJ quickly creating and deleting files.

@huttj For what it's worth, there are other file-watcher libraries you can consider using. Gaze is depended on by grunt-contrib-watch and many other grunt plugins so it's probably a good choice.