Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build.zig fails to compile due to deduplicated lazy dependency #21771

Closed
dermetfan opened this issue Oct 22, 2024 · 1 comment · Fixed by #21773
Closed

build.zig fails to compile due to deduplicated lazy dependency #21771

dermetfan opened this issue Oct 22, 2024 · 1 comment · Fixed by #21773
Labels
bug Observed behavior contradicts documented or intended behavior zig build system std.Build, the build runner, `zig build` subcommand, package management
Milestone

Comments

@dermetfan
Copy link

dermetfan commented Oct 22, 2024

Zig Version

0.14.0-dev.1952+9f84f7f92

Steps to Reproduce and Observed Behavior

If two dependencies depend on the same package,
but one does so lazily and the other one does not,
the build.zig of the latter fails to compile
if it does not use lazyDependency()
(which it should not need to use as the dependency is not lazy).

Graphically:

         A
        / \
       B   C
(eager) \ / (lazy)
         D

B fails to compile with:

thread 889721 panic: dependency 'b.zqlite' is marked as lazy in build.zig.zon which means it must use the lazyDependency function instead
/nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/lib/std/Build.zig:2095:32: 0x1414dd5 in dependency__anon_10241 (build)
                std.debug.panic("dependency '{s}{s}' is marked as lazy in build.zig.zon which means it must use the lazyDependency function instead", .{ b.dep_prefix, name });
                               ^
/foo/zig-issue-lazy-dep/b/build.zig:17:39: 0x150b433 in build (build)
                .module = b.dependency("zqlite", options).module("zqlite"),
                                      ^
/nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/lib/std/Build.zig:2295:44: 0x149e020 in runBuild__anon_61135 (build)
        .error_union => try build_zig.build(b),
                                           ^
/nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/lib/std/Build.zig:2275:29: 0x145a447 in dependencyInner__anon_55211 (build)
        sub_builder.runBuild(bz) catch @panic("unhandled error");
                            ^
/nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/lib/std/Build.zig:2097:35: 0x1414e40 in dependency__anon_10241 (build)
            return dependencyInner(b, name, pkg.build_root, if (@hasDecl(pkg, "build_zig")) pkg.build_zig else null, pkg_hash, pkg.deps, args);
                                  ^
/foo/zig-issue-lazy-dep/build.zig:14:39: 0x1414909 in build (build)
                .module = b.dependency("b", options).module("b"),
                                      ^
/nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/lib/std/Build.zig:2295:44: 0x13f9c10 in runBuild__anon_4961 (build)
        .error_union => try build_zig.build(b),
                                           ^
/nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/lib/compiler/build_runner.zig:335:29: 0x13f4725 in main (build)
        try builder.runBuild(root);
                            ^
/nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/lib/std/start.zig:621:37: 0x13cf202 in posixCallMainAndExit (build)
            const result = root.main() catch |err| {
                                    ^
/nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/lib/std/start.zig:250:5: 0x13ceddf in _start (build)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
error: the following build command crashed:
/foo/zig-issue-lazy-dep/.zig-cache/o/fb9a870f009dd40dcc1101049c53e9fd/build /nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/bin/zig /nix/store/ypa88d8507lmbqlm6f66s1q9qpzvxlc8-zig-0.14.0-dev.1952+9f84f7f92/lib /foo/zig-issue-lazy-dep /foo/zig-issue-lazy-dep/.zig-cache ~/.cache/zig --seed 0x27cf3f35 -Z5b8c9b2ff9d97b7d

My guess is that the build system marks the deduplicated dependency as lazy even though it is only lazy for C, not B.

In more complex dependency graphs this seems to happen about half the time I run zig build which leads me to conclude that it's related to dependency traversal order. However in this small example, I'm unable to find a --seed that works around the issue.

So without knowing any internals of the build system, I guess that this is what happens:

  1. The dependency graph is traversed, leading to two entries for D: One lazy (from C), one eager (from B).
  2. The eager one is dropped during deduplication because laziness is not taken into account for equality, so now the build system thinks all instances of D are lazy.
  3. B's build.zig is compiled given the lazy D in @import("@dependencies").

I prepared a repository so you can reproduce easily by running zig build: https://github.com/dermetfan/zig-issue-lazy-dep

Expected Behavior

It should not be necessary to use lazyDependency() instead of dependency() as the dependency is not lazy.

@dermetfan dermetfan added the bug Observed behavior contradicts documented or intended behavior label Oct 22, 2024
@mitchellh
Copy link
Contributor

I assume you have a project you hit this with but to add another note that this isn't theoretical: this just caused a real build issue while updating a dependency in Ghostty. One of our dependencies added a dependency we have and marked it lazy (Ghostty doesn't) and Ghostty no longer builds. For now, we've chosen to not update that dependency.

@andrewrk andrewrk added this to the 0.14.0 milestone Nov 13, 2024
@andrewrk andrewrk added the zig build system std.Build, the build runner, `zig build` subcommand, package management label Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior zig build system std.Build, the build runner, `zig build` subcommand, package management
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants