ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.1k stars 2.49k forks source link

zig build: `getty-zig/json` no longer compiles #15601

Open cr0sh opened 1 year ago

cr0sh commented 1 year ago

Zig Version

0.11.0-dev.3262+df909da5d

Steps to Reproduce and Observed Behavior

(Steps taken from https://github.com/getty-zig/getty#quick-start)

  1. zig init-exe
  2. Replace build.zig with

    const std = @import("std");
    
    pub fn build(b: *std.Build) void {
        const target = b.standardTargetOptions(.{});
        const optimize = b.standardOptimizeOption(.{});
    
        const opts = .{ .target = target, .optimize = optimize };
        const json_mod = b.dependency("json", opts).module("json");
    
        const exe = b.addExecutable(.{
            .name = "quick-start",
            .root_source_file = .{ .path = "src/main.zig" },
            .target = target,
            .optimize = optimize,
        });
    
        exe.addModule("json", json_mod);
    
        const run_cmd = b.addRunArtifact(exe);
        const run_step = b.step("run", "Run the app");
        run_step.dependOn(&run_cmd.step);
    }
  3. Create build.zig.zon with
    .{
        .name = "quick-start",
        .version = "0.0.0",
        .dependencies = .{
            .json = .{
                .url = "https://github.com/getty-zig/json/archive/5a64449.tar.gz",
                .hash = "122087fe6d2b141548103e98e4324ac0ab889e7d50d82e186f396d90f55a942aef61",
            },
        },
    }
  4. thread 245443 panic: A top-level step with name "clean" already exists
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/debug.zig:317:22: 0x10290ebdf in panicExtra__anon_44215 (build)
        std.builtin.panic(msg, trace, ret_addr);
                         ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/debug.zig:292:15: 0x1028dfb9b in panic__anon_27790 (build)
        panicExtra(null, null, format, args);
                  ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/Build.zig:971:44: 0x1028b195f in step (build)
        if (gop.found_existing) std.debug.panic("A top-level step with name \"{s}\" already exists", .{name});
                                               ^
    /Users/namjh/.cache/zig/p/1220c649e62d6bb0e07ac6b02711f76254f1dd1875859ad075873d248fb820f68d66/build.zig:116:30: 0x1029636b7 in clean (build)
        const clean_step = b.step("clean", "Remove project artifacts");
                                 ^
    /Users/namjh/.cache/zig/p/1220c649e62d6bb0e07ac6b02711f76254f1dd1875859ad075873d248fb820f68d66/build.zig:17:10: 0x102922617 in build (build)
        clean(b);
             ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/Build.zig:1602:33: 0x1028fb907 in runBuild__anon_44111 (build)
            .Void => build_zig.build(b),
                                    ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/Build.zig:1586:25: 0x1028da89b in dependencyInner__anon_27494 (build)
        sub_builder.runBuild(build_zig) catch @panic("unhandled error");
                            ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/Build.zig:1536:35: 0x1028b0e27 in dependency__anon_11051 (build)
                return dependencyInner(b, name, build_root, build_zig, args);
                                      ^
    /Users/namjh/.cache/zig/p/122087fe6d2b141548103e98e4324ac0ab889e7d50d82e186f396d90f55a942aef61/build.zig:13:38: 0x102922ecb in build (build)
        const getty_module = b.dependency("getty", dep_opts).module("getty");
                                         ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/Build.zig:1602:33: 0x1028fbd6b in runBuild__anon_44116 (build)
            .Void => build_zig.build(b),
                                    ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/Build.zig:1586:25: 0x1028daf73 in dependencyInner__anon_27509 (build)
        sub_builder.runBuild(build_zig) catch @panic("unhandled error");
                            ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/Build.zig:1536:35: 0x1028b10ff in dependency__anon_11051 (build)
                return dependencyInner(b, name, build_root, build_zig, args);
                                      ^
    /Users/namjh/dev/hello-getty/build.zig:8:34: 0x102869847 in build (build)
        const json_mod = b.dependency("json", opts).module("json");
                                     ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/Build.zig:1602:33: 0x102855f87 in runBuild__anon_6636 (build)
            .Void => build_zig.build(b),
                                    ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/build_runner.zig:292:29: 0x102851007 in main (build)
            try builder.runBuild(root);
                                ^
    /opt/homebrew/Cellar/zig/HEAD-df909da/lib/zig/std/start.zig:609:37: 0x10285649b in main (build)
                const result = root.main() catch |err| {
                                        ^
    ???:?:?: 0x181eebf27 in ??? (???)
    ???:?:?: 0xb017ffffffffffff in ??? (???)
    error: the following build command crashed:
    /Users/namjh/dev/hello-getty/zig-cache/o/32c71ae3eaaf465b6d86e35423d4e27c/build /opt/homebrew/Cellar/zig/HEAD-df909da/bin/zig /Users/namjh/dev/hello-getty /Users/namjh/dev/hello-getty/zig-cache /Users/namjh/.cache/zig run

    For some context, getty-zig/json depends on getty-zig/getty, and both package defines a step named "clean", which would be a trouble.

Expected Behavior

Compiliation succeeds on 0.11.0-dev.2563+35f9c8444, so it could be affected by a breaking change.

Deecellar commented 1 year ago

Would call it, mismatching steps on build.zig or something?

zig mantainers shouldn't wor about package breakage, but the error itself is weird

nektro commented 1 year ago
matu3ba commented 1 year ago

Likely introduced by https://github.com/ziglang/zig/pull/14872 as unintended side effect (I did not bisect this).

nektro commented 1 year ago

re-tried with 0.11.0-dev.3000+d71a43ec2 and got the same error as well. was not able to repro title issue

cr0sh commented 1 year ago

@nektro What platform are you testing on? I'm on MacOS aarch64 13.3.1a(Ventura), and zig build run downloads the module without any issue. I removed ~/.cache and zig-cache to ensure the modules are freshly downloaded, but correct me if I'm wrong.

nektro commented 1 year ago

I'm on Linux x86_64, NixOS 22.11 in particular

cr0sh commented 1 year ago

Hmm that differs. I think we should wait for https://github.com/ziglang/zig/issues/15590 to be resolved first?

cr0sh commented 1 year ago

Still fails on 0.11.0-dev.3262+df909da5d. Updated the instructions. (FYI the getty maintainer temporarily removed the 'clean' step on the latest master, so you may not reproduce the issue correctly if used)

andrewrk commented 1 year ago

Note that it should be perfectly fine for any packages to have same-named top level steps, since they each have an independent top level step namespace. There is something a bit more subtle and tricky going on here.

As a first step, we can collect a stack trace where a top level step is defined, and then print it when such a conflict occurs. That will make for a nicer debugging experience, no matter what the root cause of this ends up being.