Commit 1f7390f399

dweiller <4678790+dweiller@users.noreplay.github.com>
2023-02-08 03:56:03
fix custom test runner package path resolution
Fixes #13970. This fix makes test runners resolve package paths relative to the directory the test runner is in. This means it is not possible to import a file from outside the file tree root at the directory containing the test runner.
1 parent 9ccd8ed
Changed files (8)
src/Compilation.zig
@@ -1621,8 +1621,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
             const root_pkg = if (options.is_test) root_pkg: {
                 // TODO: we currently have two packages named 'root' here, which is weird. This
                 // should be changed as part of the resolution of #12201
-                const test_pkg = if (options.test_runner_path) |test_runner|
-                    try Package.create(gpa, "root", null, test_runner)
+                const test_pkg = if (options.test_runner_path) |test_runner| test_pkg: {
+                    const test_dir = std.fs.path.dirname(test_runner);
+                    const basename = std.fs.path.basename(test_runner);
+                    break :test_pkg try Package.create(gpa, "root", test_dir, basename);
+                }
                 else
                     try Package.createWithDir(
                         gpa,
test/standalone/issue_13970/src/empty.zig
test/standalone/issue_13970/src/main.zig
@@ -0,0 +1,8 @@
+const std = @import("std");
+const package = @import("package.zig");
+const root = @import("root");
+const builtin = @import("builtin");
+
+pub fn main() !void {
+    _ = package.decl;
+}
test/standalone/issue_13970/src/package.zig
@@ -0,0 +1,1 @@
+pub const decl = 0;
test/standalone/issue_13970/test_root/empty.zig
test/standalone/issue_13970/build.zig
@@ -0,0 +1,21 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+    const test1 = b.addTest(.{
+        .root_source_file = .{ .path = "test_root/empty.zig" },
+    });
+    const test2 = b.addTest(.{
+        .root_source_file = .{ .path = "src/empty.zig" },
+    });
+    const test3 = b.addTest(.{
+        .root_source_file = .{ .path = "empty.zig" },
+    });
+    test1.setTestRunner("src/main.zig");
+    test2.setTestRunner("src/main.zig");
+    test3.setTestRunner("src/main.zig");
+
+    const test_step = b.step("test", "Test package path resolution of custom test runner");
+    test_step.dependOn(&test1.step);
+    test_step.dependOn(&test2.step);
+    test_step.dependOn(&test3.step);
+}
test/standalone/issue_13970/empty.zig
test/standalone.zig
@@ -27,6 +27,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
     cases.add("test/standalone/noreturn_call/inline.zig");
     cases.add("test/standalone/noreturn_call/as_arg.zig");
     cases.addBuildFile("test/standalone/test_runner_path/build.zig", .{ .requires_stage2 = true });
+    cases.addBuildFile("test/standalone/issue_13970/build.zig", .{});
     cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{});
     cases.addBuildFile("test/standalone/shared_library/build.zig", .{});
     cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{});