Build step cached even though file changed?

I ran into an issue with my current build setup that took me a while to realize. I have a Rust project that builds an FFI library. I’m using the build system to build the Rust project with Cargo via addSystemCommand, and also build example projects (I have C and Zig examples).

Long story short (I lost almost 2 whole days to this), I have to run zig build twice – the first time to build the Rust project, and the second time to actually copy the Rust artifact to the install dir (zig-out). For some reason, the first zig build caches the addInstallLibFile step, but the subsequent zig build misses the cache and installs the file.

I have a minimum working example here (note: linux only): https://github.com/weskoerber/zig-build-repro

How can I prevent the caching of the file?

1 Like

You need to make the installLibFile step depend on running cargo first. Without defining that dependency Zig will rightfully run both steps in parallel.

Something along these lines :

pub fn build(b: *std.Build) void {
    const run_cargo = b.addSystemCommand(&.{ "cargo", "build" });
    const install_cargo_outputs = b.addInstallLibFile(b.path("target/debug/libmylib.a"), "libmylib.a");

    install_cargo_outputs.step.dependOn(&run_cargo.step);
    b.getInstallStep().dependOn(&install_cargo_outputs.step);
}

4 Likes

Thank you! That worked as expected.