zls icon indicating copy to clipboard operation
zls copied to clipboard

automatically empty zls.log file

Open Techatrix opened this issue 8 months ago • 4 comments

My zls.log file is 1.4 GiB 💀

ZLS should empty the file when it exits successfully.

Techatrix avatar Apr 30 '25 14:04 Techatrix

I've tried this patch out locally and it's working great.

diff --git a/src/main.zig b/src/main.zig
index a0af2c75..8fa08110 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -341,6 +341,7 @@ pub fn main() !u8 {
 
     const server = try zls.Server.create(allocator);
     defer server.destroy();
+    defer if (log_file_path) |path| if (server.status == .exiting_success) std.fs.deleteFileAbsolute(path) catch {};
     server.setTransport(transport.any());
     server.config_path = result.config_path;

The one thing I did notice is that if there are multiple instances of zls open and they share a log file, after one exits successfully all current and future logs for the remaining instances are lost. I'm not sure how this could be handled, but is it enough of an edge case that it's not worth worrying about?

WillLillis avatar May 02 '25 22:05 WillLillis

Instead of deleting the file, it's content should be removed:

defer if (log_file) |file| if (server.status == .exiting_success) file.setEndPos(0) catch {};

This should give better results when multiple ZLS processes are running.

It should also be checked whether this approach works across most editors. An editor could be terminating the ZLS process early so that it would never clear the log file.

Techatrix avatar May 03 '25 22:05 Techatrix

Instead of deleting the file, it's content should be removed:

defer if (log_file) |file| if (server.status == .exiting_success) file.setEndPos(0) catch {};

This should give better results when multiple ZLS processes are running.

That does indeed work a lot better with multiple ZLS processes running.

It should also be checked whether this approach works across most editors. An editor could be terminating the ZLS process early so that it would never clear the log file.

I've tested this with Zed, Helix, and Neovim.

  • Zed terminates the ZLS process before it has a chance to clear out the log file. I'll look into Zed's issue tracker later today and see if there's any info on this.
  • Helix allows ZLS to clear the log file, and it is empty after the editor process exits.
  • Neovim allows ZLS to clear the log file, but somehow one line remains in the log file:
debug ( diag ): zig build runner process has exited

WillLillis avatar May 11 '25 06:05 WillLillis

  • Neovim allows ZLS to clear the log file, but somehow one line remains in the log file:

This is probably because the logs are cleared before calling server.destroy(); which stops the build on save processes.

Techatrix avatar May 13 '25 21:05 Techatrix