Sobeston / zig.guide

Repo for https://zig.guide content. Get up to speed with Zig quickly.
https://zig.guide
MIT License
647 stars 170 forks source link

Hello Zig Build example doesn't work for 0.13.0 #262

Closed jhy closed 4 days ago

jhy commented 4 weeks ago

Hi there,

I can't get the Hello Zig Build example at https://zig.guide/build-system/zig-build to work. With a copy & paste of build.zig and src/main.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const exe = b.addExecutable(.{
        .name = "hello",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = b.standardTargetOptions(.{}),
        .optimize = b.standardOptimizeOption(.{}),
    });

    b.installArtifact(exe);
}
jhy@jhy-m1 zig-test % zig version
0.13.0

jhy@jhy-m1 zig-test % zig build
/Users/jhy/projects/zig-test/build.zig:6:33: error: no field named 'path' in union 'Build.LazyPath'
        .root_source_file = .{ .path = "src/main.zig" },
                                ^~~~
/opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/Build.zig:2171:22: note: union declared here
pub const LazyPath = union(enum) {
                     ^~~~~
referenced by:
    runBuild__anon_8407: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/Build.zig:2116:27
    main: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/compiler/build_runner.zig:301:29
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

From reading the Build.zig, I found this works:

        .root_source_file = .{ .cwd_relative = "src/main.zig" },

But the docs say:

    /// An absolute path or a path relative to the current working directory of
    /// the build runner process.
    /// This is uncommon but used for system environment paths such as `--zig-lib-dir` which
    /// ignore the file system path of build.zig and instead are relative to the directory from
    /// which `zig build` was invoked.
    /// Use of this tag indicates a dependency on the host system.

So I'm not sure what the idiomatic zig example should be.

Sobeston commented 4 weeks ago

Hi Jonathan, this example should probably be using b.path("src/main.zig"). LazyPath changed somewhat recently; this code unfortunately isn't covered by testing yet so it was missed. I'll have a look at this later.

jhy commented 4 weeks ago

Great, yes .root_source_file = b.path("src/main.zig"), works.

Thanks for your work on the guide, very much appreciated!