zls icon indicating copy to clipboard operation
zls copied to clipboard

use reference trace to provide better build-on-save source locations for errors

Open Techatrix opened this issue 1 year ago • 1 comments

Example:

const std = @import("std");

pub fn main() !void {
    std.log.info("{}", .{""});
}

Output:

$ zig run foo.zig -freference-trace=12
/nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/fmt.zig:663:17: error: cannot format array without a specifier (i.e. {s} or {any})
                @compileError("cannot format array without a specifier (i.e. {s} or {any})");
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    formatType__anon_9361: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/fmt.zig:627:38
    format__anon_6673: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/fmt.zig:188:23
    print__anon_3923: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/io/Writer.zig:24:26
    defaultLog__anon_2963: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/io.zig:324:47
    log__anon_2562: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/log.zig:125:22
    info__anon_1614: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/log.zig:194:16
    main: foo.zig:4:17
    posixCallMainAndExit: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/start.zig:615:37
    _start: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/start.zig:422:40
    comptime: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/start.zig:92:63
    start: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/std.zig:101:27
    comptime: /nix/store/li7vdsrycv9zgp0phkxgsgn9fayn7cx0-zig-0.14.0-dev.1371+5723fcaac/lib/std/std.zig:160:9

This means that the error is reported in fmt.zig:663:17 instead of foo.zig:4:17. The reference trace could be used to figure out how the error was reached and report it at the callsite.

Techatrix avatar Aug 31 '24 17:08 Techatrix

Hey, I have a straightforward implementation for this here: https://github.com/davidrios/zls/commit/a7370d7640491db96c42b02bf88128ae78a20fb8 It shows like this:

Image

I don't know why it reports column as 1, the generated bundle has the correct column...

The basic idea is, when the build_runner is sending an error bundle via the transport, it also checks if there's a referenced by error that's outside stdlib, if there is it creates and sends an extra error bundle with that as the root message. It could also skip the original bundle and send just the based on the referenced by one.

davidrios avatar Jul 06 '25 12:07 davidrios