Mutating generated files in later steps

The gist of what I’m trying to do is: I’m making an ISO file by calling an external command, and then I’m trying to modify that ISO file by calling another command that isn’t actually external but for the purposes of this question we can pretend that it is.

Here’s how I’ve implemented this in my build.zig:

const iso_staging_dir = b.addWriteFiles();
// ...set up the ISO filesystem hierarchy...

const build_iso_cmd = b.addSystemCommand(&.{ "xorriso", "-as", "mkisofs" });
// ...what feels like twenty thousand calls to `build_iso_cmd.addArgs`...
build_iso_cmd.addArg("-output");
const iso_path = build_iso_cmd.addOutputFileArg("aurora.iso");
build_iso_cmd.addDirectoryArg(iso_staging_dir.getDirectory());

const write_boot_sector = b.addRunArtifact(limine_exe); // pretend this is `addSystemCommand`
write_boot_sector.addArg("bios-install");
write_boot_sector.addFileArg(iso_path);

(limine_exe is a C program I built from a package dependency; it only supports mutating files in-place.)

Now I try to run QEMU passing it my ISO file. By my understanding, all I should have to do is call addFileArg and give it the LazyPath to my ISO (which I bound to iso_path above):

const qemu_cmd = b.addSystemCommand(&.{"qemu-system-x86_64"});
// ...irrelevant arguments...
qemu_cmd.addArg("-cdrom");
qemu_cmd.addFileArg(iso_path);

But this doesn’t quite work. When I run a clean build (i.e. after deleting zig-cache), the build runner skips my write_boot_sector step and only does build_iso_cmd (and qemu_cmd). If I want it to work properly, I have to explicitly make qemu_cmd depend on write_boot_sector:

// FIXME: This is definitely necessary and I'm not entirely sure why.
qemu_cmd.step.dependOn(&write_boot_sector.step);

After adding this line everything seems to work fine (or at any rate I haven’t figured out how to break it yet), but it just feels wrong to me.

I initially thought that the LazyPath was keeping track of whatever it was passed to so that the build runner could order dependencies correctly, but after looking at the source that doesn’t appear to be the case; AFAICT the type can only hold at most one *Step at a time, presumably the one that first created the LazyPath instance. I did a bit more digging and found a few doc comments suggesting that what I’m doing here (mutating the file referenced by a LazyPath after the step that initally generated said file) is not a supported use case, or at any rate that I’m doing it wrong.

So I guess what I’m asking is:

  1. Am I understanding correctly how this is supposed to work?
  2. How can I mutate this generated file in a way that’s actually supported and won’t potentially break the build cache system or anything similar?

There is nothing stopping it from mutating an input path (addFileArg), the issue is the API doesn’t track mutating steps, which just means you have to add the step dependencies your self.

Alternatively, you could make a simple wrapper script that first copies an input path to an output path before running the actual utility, this way you get new lazy paths from each mutation.

Related issue: build system: support run steps that modify input files · Issue #24758 · ziglang/zig.

The current solution people use is things like limine-install and zig-limine-install.