Is there a better to way read compiler errors for std.debug.print?

I’ve been learning zig and I’ve been using print debugging with std.debug.print to inspect and debug my small program.

What I’ve struggled with is understanding and reading compile errors like

install
└─ install c_compiler_in_zig
   └─ compile exe c_compiler_in_zig Debug native 2 errors
/home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/Io/Writer.zig:717:13: error: too few arguments
            @compileError("too few arguments");
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    defaultLogFileTerminal__anon_36438: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/log.zig:132:23
    defaultLog__anon_36431: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/log.zig:108:34
    8 reference(s) hidden; use '-freference-trace=10' to see all references
/home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/Io/Writer.zig:1379:21: error: cannot format slice without a specifier (i.e. {s}, {x}, {b64}, or {any})
                    @compileError("cannot format slice without a specifier (i.e. {s}, {x}, {b64}, or {any})");
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: 2 compilation errors
failed command: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/zig build-exe -ODebug -Mroot=/home/jr/dev/zig/c-compiler-in-zig/src/main.zig --cache-dir .zig-cache --global-cache-dir /home/jr/.cache/zig --name c_compiler_in_zig --zig-lib-dir /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/ --listen=-

Build Summary: 0/3 steps succeeded (1 failed)
install transitive failure
└─ install c_compiler_in_zig transitive failure
   └─ compile exe c_compiler_in_zig Debug native 2 errors

error: the following build command failed with exit code 1:
.zig-cache/o/73d0eff91161ae40a2dfff28e500ee38/build /home/jr/.local/bin/zig-x86_64-linux-0.16.0/zig /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib /home/jr/dev/zig/c-compiler-in-zig .zig-cache /home/jr/.cache/zig --seed 0xcf7a1437 -Z838168ed0f64d7b2

The first compile error I understood as it has too few arguments, but it would be nice if it referenced the call site where it had too few arguments.

Where my bug was were in these lines of code, where filename were surrounded by braces.

std.log.info(
    "usage: c_compiler_in_zig [--lex|--parse|--codegen] {filename} \n",
    .{},
);

I’m just wondering if there’s a better way to figure out where these bugs would be given the errors. Would it make sense to show in the stack trace where the error is in the source code?

There is no stack trace because there is no stack, these are compile errors, not runtime errors.

There is a reference trace, which is analogous, however most of it is hidden unless you pass -freference-trace=n, n being the amount to show per error, or you can omit the =n entirely to show all references.

It’s a little annoying when this happens, but the compiler does tell you how to fix it:

8 reference(s) hidden; use '-freference-trace=10' to see all references

The reason the default=2 should then become obvious; most of it is irrelevant to your problem. The vast majority of errors will mention show the offending code in the first or second trace.

That’s really useful. Thank you.

Thank you this is it!

Looking at the trace again I can see that there is reference to -freference-trace=10 that I didn’t read through properly. I can now see where it’s referenced in my code :slight_smile:

$ zig build -freference-trace

install
└─ install c_compiler_in_zig
   └─ compile exe c_compiler_in_zig Debug native 1 errors
/home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/Io/Writer.zig:717:13: error: too few arguments
            @compileError("too few arguments");
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    defaultLogFileTerminal__anon_36432: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/log.zig:132:23
    defaultLog__anon_36425: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/log.zig:108:34
    log__anon_36404: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/log.zig:72:22
    info__anon_32829: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/log.zig:166:16
    main: src/main.zig:21:21
    callMain [inlined]: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/start.zig:737:30
    callMainWithArgs [inlined]: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/start.zig:638:20
    posixCallMainAndExit: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/start.zig:590:38
    _start: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/start.zig:469:40
    comptime: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/start.zig:69:67
    start: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/std.zig:107:27
    comptime: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/std/std.zig:233:9
error: 1 compilation errors
failed command: /home/jr/.local/bin/zig-x86_64-linux-0.16.0/zig build-exe -freference-trace=256 -ODebug -Mroot=/home/jr/dev/zig/c-compiler-in-zig/src/main.zig --cache-dir .zig-cache --global-cache-dir /home/jr/.cache/zig --name c_compiler_in_zig --zig-lib-dir /home/jr/.local/bin/zig-x86_64-linux-0.16.0/lib/ --listen=-