authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-07-01 14:02:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-07-01 14:02:31-07:00
logca948d62d0a5762c2438da0a0a59034dc0a16cdb
tree4bf79510fe7cfa20c38c0bd436ce3809fc91ad4d
parenta30f029d53b5080351ce531b10535bb37776854e

std.Build: deprecate lazyDependency and dependency

in favor of dependencyLazy which has a different function signature and will be eventually renamed to dependency migrating to all dependencies being potentially lazy by setting the flag in build.zig.zon and embracing error.LazyDependencyNeeded propagation as the way to deal with discovered needed dependencies. solves the problem that handling the `null` case from `lazyDependency` was annoying.

1 files changed, 21 insertions(+), 11 deletions(-)

lib/std/Build.zig+21-11
...@@ -2109,21 +2109,29 @@ fn markNeededLazyDep(b: *Build, pkg_hash: []const u8) void {...@@ -2109,21 +2109,29 @@ fn markNeededLazyDep(b: *Build, pkg_hash: []const u8) void {
2109 b.graph.needed_lazy_dependencies.put(b.graph.arena, pkg_hash, {}) catch @panic("OOM");2109 b.graph.needed_lazy_dependencies.put(b.graph.arena, pkg_hash, {}) catch @panic("OOM");
2110}2110}
21112111
2112/// Deprecated in favor of `dependencyLazy`.
2113pub fn lazyDependency(b: *Build, name: []const u8, args: anytype) ?*Dependency {
2114 return dependencyLazy(b, name, args) catch |err| switch (err) {
2115 error.LazyDependencyNeeded => null,
2116 };
2117}
2118
2112/// When this function is called, it means that the current build does, in2119/// When this function is called, it means that the current build does, in
2113/// fact, require this dependency. If the dependency is already fetched, it is2120/// fact, require this dependency. If the dependency is already fetched, it is
2114/// returned. However if the dependency is not yet fetched, then when the build2121/// returned. However if the dependency is not yet fetched, then when the build
2115/// script is finished running, the build will not proceed to the make phase.2122/// script is finished running, the toolchain will not proceed to the make phase.
2116/// Instead, the parent process will additionally fetch all the lazy2123/// Instead, the parent process will additionally fetch all the lazy
2117/// dependencies that were actually required by running the build script,2124/// dependencies that were actually required by running the build script,
2118/// rebuild the build script, and then run it again. In other words, if this2125/// recompile the build script, and then run it again. In other words, if this
2119/// function returns `null` it means that the only purpose of completing the2126/// function returns `error.LazyDependencyNeeded` it means that the only
2120/// configure phase is to find out all the other lazy dependencies that are2127/// purpose of completing the configure phase is to find out all the other lazy
2121/// also required.2128/// dependencies that are also required. In this case, one must propagate the
2129/// error all the way up and return it from the main build function.
2122///2130///
2123/// It is allowed to use this function for non-lazy dependencies, in which case2131/// For non-lazy dependencies, this always succeeds.
2124/// it will never return `null`. This allows toggling laziness via2132///
2125/// build.zig.zon without changing build.zig logic.2133/// This function will be eventually renamed to `dependency`.
2126pub fn lazyDependency(b: *Build, name: []const u8, args: anytype) ?*Dependency {2134pub fn dependencyLazy(b: *Build, name: []const u8, args: anytype) error{LazyDependencyNeeded}!*Dependency {
2127 const build_runner = @import("root");2135 const build_runner = @import("root");
2128 const deps = build_runner.dependencies;2136 const deps = build_runner.dependencies;
2129 const pkg_hash = findPkgHashOrFatal(b, name);2137 const pkg_hash = findPkgHashOrFatal(b, name);
...@@ -2134,15 +2142,17 @@ pub fn lazyDependency(b: *Build, name: []const u8, args: anytype) ?*Dependency {...@@ -2134,15 +2142,17 @@ pub fn lazyDependency(b: *Build, name: []const u8, args: anytype) ?*Dependency {
2134 const available = !@hasDecl(pkg, "available") or pkg.available;2142 const available = !@hasDecl(pkg, "available") or pkg.available;
2135 if (!available) {2143 if (!available) {
2136 markNeededLazyDep(b, pkg_hash);2144 markNeededLazyDep(b, pkg_hash);
2137 return null;2145 return error.LazyDependencyNeeded;
2138 }2146 }
2139 return dependencyInner(b, name, pkg.build_root, if (@hasDecl(pkg, "build_zig")) pkg.build_zig else null, pkg_hash, pkg.deps, args);2147 return dependencyInner(b, name, pkg.build_root, if (@hasDecl(pkg, "build_zig")) pkg.build_zig else null, pkg_hash, pkg.deps, args);
2140 }2148 }
2141 }2149 }
21422150
2143 unreachable; // Bad @dependencies source2151 unreachable; // bad @dependencies source
2144}2152}
21452153
2154/// Deprecated in favor of `dependencyLazy`. To get the same behavior as before, use `try` to propagate
2155/// the potential `error.LazyDependencyNeeded` all the way out of your main build function.
2146pub fn dependency(b: *Build, name: []const u8, args: anytype) *Dependency {2156pub fn dependency(b: *Build, name: []const u8, args: anytype) *Dependency {
2147 const build_runner = @import("root");2157 const build_runner = @import("root");
2148 const deps = build_runner.dependencies;2158 const deps = build_runner.dependencies;