I’m trying to declare an enum type in my build.zig, create a build flag for it with Build.option(), and pass the flag down to the module with Build.addOptions() → Module.addOptions().
const UnixWindowingSystem = enum { x11, wayland };
const linux_window_hint = b.option(
UnixWindowingSystem,
"linux_windowing",
"If targeting Linux, hint the windowing system.",
);
const options = b.addOptions();
options.addOption(
?UnixWindowingSystem,
"linux_windowing",
linux_window_hint,
);
module.addOptions("options", options);
When I compile:
.zig-cache/c/2e7c8fa2ad0f40ba4fb243e0c52d0261/options.zig:1:29: error: use of undeclared identifier 'build'
pub const linux_windowing: ?build.build.UnixWindowingSystem = null;
^~~~~
I see the issue is that the generated options.zig expects a missing build module where the type should have been passed down.
How do I give it the type? I’ve already tried putting the identifier at every scope of build.zig and searched through the std docs… sorry if I missed the right function. Is this not something I’m able to do?
std.Build.Step.Options generating invalid code for arrays/slices/optionals of custom types is a known issue:
opened 05:20PM - 09 Apr 24 UTC
bug
standard library
zig build system
### Zig Version
0.12.0-dev.3597+d979df585
### Steps to Reproduce and Observed … Behavior
```sh
git clone https://github.com/lbirkert/issue_zig_options
zig build run
```
crashes with
```
$ zig build run
run
└─ run issue_zig_options
└─ install
└─ install issue_zig_options
└─ zig build-exe issue_zig_options Debug native 1 errors
zig-cache/c/c7bcb4d097e4baeff8cd8d531d51090a/options.zig:1:57: error: expected expression, found 'pub'
pub const @"test": []const build.Test = &[_]build.Test {
^
error: the following command failed with 1 compilation errors:
/usr/lib/zig/zig build-exe -ODebug --dep options -Mroot=/home/lbirkert/Projects/issue_zig_options/src/main.zig -Moptions=/home/lbirkert/Projects/issue_zig_options/zig-cache/c/c7bcb4d097e4baeff8cd8d531d51090a/options.zig --cache-dir /home/lbirkert/Projects/issue_zig_options/zig-cache --global-cache-dir /home/lbirkert/.cache/zig --name issue_zig_options --listen=-
Build Summary: 1/6 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run issue_zig_options transitive failure
├─ zig build-exe issue_zig_options Debug native 1 errors
└─ install transitive failure
└─ install issue_zig_options transitive failure
└─ zig build-exe issue_zig_options Debug native (+1 more reused dependencies)
error: the following build command failed with exit code 1:
/home/lbirkert/Projects/issue_zig_options/zig-cache/o/0abbb8db28aae576395c817b802d44b8/build /usr/lib/zig/zig /home/lbirkert/Projects/issue_zig_options /home/lbirkert/Projects/issue_zig_options/zig-cache /home/lbirkert/.cache/zig --seed 0x52ce0923 -Zac9cc083f3c12f62 run
```
options.zig:
```zig
pub const @"test": []const build.Test = &[_]build.Test {
pub const @"build.Test" = struct {
};
};
```
neofetch:
```
$ neofetch
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢰⡆⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ lbirkert@laptop
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢠⣿⣿⡄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⣾⣿⣿⣿⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ · Archcraft x86_64
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣷⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ · LIFEBOOK A557
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣿⣿⣷⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ · 6.8.2-arch2-1
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢼⣿⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ · 4 hours, 43 mins
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣰⣤⣈⠻⢿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ · 1454 (pacman), 13 (flatpak)
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣮⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ · zsh 5.9
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ · 1920x1080
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ · bspwm
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ · Arc-Dark [GTK2/3]
⠄⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣿⡿⣿⣿⡟⠄⠄⠸⣿⣿⡿⣿⣿⣿⣿⣿⣷⡀⠄⠄⠄⠄⠄⠄⠄⠄ · Arc-Circle [GTK2/3]
⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣿⡏⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠈⣿⣿⣿⣿⣿⣷⡀⠄⠄⠄⠄⠄⠄⠄ · alacritty
⠄⠄⠄⠄⠄⠄⢀⣼⣿⣿⣿⣿⣿⣿⡗⠄⠄⠄⢀⣠⣤⣀⠄⠄⠄⠸⣿⣿⣿⣿⣿⣿⣷⡀⠄⠄⠄⠄⠄⠄ · Intel i5-7200U (4) @ 3.100GHz
⠄⠄⠄⠄⠄⢀⣾⣿⣿⣿⣿⣿⡏⠁⠄⠄⠄⢠⣿⣿⣿⣿⡇⠄⠄⠄⠄⢙⣿⣿⣻⠿⣿⣷⡀⠄⠄⠄⠄⠄ · Intel HD Graphics 620
⠄⠄⠄⠄⢀⣾⣿⣿⣿⣿⣿⣿⣷⣤⡀⠄⠄⠄⠻⣿⣿⡿⠃⠄⠄⠄⢀⣼⣿⣿⣿⣿⣦⣌⠙⠄⠄⠄⠄⠄ · 3811MiB / 15894MiB
⠄⠄⠄⢠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡀⠄⠄⠄
⠄⠄⢠⣿⣿⣿⣿⣿⣿⣿⡿⠟⠋⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠙⠻⣿⣿⣿⣿⣿⣿⣿⣿⡄⠄⠄
⠄⣠⣿⣿⣿⣿⠿⠛⠋⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠉⠙⠻⢿⣿⣿⣿⣿⣆⠄
⡰⠟⠛⠉⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠉⠙⠛⠿⢆
```
### Expected Behavior
One should be able to pass structs as build options. The zig compiler seems to currently generate faulty code in the options.zig file. I believe the declaration of the struct should happen earlier in the file. Expected code:
```zig
pub const @"build.Test" = struct {};
pub const @"test": []const build.Test = &[_]build.Test {
.{},
};
```
opened 08:34PM - 14 Apr 24 UTC
bug
zig build system
### Zig Version
0.12.0-dev.3633+f7a76bdfe
### Steps to Reproduce and Observed … Behavior
My setup is as follows:
```zig
// src/BuildOptions.zig
pub const EngineModule = enum { ... };
```
```zig
// build.zig
const BuildOptions = @import("./src/BuildOptions.zig");
...
const buildOptions = b.addOptions();
buildOptions.addOption([]BuildOptions.EngineModule, "modules", ...);
...
projectExe.root_module.addOptions("build_options", buildOptions);
```
```zig
// src/main.zig
const BuildOptions = @import("./BuildOptions.zig");
const buildOptions: BuildOptions = @import("build_options");
...
```
This results in something akin to the following errors in the generated `options.zig` file somewhere in `zig-cache`:
```
zig-cache\c\1851a2fa7541ae7147214fe8bc710207\options.zig:11:31: error: use of undeclared identifier 'src'
pub const disabled_modules: []src.BuildOptions.EngineModule = &[_]src.BuildOptions.EngineModule {
```
Here's the generated code:
```zig
pub const disabled_modules: []src.BuildOptions.EngineModule = &[_]src.BuildOptions.EngineModule {
};
```
### Expected Behavior
Presumably the build system is failing to recognise that `EngineModule` is a symbol that needs to be referenced and contained within the `options.zig` file, since it's inside an array. You can see this since other options have their types properly generated:

Expected behaviour is probably that the `EngineModule` enum is properly copied to the `options.zig` file inside `zig-cache` when using options.
There are two open pull requests (1 , 2 ) that attempt to fix this but they are not passing CI or have been abandoned.
As a workaround you could define the enum as enum { none, x11, wayland }, define the option as non-optional and use .none in place of null.
2 Likes