--watch and run step?

I’m playing around with Zig, wrote a little echo server. I’d like to be able to have it automatically rebuild&rerun.

With zig build --watch it recompiles on change but is there a way to automatically run here too?

Or is there a way to make zig build run --watch work, right now it just starts the server.

Welcome to the forums. Hope you enjoy your stay here in the land of Zig. :slight_smile:

--watch needs the watched build step to finish.

The run step doesn’t finish before your program exits, so it never gets there. If you interrupt the process to close the server (ctrl-c), then the zig process gets killed as well, so --watch doesn’t trigger.

That is to say, your server executable needs to cooperate and exit cleanly when you update the source code.

Maybe write a small shim that launches the server in a thread and then looks for filesystem changes in the source files with (your OS’s equivalent of) inotifywait?

A similar question as this here:

Its not possible yet, once a run command runs, the compiler cant stop it. But zig build will get an api in the future that can restart a rebuilt running process

4 Likes

It’s definitely nice to make your own tools, but just for convenience I’ll share what I’ve been using:

Initially entr for Linux:

echo "zig-out/bin/foo" | entr -r ./zig-out/bin/foo

Now usually watchexec for Linux, Windows and Mac:

watchexec -w zig-out --no-vcs-ignore -r -- ./zig-out/bin/foo

EDIT: Just for completeness, both tools were mentioned in a previous thread. The difference is that now we can have the build system do the rebuilds on its own, potentially with -fincremental, and now we only watch the outputs with external tool.

2 Likes