`--watch -fincremental` spams the console

First of all I want to say that I really like the development experience with the --watch flag, thank you zig team.

But I have one minor issue with it when using in combination with the -fincremental flag that really bugs me: It sometimes just spams a lot into the console or flickers, particularly on syntax errors. Does someone else also have that problem?

Here are some steps to reproduce what I mean:

mkdir /tmp/spam && cd $_
zig init
zig build test --watch -fincremental

and then just remove the first character of the main.zig file:

tail -c +2 src/main.zig > temp && mv temp src/main.zig

If I leave the -fincremental flag out it just works as expected. Also if I restart the watch
command it also doesn’t unnecessarily updates repeatedly.

I know that I can use --error-style minimal_clear but it still flickers which annoys me. I’ve also
already tried different terminals but it’s always the same.

edit: version is 0.16 of course

1 Like

I can reproduce it, if I do the modification of the file like you have described, but if I just use a simple text editor to remove something from the file than it no longer happens.

So my guess is that it may have to do with your specific command, I think the difference here is that most editors will edit the file in place, while your command creates a modified copy and then replaces the original file.

So the difference may be that with your command the files file handle / inode becomes replaced with a new file and it seems that this replacement results in some kind of endless loop where the code that checks whether things have changed always comes to the conclusion that there are still new changes, but some other code looks at the old file and doesn’t find any changes or something like that. Some part of the code clearly looks at the new file because the error gets reported. I think this is a bug that somewhere something doesn’t get updated quite right, I think it would make sense to create an issue for this.

4 Likes

Oh dear, this is an odd bug. Sorry for the bad first experience! I’ll take a look now.

5 Likes

Try applying this patch to the file lib/std/Build/Step.zig from the standard library (the lib/ directory will be next to your zig binary if you’re using our standard tarballs):

diff --git a/lib/std/Build/Step.zig b/lib/std/Build/Step.zig
index 773706d39a..51c1514f85 100644
--- a/lib/std/Build/Step.zig
+++ b/lib/std/Build/Step.zig
@@ -981,6 +981,7 @@ pub fn reset(step: *Step, gpa: Allocator) void {
     step.result_peak_rss = 0;
     step.result_failed_command = null;
     step.test_results = .{};
+    step.clearWatchInputs();

     step.result_error_bundle.deinit(gpa);
     step.result_error_bundle = std.zig.ErrorBundle.empty;

This solves the problem for me. If you can confirm it works for you too I’ll PR it and it should get into 0.17.0, which is likely coming soon. (While you’re waiting for 0.17.0, you can just keep that patch applied locally, it shouldn’t cause any issues!)

8 Likes

For me on 0.16.0 with that change it no longer spams the console.

2 Likes

It was also with (n)vim and nano so I just used a command for easier reproduceability.

Yeah no problem. I also had it on 0.15 but guessed that it might be fixed by the upgrade.

I will take a look at the solution later when I’m able to.

Thank you

I’ve opened #35224 with the fix I gave above.

4 Likes