I have a project with the following structure:
$ tree .
src
├── decode.zig
├── main.zig
└── types.zig
The types.zig file is part of a new module which I'm trying to extract out some type definitions into. Previously, they were within decode.zig, i.e.:
$ tree src
src
├── decode.zig
└── main.zig
In order to create the new module, I've:
- Created the
src/types.zigfile - Added a call to
createModuleto mybuild.zigfile:
exe.addModule("types", b.createModule(.{ .source_file = std.build.FileSource.relative("src/types.zig") }));
- Added appropriate import statements to both
src/main.zigandsrc/decode.zig, for the relevant types depended on by these files.
Currently, zig build succeeds and the executable runs fine. The problem is, when I go to run the (previously passing) test suite, via zig test src/decode.zig:
$ zig test/decode.zig
src/decode.zig:4:20: error: root struct of file 'test_runner' has no member named 'types'
const types = cairo.types;
~~~~~^~~~~~
/home/jmcph4/Downloads/zig-linux-x86_64-0.12.0-dev.1861+412999621/lib/test_runner.zig:1:1: note: struct declared here
//! Default test runner for unit tests.
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
Instruction: src/decode.zig:6:21
decode: src/decode.zig:107:79
remaining reference traces hidden; use '-freference-trace' to see all reference traces
Steps I've taken in an effort to remedy this:
- Add
createModulecalls to theunit_testsartefact within mybuild.zigfile, like so:
unit_tests.addModule("decode", b.createModule(.{ .source_file = std.build.FileSource.relative("src/decode.zig") }));
unit_tests.addModule("types", b.createModule(.{ .source_file = std.build.FileSource.relative("src/types.zig") }));
For reference, I encountered these tickets:
- Root set to test runner instead of main file when running tests
- "root" module is real root file when building executable, but is test_runner when testing
How do I declare these modules in a way that is both compatible with zig build and zig test?
zig buildruns the build system which is intended to contain all the otherzigcommands, likezig build-exeandzig test. If you would like to use the build system for normal building, you may want to use the build system for testing too. Tests then would be run withzig build test.Without modules:
In the project structure you provided, there is no need for modules.
main.zigcan importtypes.zigdirectly and so candecode.zig. Modules are intended for large chunks of code to share between different zig projects.Using the build system:
If you run
zig init-exein an empty folder, the default build script contains an example for running tests: (the comments have been removed for brevity, but they are useful for understanding the build system)Unfortunately, there doesn't seem to be a standardized way of adding a module to both build steps right now, but the module can be added to unit_tests the same way you tried:
Tests can then be run with
zig build test(NOTzig test)Without the build system:
zig testaccepts command line arguments to specify module roots. This will only work for simple modules, modules from the internet that have complex build.zig logic won't be able to be used this way.The syntax is described in
zig test --help: