Browse Source

build: don't print errors in ZiglingStep.eval

Move the code for printing compiler errors and messages to the new
ZiglingStep.printErrors method.

Call printErrors in the Zigling.doCompile method, both in the normal and
error flow.

When handling an error from the Zig IPC, add the case when the compiler
was unable to return the executable path.

Before using the IPC, the error was
  "The following command exited with error code 1"
now it is
  "The following command failed to communicate the compilation result"
Manlio Perillo 2 years ago
parent
commit
c6e055dd83
1 changed files with 33 additions and 14 deletions
  1. 33 14
      build.zig

+ 33 - 14
build.zig

@@ -690,6 +690,9 @@ const ZiglingStep = struct {
     builder: *Build,
     builder: *Build,
     use_healed: bool,
     use_healed: bool,
 
 
+    result_messages: []const u8 = "",
+    result_error_bundle: std.zig.ErrorBundle = std.zig.ErrorBundle.empty,
+
     pub fn create(builder: *Build, exercise: Exercise, use_healed: bool) *@This() {
     pub fn create(builder: *Build, exercise: Exercise, use_healed: bool) *@This() {
         const self = builder.allocator.create(@This()) catch unreachable;
         const self = builder.allocator.create(@This()) catch unreachable;
         self.* = .{
         self.* = .{
@@ -829,6 +832,8 @@ const ZiglingStep = struct {
         const argv = zig_args.items;
         const argv = zig_args.items;
         var code: u8 = undefined;
         var code: u8 = undefined;
         const file_name = self.eval(argv, &code, prog_node) catch |err| {
         const file_name = self.eval(argv, &code, prog_node) catch |err| {
+            self.printErrors();
+
             switch (err) {
             switch (err) {
                 error.FileNotFound => {
                 error.FileNotFound => {
                     print("{s}{s}: Unable to spawn the following command: file not found{s}\n", .{ red_text, self.exercise.main_file, reset_text });
                     print("{s}{s}: Unable to spawn the following command: file not found{s}\n", .{ red_text, self.exercise.main_file, reset_text });
@@ -845,11 +850,21 @@ const ZiglingStep = struct {
                     for (argv) |v| print("{s} ", .{v});
                     for (argv) |v| print("{s} ", .{v});
                     print("\n", .{});
                     print("\n", .{});
                 },
                 },
+                error.ZigIPCError => {
+                    print("{s}{s}: The following command failed to communicate the compilation result:{s}\n", .{
+                        red_text,
+                        self.exercise.main_file,
+                        reset_text,
+                    });
+                    for (argv) |v| print("{s} ", .{v});
+                    print("\n", .{});
+                },
                 else => {},
                 else => {},
             }
             }
 
 
             return err;
             return err;
         };
         };
+        self.printErrors();
 
 
         return file_name;
         return file_name;
     }
     }
@@ -908,17 +923,7 @@ const ZiglingStep = struct {
                         return error.ZigVersionMismatch;
                         return error.ZigVersionMismatch;
                 },
                 },
                 .error_bundle => {
                 .error_bundle => {
-                    const error_bundle = try ipc.parseErrorBundle(allocator, body);
-
-                    // Print the compiler error bundle now.
-                    // TODO: use the same ttyconf from the builder.
-                    const ttyconf: std.debug.TTY.Config = if (use_color_escapes)
-                        .escape_codes
-                    else
-                        .no_color;
-                    error_bundle.renderToStdErr(
-                        .{ .ttyconf = ttyconf },
-                    );
+                    self.result_error_bundle = try ipc.parseErrorBundle(allocator, body);
                 },
                 },
                 .progress => {
                 .progress => {
                     node_name.clearRetainingCapacity();
                     node_name.clearRetainingCapacity();
@@ -937,9 +942,7 @@ const ZiglingStep = struct {
 
 
         const stderr = poller.fifo(.stderr);
         const stderr = poller.fifo(.stderr);
         if (stderr.readableLength() > 0) {
         if (stderr.readableLength() > 0) {
-            // Print the additional log and verbose messages now.
-            const messages = try stderr.toOwnedSlice();
-            print("{s}\n", .{messages});
+            self.result_messages = try stderr.toOwnedSlice();
         }
         }
 
 
         // Send EOF to stdin.
         // Send EOF to stdin.
@@ -965,6 +968,22 @@ const ZiglingStep = struct {
 
 
         return result orelse return error.ZigIPCError;
         return result orelse return error.ZigIPCError;
     }
     }
+
+    fn printErrors(self: *ZiglingStep) void {
+        // Print the additional log and verbose messages.
+        // TODO: use colors?
+        if (self.result_messages.len > 0) print("{s}", .{self.result_messages});
+
+        // Print the compiler errors.
+        // TODO: use the same ttyconf from the builder.
+        const ttyconf: std.debug.TTY.Config = if (use_color_escapes)
+            .escape_codes
+        else
+            .no_color;
+        if (self.result_error_bundle.errorMessageCount() > 0) {
+            self.result_error_bundle.renderToStdErr(.{ .ttyconf = ttyconf });
+        }
+    }
 };
 };
 
 
 // Print a message to stderr.
 // Print a message to stderr.