From b36f7c4be2bd0862b4d572aa63ddb29f9ea88fce Mon Sep 17 00:00:00 2001 From: Techatrix Date: Mon, 30 Dec 2024 08:07:20 +0100 Subject: [PATCH] remove diagnostics when stopping build on save --- src/DiagnosticsCollection.zig | 23 +++++++++++++++++++++++ src/features/diagnostics.zig | 10 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/DiagnosticsCollection.zig b/src/DiagnosticsCollection.zig index 073c19a8c..b23c2b690 100644 --- a/src/DiagnosticsCollection.zig +++ b/src/DiagnosticsCollection.zig @@ -168,6 +168,29 @@ pub fn pushErrorBundle( } } +pub fn clearErrorBundle(collection: *DiagnosticsCollection, tag: Tag) void { + collection.mutex.lock(); + defer collection.mutex.unlock(); + + const item = collection.tag_set.getPtr(tag) orelse return; + + collectUrisFromErrorBundle( + collection.allocator, + item.error_bundle, + item.error_bundle_src_base_path, + &collection.outdated_files, + ) catch |err| switch (err) { + error.OutOfMemory => return, + }; + + if (item.error_bundle_src_base_path) |base_path| { + collection.allocator.free(base_path); + item.error_bundle_src_base_path = null; + } + item.error_bundle.deinit(collection.allocator); + item.error_bundle = .empty; +} + fn collectUrisFromErrorBundle( allocator: std.mem.Allocator, error_bundle: std.zig.ErrorBundle, diff --git a/src/features/diagnostics.zig b/src/features/diagnostics.zig index a3eb80742..c375e92e3 100644 --- a/src/features/diagnostics.zig +++ b/src/features/diagnostics.zig @@ -551,6 +551,12 @@ pub const BuildOnSave = struct { }); defer transport.deinit(); + var diagnostic_tags: std.AutoArrayHashMapUnmanaged(DiagnosticsCollection.Tag, void) = .empty; + defer { + for (diagnostic_tags.keys()) |tag| collection.clearErrorBundle(tag); + collection.publishDiagnostics() catch {}; + } + while (true) { const header = transport.receiveMessage(null) catch |err| switch (err) { error.EndOfStream => { @@ -570,6 +576,7 @@ pub const BuildOnSave = struct { &transport, collection, workspace_path, + &diagnostic_tags, ) catch |err| { log.err("failed to handle error bundle message from zig build runner: {}", .{err}); return; @@ -587,6 +594,7 @@ pub const BuildOnSave = struct { transport: *Transport, collection: *DiagnosticsCollection, workspace_path: []const u8, + diagnostic_tags: *std.AutoArrayHashMapUnmanaged(DiagnosticsCollection.Tag, void), ) !void { const header = try transport.reader().readStructEndian(ServerToClient.ErrorBundle, .little); @@ -604,6 +612,8 @@ pub const BuildOnSave = struct { const diagnostic_tag: DiagnosticsCollection.Tag = @enumFromInt(@as(u32, @truncate(hasher.final()))); + try diagnostic_tags.put(self.allocator, diagnostic_tag, {}); + try collection.pushErrorBundle(diagnostic_tag, header.cycle, workspace_path, error_bundle); try collection.publishDiagnostics(); }