#Read `build.zig` options from `zig test`

1 messages · Page 1 of 1 (latest)

calm citrus
#

In build.zig I can make some nice options:

const module = b.option([]const u8, "foo", "Path to foo") orelse "/path/to/foo";
const options = b.addOptions();
options.addOption([]const u8, "foo", foo);
lib_unit_tests.root_module.addOptions("config", options);

Given the above, if I execute tests via zig build test, then any options I supplied in build.zig can be read pretty easily in my code:

const config = @import("config");
// ... I can read config.foo here.

If I execute the same tests, but instead I use zig test main.zig, the compiler tells me I cannot find the "config" import. Is there a method to retrieve build options added in build.zig which works for both scenarios (zig build test and zig test foo.zig)? I thought about using environment variables instead, but on non-POSIX systems, I have to use std.process.getEnvVarOwned which I'd like to avoid. Should I be doing this a different way?

sage jackal
#

build system options only work in the context of the build system. If you run zig test foo.zig, you are just compiling that single file without any other context

#

is there a particular reason why you want to run zig test and not zig build test?

calm citrus
#

Mostly just because I can't get zig build test to support debugging in VSCode. I swapped to using zig test --test-no-exec --femit-bin=/path/to/bin src/main.zig so I can just run the emitted binary and attach the debugger.

sage jackal
#

you could try to emit the build options file as a regular file and register it as a module through the command line

#

let me look up the appropriate commands

calm citrus
#

ultimately I'd love to attach a debugger to zig build test but I think it's forking a child process or something.

#

and I can't attach to it at all

#

using zig test I end up having to do all the linking manually on the command line anyway. I'd much rather use the build system.

sage jackal
#

actually, have you tried installing the test executable somewhere and running it manually like a regular executable, attaching the debugger?

#

i.e. b.installArtifact(unit_tests)

calm citrus
#

I have not. Let me give that a try. It sounds very similar to how I use zig test to emit a bin and then run it.

sage jackal
#

either way, I do think the problems/inconveniences with attaching debuggers when using the build system would be valuable feedback for the core team

calm citrus
#

this works actually! Now I can attach a debugger to the installed test binary. thanks for the suggestion!