202 points · by zdw · 6y ago · 81 comments
For example, in the example on the blog post, `git ls-files` will almost certainly ignore autogenerated build files, but it's possible for one of those files to change without the output of `git ls-files` changing. Similarly for things like third party packages that are installed system-wide.
With an LD_PRELOAD, all you'd need to do is
my-watcher ruby foo.rb
and the watcher would figure out which other Ruby files were opened, be them git versioned Ruby files in the current folder, or Ruby gems / Ruby VM dependencies in $HOME/.rbenv/, or system packages in /usr/local, or config files in /etc, ...I guess I wouldn't actually be surprised to hear if someone has built this already.
By hooking the filesystem calls, you can make a list of all files that a given process touches. When that process finishes, serialize a dependency file containing the hashes, timestamps, and sizes of all those files. Next time you run that same command line, read the dependency file from the last run and compare to the current filesystem state. If it's the same, and you know your command is idempotent, you can skip execution entirely.
Now, if you put that logic in a dll, you can inject it into arbitrary third-party processes to which you don't have source code and it will still work. Name the dependency file after the hash of the command line.
You can run an audited build command under a special ClearCase wrapper and it will look at the versions of the elements used in the build - if that build has already occurred with the same input elements before, even if in a different view, it can "wink in" the derived object result - that is, it can cause that previous build to be visible in your current view. This can save a lot of time when you're building a large codebase.
0 - https://www.freebsd.org/cgi/man.cgi?query=kqueue&apropos=0&s...
In my experience with C/++, it is faster to combine Make & ccache: just have every C file depend on every header file, and let ccache decide if it needs to be rebuilt.
I suggest you don't wait until it's too complex to fix the mistake and do proper dependency tracking since the very beginning. It's not that hard in C/C++.
I know that's not large, but I've got one Makefile that's only about 200 lines long. It's a pretty good trade-off.
I'm rather reusing existing stuff but seeing how every scriptable filesystem watcher misses the point, I'm inclined to write my own inotifywait/inotifywatch wrapper.
mkdir /tmp/project
cd /tmp/project
touch file{1,2}
ls | entr -d echo "a change"
Terminal 2: rm file2
Terminal 1: entr: cannot open 'file2': No child processes
Regarding `-d`:> Track the directories of regular files provided as input and exit if a new file is added. This option also enables directories to be specified explicitly. Files with names beginning with ‘.’ are ignored.
So first, it doesn't track NEW directories, and second, it exits if new file is added. Exactly how is this useful?
EDIT: All I want from filesystem watcher is to track files by pattern and just re-run the command, however complex that may be to implement via OS interfaces. When I'm working on a Python project that has packages (directories), I may also refactor (rename files), and this simplistic behavior does not catch that.
$ while true; do
> ls -d src/*.py | entr -d ./setup.py
> done
that way when a file/dir is added/removed it restartsI've had good luck with modd: https://github.com/cortesi/modd/
I just tested exactly what you described, did not see any errors, and it ran my commands exactly the way I wanted.
I assume I'm misunderstanding something, so this is probably a naive question and maybe you can explain further, but if that is all you need why not just write it yourself? It sounds like an <1 hour project to write something cross-platform in Qt that monitors the filesystem recursively with QFileSystemWatcher https://doc.qt.io/qt-5/qfilesystemwatcher.html checks for the configured regexen and runs the configured command.
A more technical description is mentioned here: https://github.com/fsnotify/fsnotify/issues/18
inotifywait -rq -m -e close_write --format %f . | grep '\.rules$'
Sure, there may races due to limitations of underlying OS interfaces, but no such races really matter when you edit with mammal speed, so it's perfectly sufficient, and so it is perfectly possible to make good file system watcher, without any user-space power-hungry hacks.In fact it's man page comes with a big fat warning about using -r:
> Warning: If you use this option while watching the root directory of a large tree, it may take quite a while until all inotify watches are established, and events will not be received in this time. Also, since one inotify watch will be established per subdirectory, it is possible that the maximum amount of inotify watches per user will be reached. The default maximum is 8192; it can be increased by writing to /proc/sys/fs/inotify/max_user_watches.
(ref: https://linux.die.net/man/1/inotifywait)
Really what would be nice would be for Linux to support this natively so we don't have to resort to dirty hacks.
`while inotifywait -q -q -r -e modify -e move -e create -e delete --exclude '[*~#]' $(mapi $wd $@); do`
wd is the working directory.and mapi
my $prefix=shift;
foreach my $s (@ARGV) {
print "$prefix/$s ";
}
Adjust to taste.it is _hax_, as it were. but it has worked well for me.
[1] - http://gittup.org/tup/
I've heard good things about watchman because it will make a best effort to let filesystem changes "settle" before running the command specified. If there's a comparison of these two written up or if someone can give their testimonial I'd love to hear it.
watchexec was borne out of a few frustrations with entr, mostly around how it handles new files being created. However, from a pure design standpoint, entr is just better than most anything out there due how closely it hews to UNIX philosophy, and it gets so far on just that.
The only real improvement that can be made to these tools (currently) would be to have perfect information about what caused a file to change. Currently, most tools require you to tell it files/patterns to ignore to avoid triggering loops where the file watcher changes files and ends up triggering itself over and over. watchexec did good work here by ingesting your .gitignore and using that.
Unfortunately OSes don't provide great info when it comes to file modifications. On Linux, ptrace/LD_PRELOAD would enable us to know the set of all files changed as a result of running the file watcher (and thus ignoring them automatically). DYLD_INSERT_LIBRARIES is a thing on macOS, though it is subject to SIP restrictions with some binaries. I'm unsure what mechanism exists on Windows. The highly platform-dependent nature of this is one reason why I haven't really pursued this line of work in watchexec.
I used atchange for a long time:
http://jeffreycopeland.com/work/PDF/1996-03-atchange.pdf http://users.fred.net/tds/lab/atchange.html
And now, out of habit, my own wrapper around inotify. But more polished solutions like entr do seem the place to start now.
https://smalldata.tech/blog/2019/03/15/hot-reload-go-applica...
Tip: Emulate vscode Live preview of Markdown with entr:
ls file.md | entr script.sh
script.sh #!/bin/sh
pandoc file.md -o file.pdf
mupdf file.pdfWhen developing I use make to invoke my program; that ensures everything is rebuilt.
$ while sleep 2; do make -s build; done
What files are interesting is already encoded in your Makefile and make is pretty good at figuring out if a given file changed or not so, instead of duplicating efforts, run make every second or two and let it figure out what needs to be done.It won't eat your memory or pool of file pointers and your CPU will barely feel it.
If you mean “invoke as soon as a file has changed”: I certainly don’t want that behavior as many code changes require multiple files to be edited and there’s no automatic way to understand as the change hasn’t been written yet.
[1] - https://github.com/guard/guard [2] - https://github.com/guard/listen
> The inotify cron daemon (incrond) is a daemon which monitors filesystem events and executes commands defined in system and user tables. It's use is generally similar to cron(8).
But until then, no I don't know any.
I love the way she straces the tool to find out how it works - a true hacker at work :-)
Interesting thing to look at and see if it maybe suits you better.
inotifywait --event modify --recursive . --quiet
If anyone wants the full script I can post it here. One thing many of these tools miss is that I typically want the build command run once at the start.When developing on VM, sometimes you have to "forward" filesystem events to the guest. Usually that happens via TCP/UDP and touch, but wont trigger the reload.
Since Evince reloads file if it changes on disk, that meant I had immediate feedback.
One downside is that ptrace is not recursive/reentrant so some tools won't work.
Otherwise, it doesn't seem especially unusual or unique to me, and yet the author is presenting it as a revolution.
Am I missing something?
Also, the docs are explaining the problems with inotify: http://eradman.com/entrproject/