remy/nodemon — pull request #102 by bryankaplan (2012-06-05 10:54:52+00:00)
Title: Reduction to open file counts

Nodemon was sometimes failing to notice when files changed. After some debugging, I discovered that it was unnecessarily trying to open a vast number of files that exceeded the ulimit maximum. Specifically, the following issues have been fixed:

1. `fs.watch` was previously watching files matching .nodemonignore regexes. Listing a file in .nodemonignore only prevented nodemon from restarting, but not from opening the file via `fs.watch`. This created a potentially enormous number of unnecessarily open files from directories like .git and node_modules. This commit fixes that behavior by filtering out the ignored files before handing them to fs.watch().

2. `fs.watch` was previously opening each file each time nodemon restarted, without closing the previously open files. That quickly added up to an enormous number of open files. This commit fixes that behavior by keeping an array of `watched` files, and only requesting `fs.watch` for files not already in that array.

3. The above two conditions went unnoticed because nodemon silently did `catch` EMFILE errors. A new `console.error` message has been added to identify when too many files are open for watching. After the first two fixes, such situations should be rare, but in larger projects it's easy to trigger by omitting */.git/* and */node_modules/* from .nodemonignore. Prior to the above two fixes, I was seeing hundreds of caught EMFILE errors each time nodemon restarted.