Consider the following build script stub :
pub fn build(b: *std.Build) void {
const opt_fetch = b.option(
bool,
"fetch",
"this should prevent any fetch of the package",
) orelse false;
if (opt_fetch) {
if (b.lazyDependency("sdl", .{})) |sd| {
const step_install_sdl_tree = b.addInstallDirectory(
.{
.source_dir = sd.artifact("SDL2").getEmittedIncludeTree(),
.install_dir = .prefix,
.install_subdir = "sdl2_tree",
},
);
_ = step_install_sdl_tree; // autofix
}
}
}
SDL dependency is not really relevant, just happens to trigger an error when fetched on zig 0.17 because it uses b.sysroot that was removed.
build.zig.zon file I'm testing it with
.{ .name = .lazy_fetch_behavior, .version = "0.0.1", .minimum_zig_version = "0.17.0-dev.813+2153f8143", .paths = .{""}, .dependencies = .{ .sdl = .{ .url = "git+https://github.com/david-vanderson/SDL#b25735a465f7db1c160d2eca8ac46dde9ec41cfb", .hash = "SDL-2.32.10-JToi37G3EgG0eraPos4vhCwC6vVgyB1FYf1SxxhpBr_Y", .lazy = true, }, }, .fingerprint = 0x6fabe45dbfdfd5be, }If I run a bare zig build nothing happens after the configuration phase as expected.
I’m not entirely sure why the opt_fetch guard is even required, my naive assumption would be that the fetch only occurs if a step actually depends on the lazyDependency but maybe there is a good technical reason for that.
Then let’s say you try the dependency (i.e. zig build -Dfetch=true), it download, recompress, and in that particular case it triggers a build error related to b.sysroot missing.
Ok, so, you go back to zig build -Dfetch=false and the error is still there.
After being confused for a while, I realize it was already fetched, so I rm -r zig-pkg but the error persists.
It took me a while to realize that I had to rm -r ~/.cache/zig/p/SDL-... to “revert” to a state where I don’t trigger the error.
I very much like the design architecture with zig-pkg + recompressed global cache, but I have to say that the semantics of when which is used is not super transparent.
And everything is so fast that it’s tricky to follow
(Is it a download ? Is it a cache hit ? Don’t know, don’t care, it’s just zig and it’s fast. ahahahah. Ah, no more disc space third time today ? Right, I have 500 .zig-cache folders in my trash bin again.
I’m having a lot of fun.)
Anyways, shouldn’t the analysis of the package be skipped in that case regardless of the presence/absence of package in cache ? Am I missing something ?