zig icon indicating copy to clipboard operation
zig copied to clipboard

Root set to test runner instead of main file when running tests

Open Sirius902 opened this issue 3 years ago • 0 comments

Zig Version

0.10.0-dev.3071+8e75ba653

Steps to Reproduce

Create a project with zig init-exe and modify main.zig to contain the following code.

const a = @import("a.zig");

comptime {
    _ = a;
}

pub const message = "hi";

pub fn main() void {
    a.logMessage();
}

Then, add a.zig with the following source.

const std = @import("std");
const message = @import("root").message;

pub fn logMessage() void {
    std.log.info(message, .{});
}

test "foo" {
    try std.testing.expectEqualStrings("hi", message);
}

Expected Behavior

I expect root to be main.zig both when executing zig build run and zig build test, causing both to compile successfully.

Actual Behavior

The program runs properly when run with zig build run, outputting info: hi, but when tests are run with zig build test it fails with the following compiler error.

.\src\a.zig:2:32: error: container '(root)' has no member called 'message'
const message = @import("root").message;
                               ^
error: test...

I believe this is due to root being main.zig when building the executable, but it is lib/test_runner.zig when running tests.

Sirius902 avatar Jul 22 '22 21:07 Sirius902