Skip to content

Commit f486e88

Browse files
committed
show compile log output in build on save diagnostics
1 parent 86f8a8b commit f486e88

File tree

1 file changed

+64
-22
lines changed

1 file changed

+64
-22
lines changed

src/DiagnosticsCollection.zig

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ pub fn pushErrorBundle(
162162
}
163163
}
164164

165-
var owned_error_bundle = try new_error_bundle.toOwnedBundle("");
165+
const compile_log_text = if (error_bundle.errorMessageCount() == 0) "" else error_bundle.getCompileLogOutput();
166+
167+
var owned_error_bundle = try new_error_bundle.toOwnedBundle(compile_log_text);
166168
errdefer owned_error_bundle.deinit(collection.allocator);
167169

168170
const duped_error_bundle_src_base_path = if (src_base_path) |base_path| try collection.allocator.dupe(u8, base_path) else null;
@@ -366,17 +368,20 @@ fn convertErrorBundleToLSPDiangostics(
366368

367369
var tags: std.ArrayList(lsp.types.DiagnosticTag) = .empty;
368370

369-
const diag_msg = eb.nullTerminatedString(err.msg);
371+
var message: []const u8 = eb.nullTerminatedString(err.msg);
370372

371-
if (std.mem.startsWith(u8, diag_msg, "unused ")) {
373+
if (std.mem.startsWith(u8, message, "unused ")) {
372374
try tags.append(arena, lsp.types.DiagnosticTag.Unnecessary);
373375
}
376+
if (std.mem.eql(u8, message, "found compile log statement")) {
377+
message = try std.fmt.allocPrint(arena, "{s}\n\nCompile Log Output:\n{s}", .{ message, eb.getCompileLogOutput() });
378+
}
374379

375380
try diagnostics.append(arena, .{
376381
.range = src_range,
377382
.severity = .Error,
378383
.source = "zls",
379-
.message = eb.nullTerminatedString(err.msg),
384+
.message = message,
380385
.tags = if (tags.items.len != 0) tags.items else null,
381386
.relatedInformation = relatedInformation,
382387
});
@@ -438,7 +443,7 @@ test errorBundleSourceLocationToRange {
438443
.source_line = null,
439444
},
440445
},
441-
});
446+
}, "");
442447
defer eb.deinit(std.testing.allocator);
443448

444449
const src_loc0 = eb.getSourceLocation(eb.getErrorMessage(eb.getMessages()[0]).src_loc);
@@ -466,11 +471,11 @@ test DiagnosticsCollection {
466471

467472
try std.testing.expectEqual(0, collection.outdated_files.count());
468473

469-
var eb1 = try createTestingErrorBundle(&.{.{ .message = "Living For The City" }});
474+
var eb1 = try createTestingErrorBundle(&.{.{ .message = "Living For The City" }}, "");
470475
defer eb1.deinit(std.testing.allocator);
471-
var eb2 = try createTestingErrorBundle(&.{.{ .message = "You Haven't Done Nothin'" }});
476+
var eb2 = try createTestingErrorBundle(&.{.{ .message = "You Haven't Done Nothin'" }}, "");
472477
defer eb2.deinit(std.testing.allocator);
473-
var eb3 = try createTestingErrorBundle(&.{.{ .message = "As" }});
478+
var eb3 = try createTestingErrorBundle(&.{.{ .message = "As" }}, "");
474479
defer eb3.deinit(std.testing.allocator);
475480

476481
const uri = try URI.fromPath(std.testing.allocator, testing_src_path);
@@ -532,24 +537,61 @@ test DiagnosticsCollection {
532537
}
533538
}
534539

540+
test "DiagnosticsCollection - compile_log_text" {
541+
var collection: DiagnosticsCollection = .{ .allocator = std.testing.allocator };
542+
defer collection.deinit();
543+
544+
var eb = try createTestingErrorBundle(&.{.{ .message = "found compile log statement" }}, "@as(comptime_int, 7)\n@as(comptime_int, 13)");
545+
defer eb.deinit(std.testing.allocator);
546+
547+
const uri = try URI.fromPath(std.testing.allocator, testing_src_path);
548+
defer std.testing.allocator.free(uri);
549+
550+
try collection.pushErrorBundle(.parse, 1, null, eb);
551+
try std.testing.expectEqual(1, collection.outdated_files.count());
552+
try std.testing.expectEqualStrings(uri, collection.outdated_files.keys()[0]);
553+
554+
var arena_allocator: std.heap.ArenaAllocator = .init(std.testing.allocator);
555+
defer arena_allocator.deinit();
556+
557+
const arena = arena_allocator.allocator();
558+
559+
var diagnostics: std.ArrayListUnmanaged(lsp.types.Diagnostic) = .empty;
560+
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);
561+
562+
try std.testing.expectEqual(1, diagnostics.items.len);
563+
try std.testing.expectEqual(lsp.types.DiagnosticSeverity.Error, diagnostics.items[0].severity);
564+
try std.testing.expectEqualStrings(
565+
\\found compile log statement
566+
\\
567+
\\Compile Log Output:
568+
\\@as(comptime_int, 7)
569+
\\@as(comptime_int, 13)
570+
, diagnostics.items[0].message);
571+
try std.testing.expectEqual(null, diagnostics.items[0].relatedInformation);
572+
}
573+
535574
const testing_src_path = switch (@import("builtin").os.tag) {
536575
.windows => "C:\\sample.zig",
537576
else => "/sample.zig",
538577
};
539578

540-
fn createTestingErrorBundle(messages: []const struct {
541-
message: []const u8,
542-
count: u32 = 1,
543-
source_location: struct {
544-
src_path: []const u8,
545-
line: u32,
546-
column: u32,
547-
span_start: u32,
548-
span_main: u32,
549-
span_end: u32,
550-
source_line: ?[]const u8,
551-
} = .{ .src_path = testing_src_path, .line = 0, .column = 0, .span_start = 0, .span_main = 0, .span_end = 0, .source_line = "" },
552-
}) error{OutOfMemory}!std.zig.ErrorBundle {
579+
fn createTestingErrorBundle(
580+
messages: []const struct {
581+
message: []const u8,
582+
count: u32 = 1,
583+
source_location: struct {
584+
src_path: []const u8,
585+
line: u32,
586+
column: u32,
587+
span_start: u32,
588+
span_main: u32,
589+
span_end: u32,
590+
source_line: ?[]const u8,
591+
} = .{ .src_path = testing_src_path, .line = 0, .column = 0, .span_start = 0, .span_main = 0, .span_end = 0, .source_line = "" },
592+
},
593+
compile_log_text: []const u8,
594+
) error{OutOfMemory}!std.zig.ErrorBundle {
553595
var eb: std.zig.ErrorBundle.Wip = undefined;
554596
try eb.init(std.testing.allocator);
555597
errdefer eb.deinit();
@@ -570,5 +612,5 @@ fn createTestingErrorBundle(messages: []const struct {
570612
});
571613
}
572614

573-
return eb.toOwnedBundle("");
615+
return eb.toOwnedBundle(compile_log_text);
574616
}

0 commit comments

Comments
 (0)