If you own a platform and want even better debug messages, I just changed debug to take an extra arg that is the actual source of the debugged expression. Just add the extra arg and print it in a pretty way. Here is an example in rust and zig:
Requires using the latest version of the compiler (should be in the next nightly).
Can get awesome dbg messages like:
[day3.roc:18] (part1 p1Sample) = 4361
#[no_mangle]
pub unsafe extern "C" fn roc_dbg(loc: *mut RocStr, msg: *mut RocStr, src: *mut RocStr) {
eprintln!("[{}] {} = {}", &*loc, &*src, &*msg);
}
export fn roc_dbg(loc: *RocStr, msg: *RocStr, src: *RocStr) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
stderr.print("[{s}] {s} = {s}\n", .{ loc.asSlice(), src.asSlice(), msg.asSlice() }) catch unreachable;
}
If you don't update roc_dbg, everything should be backwards compatible and still just work the same. Your function will just be missing and ignore the third arg.
Thank you, that is nice.
Could it be, that there is a off-by-one error? It does not show the line of the dbg statement, but one line above it. For example, if I put dbg on line 10, the output shows line 9.
That's definitely possible, though it had seemed to be working on my testing (at least in terms of the statements printed). I guess I never checked the actual line number and if it is zero indexed or not.
@Oskar Hahn do you have more details or a repro file?
Brendan Hansknecht said:
Oskar Hahn do you have more details or a repro file?
I already updated my platform. You can add the dbg statement anywhere here https://github.com/ostcar/aoc2023/blob/main/platform/host.zig
Here is an example: https://github.com/ostcar/aoc2023/blob/dbg_bug/days/dayXX.roc
roc days/dayXX.roc
🔨 Rebuilding platform...
[days/dayXX.roc:13] "this is on line 14" = "this is on line 14"
Part1 in 58.171us:
Not implemented yet
Part2 in 1.956us:
Not implemented yet
Ok. So yeah just line numbers being zero index in roc. Need to add 1 to them. Thanks
And why is the string being printed two times? Here is the platform code: https://github.com/ostcar/aoc2023/blob/33b645286345c79ea2ae9e757ddb931d09fef475/platform/host.zig#L69
Ah. Because I did not use a variable
Yeah, it prints the source code and the output.
Strange when debuging with a constant, but really awesome the rest of the time.
For example, you can get output like:
[path/my-file.roc:12] (List.len out) = 7
The case, where you just debug a constant was easy to fix in the platform: https://github.com/ostcar/aoc2023/blob/f657d50aa2b393b5dc26b49baeb6f740ef405908/platform/host.zig#L67-L74
But I don't know if this is really necessary. It is not the use case of a debugger to print a constant
yep. Up to each platform if they want to do that or not
As an extra note, if you are using dbg and notice weird type issues (probably with number types) that appear when you add a debug statement, please update to the lastest main or wait for the next nightly. Just got a bug fix in.
Fresh nightlies are up
Last updated: Jul 26 2025 at 12:14 UTC