Commit 363d4a107d

Andrew Kelley <andrew@ziglang.org>
2023-03-15 00:12:24
add compile log output to build runner
1 parent ed33901
Changed files (4)
lib/std/zig/ErrorBundle.zig
@@ -32,6 +32,8 @@ pub const SourceLocationIndex = enum(u32) {
 pub const ErrorMessageList = struct {
     len: u32,
     start: u32,
+    /// null-terminated string index. 0 means no compile log text.
+    compile_log_text: u32,
 };
 
 /// Trailing:
@@ -110,6 +112,10 @@ pub fn getNotes(eb: ErrorBundle, index: MessageIndex) []const MessageIndex {
     return @ptrCast([]const MessageIndex, eb.extra[start..][0..notes_len]);
 }
 
+pub fn getCompileLogOutput(eb: ErrorBundle) [:0]const u8 {
+    return nullTerminatedString(eb, getErrorMessageList(eb).compile_log_text);
+}
+
 /// Returns the requested data, as well as the new index which is at the start of the
 /// trailers for the object.
 fn extraData(eb: ErrorBundle, comptime T: type, index: usize) struct { data: T, end: usize } {
@@ -145,6 +151,7 @@ pub const RenderOptions = struct {
     ttyconf: std.debug.TTY.Config,
     include_reference_trace: bool = true,
     include_source_line: bool = true,
+    include_log_text: bool = true,
 };
 
 pub fn renderToStdErr(eb: ErrorBundle, options: RenderOptions) void {
@@ -158,6 +165,14 @@ pub fn renderToWriter(eb: ErrorBundle, options: RenderOptions, writer: anytype)
     for (eb.getMessages()) |err_msg| {
         try renderErrorMessageToWriter(eb, options, err_msg, writer, "error", .Red, 0);
     }
+
+    if (options.include_log_text) {
+        const log_text = eb.getCompileLogOutput();
+        if (log_text.len != 0) {
+            try writer.writeAll("\nCompile Log Output:\n");
+            try writer.writeAll(log_text);
+        }
+    }
 }
 
 fn renderErrorMessageToWriter(
@@ -314,6 +329,7 @@ pub const Wip = struct {
         assert(0 == try addExtra(wip, ErrorMessageList{
             .len = 0,
             .start = 0,
+            .compile_log_text = 0,
         }));
     }
 
@@ -325,9 +341,10 @@ pub const Wip = struct {
         wip.* = undefined;
     }
 
-    pub fn toOwnedBundle(wip: *Wip) !ErrorBundle {
+    pub fn toOwnedBundle(wip: *Wip, compile_log_text: []const u8) !ErrorBundle {
         const gpa = wip.gpa;
         if (wip.root_list.items.len == 0) {
+            assert(compile_log_text.len == 0);
             // Special encoding when there are no errors.
             wip.deinit();
             wip.* = .{
@@ -338,9 +355,19 @@ pub const Wip = struct {
             };
             return empty;
         }
+
+        const compile_log_str_index = if (compile_log_text.len == 0) 0 else str: {
+            const str = @intCast(u32, wip.string_bytes.items.len);
+            try wip.string_bytes.ensureUnusedCapacity(gpa, compile_log_text.len + 1);
+            wip.string_bytes.appendSliceAssumeCapacity(compile_log_text);
+            wip.string_bytes.appendAssumeCapacity(0);
+            break :str str;
+        };
+
         wip.setExtra(0, ErrorMessageList{
             .len = @intCast(u32, wip.root_list.items.len),
             .start = @intCast(u32, wip.extra.items.len),
+            .compile_log_text = compile_log_str_index,
         });
         try wip.extra.appendSlice(gpa, @ptrCast([]const u32, wip.root_list.items));
         wip.root_list.clearAndFree(gpa);
src/Compilation.zig
@@ -2705,7 +2705,8 @@ pub fn getAllErrorsAlloc(self: *Compilation) !ErrorBundle {
 
     assert(self.totalErrorCount() == bundle.root_list.items.len);
 
-    return bundle.toOwnedBundle();
+    const compile_log_text = if (self.bin_file.options.module) |m| m.compile_log_text.items else "";
+    return bundle.toOwnedBundle(compile_log_text);
 }
 
 pub const ErrorNoteHashContext = struct {
@@ -2954,11 +2955,6 @@ pub fn addZirErrorMessages(eb: *ErrorBundle.Wip, file: *Module.File) !void {
     }
 }
 
-pub fn getCompileLogOutput(self: *Compilation) []const u8 {
-    const module = self.bin_file.options.module orelse return &[0]u8{};
-    return module.compile_log_text.items;
-}
-
 pub fn performAllTheWork(
     comp: *Compilation,
     main_progress_node: *std.Progress.Node,
src/main.zig
@@ -3886,10 +3886,6 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void
 
     if (errors.errorMessageCount() > 0) {
         errors.renderToStdErr(renderOptions(comp.color));
-        const log_text = comp.getCompileLogOutput();
-        if (log_text.len != 0) {
-            std.debug.print("\nCompile Log Output:\n{s}", .{log_text});
-        }
         return error.SemanticAnalyzeFail;
     } else switch (hook) {
         .none => {},
@@ -4512,7 +4508,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
                 &all_modules,
             );
             if (wip_errors.root_list.items.len > 0) {
-                var errors = try wip_errors.toOwnedBundle();
+                var errors = try wip_errors.toOwnedBundle("");
                 defer errors.deinit(gpa);
                 errors.renderToStdErr(renderOptions(color));
                 process.exit(1);
@@ -4775,7 +4771,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
                 try wip_errors.init(gpa);
                 defer wip_errors.deinit();
                 try Compilation.addZirErrorMessages(&wip_errors, &file);
-                var error_bundle = try wip_errors.toOwnedBundle();
+                var error_bundle = try wip_errors.toOwnedBundle("");
                 defer error_bundle.deinit(gpa);
                 error_bundle.renderToStdErr(renderOptions(color));
                 process.exit(2);
@@ -4981,7 +4977,7 @@ fn fmtPathFile(
             try wip_errors.init(gpa);
             defer wip_errors.deinit();
             try Compilation.addZirErrorMessages(&wip_errors, &file);
-            var error_bundle = try wip_errors.toOwnedBundle();
+            var error_bundle = try wip_errors.toOwnedBundle("");
             defer error_bundle.deinit(gpa);
             error_bundle.renderToStdErr(renderOptions(fmt.color));
             fmt.any_error = true;
@@ -5018,7 +5014,7 @@ fn printAstErrorsToStderr(gpa: Allocator, tree: Ast, path: []const u8, color: Co
 
     try putAstErrorsIntoBundle(gpa, tree, path, &wip_errors);
 
-    var error_bundle = try wip_errors.toOwnedBundle();
+    var error_bundle = try wip_errors.toOwnedBundle("");
     defer error_bundle.deinit(gpa);
     error_bundle.renderToStdErr(renderOptions(color));
 }
@@ -5622,7 +5618,7 @@ pub fn cmdAstCheck(
         try wip_errors.init(gpa);
         defer wip_errors.deinit();
         try Compilation.addZirErrorMessages(&wip_errors, &file);
-        var error_bundle = try wip_errors.toOwnedBundle();
+        var error_bundle = try wip_errors.toOwnedBundle("");
         defer error_bundle.deinit(gpa);
         error_bundle.renderToStdErr(renderOptions(color));
         process.exit(1);
@@ -5739,7 +5735,7 @@ pub fn cmdChangelist(
         try wip_errors.init(gpa);
         defer wip_errors.deinit();
         try Compilation.addZirErrorMessages(&wip_errors, &file);
-        var error_bundle = try wip_errors.toOwnedBundle();
+        var error_bundle = try wip_errors.toOwnedBundle("");
         defer error_bundle.deinit(gpa);
         error_bundle.renderToStdErr(renderOptions(color));
         process.exit(1);
@@ -5774,7 +5770,7 @@ pub fn cmdChangelist(
         try wip_errors.init(gpa);
         defer wip_errors.deinit();
         try Compilation.addZirErrorMessages(&wip_errors, &file);
-        var error_bundle = try wip_errors.toOwnedBundle();
+        var error_bundle = try wip_errors.toOwnedBundle("");
         defer error_bundle.deinit(gpa);
         error_bundle.renderToStdErr(renderOptions(color));
         process.exit(1);
src/Sema.zig
@@ -2219,7 +2219,7 @@ fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
         wip_errors.init(gpa) catch unreachable;
         Compilation.addModuleErrorMsg(&wip_errors, err_msg.*) catch unreachable;
         std.debug.print("compile error during Sema:\n", .{});
-        var error_bundle = wip_errors.toOwnedBundle() catch unreachable;
+        var error_bundle = wip_errors.toOwnedBundle("") catch unreachable;
         error_bundle.renderToStdErr(.{ .ttyconf = .no_color });
         crash_report.compilerPanic("unexpected compile error occurred", null, null);
     }