authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-10 10:48:52+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-20 00:14:58+00:00
logb8e568504e1dca38273a5845ac20d056cd7db5bb
tree82822f7b5d9be9aeec46a5cda41ea05a156137bc
parentb074fb7ddaefaa6ed9c5235d22b2f7378e3b80e8
signaturelock-open Commit is signed but in an unrecognized format.

std.Build: extend `test_runner` option to specify whether runner uses `std.zig.Server`

The previous logic here was trying to assume that custom test runners never used `std.zig.Server` to communicate with the build runner; however, it was flawed, because modifying the `test_runner` field on `Step.Compile` would not update this flag. That might have been intentional (allowing a way for the user to specify a custom test runner which *does* use the compiler server protocol), but if so, it was a flawed API, since it was too easy to update one field without updating the other. Instead, bundle these two pieces of state into a new type `std.Build.Step.Compile.TestRunner`. When passing a custom test runner, you are now *provided* to specify whether it is a "simple" runner, or whether it uses the compiler server protocol. This is a breaking change, but is unlikely to affect many people, since custom test runners are seldom used in the wild.

4 files changed, 29 insertions(+), 15 deletions(-)

lib/std/Build.zig+3-4
......@@ -979,7 +979,7 @@ pub const TestOptions = struct {
979979 /// Deprecated; use `.filters = &.{filter}` instead of `.filter = filter`.
980980 filter: ?[]const u8 = null,
981981 filters: []const []const u8 = &.{},
982 test_runner: ?LazyPath = null,
982 test_runner: ?Step.Compile.TestRunner = null,
983983 use_llvm: ?bool = null,
984984 use_lld: ?bool = null,
985985 zig_lib_dir: ?LazyPath = null,
......@@ -1136,9 +1136,8 @@ pub fn addRunArtifact(b: *Build, exe: *Step.Compile) *Step.Run {
11361136 run_step.addArtifactArg(exe);
11371137 }
11381138
1139 if (exe.test_server_mode) {
1140 run_step.enableTestRunnerMode();
1141 }
1139 const test_server_mode = if (exe.test_runner) |r| r.mode == .server else true;
1140 if (test_server_mode) run_step.enableTestRunnerMode();
11421141 } else {
11431142 run_step.addArtifactArg(exe);
11441143 }
lib/std/Build/Step/Compile.zig+18-9
......@@ -56,8 +56,7 @@ global_base: ?u64 = null,
5656zig_lib_dir: ?LazyPath,
5757exec_cmd_args: ?[]const ?[]const u8,
5858filters: []const []const u8,
59test_runner: ?LazyPath,
60test_server_mode: bool,
59test_runner: ?TestRunner,
6160wasi_exec_model: ?std.builtin.WasiExecModel = null,
6261
6362installed_headers: ArrayList(HeaderInstallation),
......@@ -268,7 +267,7 @@ pub const Options = struct {
268267 version: ?std.SemanticVersion = null,
269268 max_rss: usize = 0,
270269 filters: []const []const u8 = &.{},
271 test_runner: ?LazyPath = null,
270 test_runner: ?TestRunner = null,
272271 use_llvm: ?bool = null,
273272 use_lld: ?bool = null,
274273 zig_lib_dir: ?LazyPath = null,
......@@ -347,6 +346,14 @@ pub const HeaderInstallation = union(enum) {
347346 }
348347};
349348
349pub const TestRunner = struct {
350 path: LazyPath,
351 /// Test runners can either be "simple", running tests when spawned and terminating when the
352 /// tests are complete, or they can use `std.zig.Server` over stdio to interact more closely
353 /// with the build system.
354 mode: enum { simple, server },
355};
356
350357pub fn create(owner: *std.Build, options: Options) *Compile {
351358 const name = owner.dupe(options.name);
352359 if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) {
......@@ -411,8 +418,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
411418 .zig_lib_dir = null,
412419 .exec_cmd_args = null,
413420 .filters = options.filters,
414 .test_runner = null,
415 .test_server_mode = options.test_runner == null,
421 .test_runner = null, // set below
416422 .rdynamic = false,
417423 .installed_path = null,
418424 .force_undefined_symbols = StringHashMap(void).init(owner.allocator),
......@@ -438,9 +444,12 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
438444 lp.addStepDependencies(&compile.step);
439445 }
440446
441 if (options.test_runner) |lp| {
442 compile.test_runner = lp.dupe(compile.step.owner);
443 lp.addStepDependencies(&compile.step);
447 if (options.test_runner) |runner| {
448 compile.test_runner = .{
449 .path = runner.path.dupe(compile.step.owner),
450 .mode = runner.mode,
451 };
452 runner.path.addStepDependencies(&compile.step);
444453 }
445454
446455 // Only the PE/COFF format has a Resource Table which is where the manifest
......@@ -1399,7 +1408,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
13991408
14001409 if (compile.test_runner) |test_runner| {
14011410 try zig_args.append("--test-runner");
1402 try zig_args.append(test_runner.getPath2(b, step));
1411 try zig_args.append(test_runner.path.getPath2(b, step));
14031412 }
14041413
14051414 for (b.debug_log_scopes) |log_scope| {
test/standalone/test_runner_module_imports/build.zig+4-1
......@@ -13,7 +13,10 @@ pub fn build(b: *std.Build) void {
1313
1414 const t = b.addTest(.{
1515 .root_module = test_mod,
16 .test_runner = b.path("test_runner/main.zig"),
16 .test_runner = .{
17 .path = b.path("test_runner/main.zig"),
18 .mode = .simple,
19 },
1720 });
1821
1922 const test_step = b.step("test", "Run unit tests");
test/standalone/test_runner_path/build.zig+4-1
......@@ -8,7 +8,10 @@ pub fn build(b: *std.Build) void {
88 .target = b.graph.host,
99 .root_source_file = b.path("test.zig"),
1010 }) });
11 test_exe.test_runner = b.path("test_runner.zig");
11 test_exe.test_runner = .{
12 .path = b.path("test_runner.zig"),
13 .mode = .simple,
14 };
1215
1316 const test_run = b.addRunArtifact(test_exe);
1417 test_step.dependOn(&test_run.step);