Zig Build Step.Options cannot be found

I have the following code in my build.zig:

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const options = b.addOptions();
    options.addOption(bool, "some_config", 123);

    const anyline_zig_mod = b.addModule("anyline", .{
        .root_source_file = b.path("src/root.zig"),
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    anyline_zig_mod.addOptions("build_config", options);

    // I'm skipping code for brevity
    // The code simply adds the module to an executable's imports
}

When I attempt to import the build_config in src/root.zig, I get the following compiler error:

error: no module named 'build_config' available within module 'root'
const build_config = @import("build_config");

I’ve used Step.Options in other projects before, but for some reason, this one cannot be found. What am I missing?

What we’re missing is the rest of build.zig, and the import of build_config. Sharing the minimum amount off source code is generally only useful if you’re providing a minimal workable example of what breaks or works.

My guess would be that you add the options to some module, but not another that also needs them.

1 Like

Yep, that was the problem. There was a second module that also imported src/root.zig, but didn’t have build_config added as an option.

1 Like