zigtools / zls

A Zig language server supporting Zig developers with features like autocomplete and goto definition
MIT License
2.59k stars 273 forks source link

Missing Diagnostics #1659

Open XeroOl opened 6 months ago

XeroOl commented 6 months ago

Zig Version

0.11.0

Zig Language Server Version

0.11.0

Steps to Reproduce

Open a file with the contents:

const std = @import("std");

pub fn main() !void {
    std.debug.print("All your {s} are belong to us.\n", .{});
}

Note that zls does not emit any warning or error diagnostic describing any problem.

Then, run zig build, and note that zig build does produce an error describing a problem.

Expected Behavior

I would expect that zls can detect compile errors, because zls uses zig to analyze the code. More precisely, I would expect one of these two cases: 1) zls produces no error diagnostics, and zig build compiles the code 2) zls produces error diagnostics, and zig build fails to compile the code

Actual Behavior

With zls running in my editor, no diagnostic is produced, but zig build fails to build the code.

This is incredibly annoying because it means I have to repeatedly switch between the terminal and the editor, and search for line numbers, instead of being able to just see the problems in my code.

There are many many more reproduction cases where zig build is able to find problems that zls seems oblivious to.

Techatrix commented 6 months ago

ZLS does not use Zig to perform code analysis (semantic analysis to be specific). Integrating semantic analysis into ZLS is a large and complex task. Related issues: #551 #552 https://github.com/ziglang/zig/issues/615

There is a relatively new enable_build_on_save option in ZLS master that allows you to get diagnostics that would are generated by building the project with zig build.

WillLillis commented 2 weeks ago

I was kinda curious why this error wasn't showing up even with enable_build_on_save turned on. It looks like the issue is how the error is reported by the zig compiler. The error diagnostic is published by zls, but it won't show up inline for the file where you're calling std.debug.print.

image

but rather where @compileError() is hit:

image

It looks like any error reported with @compileError will behave this way. I thought that maybe there could be some alternative way of parsing the source location in these cases to work around it, but (unless I'm missing something) the source of the error isn't reported anywhere in the output from zig build (even with -freference-trace.:

❯ zig build -freference-trace                                                                            ⏎
install
└─ install test_zls
   └─ zig build-exe test_zls Debug native 1 errors
/home/lillis/zig/lib/std/fmt.zig:183:13: error: too few arguments
            @compileError("too few arguments");
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    print__anon_3530: /home/lillis/zig/lib/std/io/Writer.zig:24:26
    print: /home/lillis/zig/lib/std/io.zig:324:47
    dumpCurrentStackTrace: /home/lillis/zig/lib/std/debug.zig:198:19
    unexpectedErrno: /home/lillis/zig/lib/std/posix.zig:7324:18
    raise: /home/lillis/zig/lib/std/posix.zig:716:34
    abort: /home/lillis/zig/lib/std/posix.zig:684:9
    handleSegfaultPosix: /home/lillis/zig/lib/std/debug.zig:2624:10
    attachSegfaultHandler: /home/lillis/zig/lib/std/debug.zig:2558:36
    maybeEnableSegfaultHandler: /home/lillis/zig/lib/std/debug.zig:2535:18
    callMainWithArgs: /home/lillis/zig/lib/std/start.zig:479:14
    posixCallMainAndExit: /home/lillis/zig/lib/std/start.zig:438:20
    _start: /home/lillis/zig/lib/std/start.zig:351:40
error: the following command failed with 1 compilation errors:
/home/lillis/zig/build/stage3/bin/zig build-exe -freference-trace=256 -ODebug -Mroot=/home/lillis/projects/test_zls/src/main.zig --cache-dir /home/lillis/projects/test_zls/.zig-cache --global-cache-dir /home/lillis/.cache/zig --name test_zls --listen=-
Build Summary: 2/5 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install test_zls transitive failure
   └─ zig build-exe test_zls Debug native 1 errors
error: the following build command failed with exit code 1:
/home/lillis/projects/test_zls/.zig-cache/o/169ef625c4b03d73e3a2b14672ed13c1/build /home/lillis/zig/build/stage3/bin/zig /home/lillis/projects/test_zls /home/lillis/projects/test_zls/.zig-cache /home/lillis/.cache/zig --seed 0x5b844879 -Z977f7c67defdb21c -freference-trace

Maybe it would be worth looking into how something could be changed upstream to include the actual source location for the error? I'd love to look into this but I'm still pretty useless when it comes tot zig internals.

Techatrix commented 2 weeks ago

I recall having a conversation with someone about this exact issue. I also had the idea of using the reference trace to report a diagnostic at the call-site of std.debug.print but this bug in Zig made it not implementable. This has been reported in https://github.com/ziglang/zig/issues/19158. There are also a few other issues that track similar issues.