zig icon indicating copy to clipboard operation
zig copied to clipboard

Zig sometimes produces a bad entry in the global cache that breaks caching entirely for my project (across all clones)

Open IntegratedQuantum opened this issue 8 months ago • 6 comments

Zig Version

0.14.0 (you need this exact version, since zig does not seem to use cache files from non-matching versions)

Steps to Reproduce and Observed Behavior

(Note that this was reproduced on x86_64 linux, and it probably won't be possible to reproduce this on other architectures)

Download this zig global cache (reduced to one file) and extract it in ~.cache/, deleting the old cache: zig.zip.

You will also need to apply the following patch to the standard library: https://github.com/PixelGuys/Cubyz-std-lib/commit/b0a5e734760ccc948202b3976cb1481a564721ce

Run the following commands to clone and compile the project (here is also the exact commit, just in case 1b7102768fb1f7e2d0421cdf6ff6993b0a9480b6):

git clone https://github.com/PixelGuys/Cubyz
cd Cubyz/
zig build

Initial compilation takes a while, so far nothing out of the ordinary, but now if you compile it again, observe that it still takes 20 seconds:

$ time zig build

real	0m20,084s
user	0m18,181s
sys	0m2,102s

This happened to me (and others) multiple times in the past months and seemingly got more frequent after updating to 0.14.0. It's really frustrating to have to wait for the game to compile again if I only changed some shader code or some assets or just want to relaunch the game to test some more things.

Expected Behavior

To get the correct behavior just delete the caches and compile again:

rm -r ~/.cache/zig
rm -r .zig-cache
zig build

Without the bad entry the cache works correctly and all following attempts to compile are instantaneous:

$ time zig build

real	0m0,034s
user	0m0,010s
sys	0m0,028s

IntegratedQuantum avatar Mar 30 '25 11:03 IntegratedQuantum

Can confirm this happened to me multiple times in windows and linux

alichraghi avatar Mar 30 '25 11:03 alichraghi

Can you still reproduce this with 0.14.1?

alexrp avatar May 27 '25 12:05 alexrp

No, this particular example does not reproduce in 0.14.1 anymore, but I cannot reproduce it with a debug build of 0.15.0-dev.1+05937b362 either, so I think it's safe to assume that this is no longer reproducible because Zig ignores cache entries from older versions. Given how long these caching issues have been present in Zig, I doubt that it would just be silently fixed in a minor version anyways.

It does still reproduce consistently with both release and debug builds of 0.14.0.

Also I discovered that there is a crucial step missing in my reproduction: I have applied a small patch to the standard library (just some changes to the formatter). The patch is available here: https://github.com/PixelGuys/Cubyz-std-lib/commit/b0a5e734760ccc948202b3976cb1481a564721ce, you need it to be able to reproduce the issue because it seems that the relevant file render.zig, although not even used to compile the game, is still included in the broken cache file.

IntegratedQuantum avatar May 27 '25 16:05 IntegratedQuantum

I doubt that it would just be silently fixed in a minor version anyways.

@mlugg fixed some caching issues in 0.14.1.

alexrp avatar May 27 '25 17:05 alexrp

It sounds like this might be the same as an issue which sometimes affects me, where this "bad cache" somehow happens with the compiler_rt cache, leading literally every compilation to take 5-10 seconds building compiler_rt. I'll hopefully be able to take a look soon.

mlugg avatar May 27 '25 17:05 mlugg

mlugg fixed some caching issues in 0.14.1.

I assume you mean https://github.com/ziglang/zig/commit/3783b1b23c056b30923d6c9dbd2e44c499738198

I cherry-picked (no-commit) this into 0.14.0 and I cannot reproduce the issue with it.

However I'm not certain if this means the issue is actually fixed, or if it just means that this commit somehow changed the caching in other ways that makes it work accidentally with this particular reproducible.

IntegratedQuantum avatar May 27 '25 19:05 IntegratedQuantum