FalsePattern / ZigBrains

The zig language plugin for intellij
https://plugins.jetbrains.com/plugin/22456-zigbrains
Other
115 stars 7 forks source link

Debugging has different behavior than running #68

Open BucketOfNubbins opened 3 hours ago

BucketOfNubbins commented 3 hours ago

I have a simple program to take input from the user, then print what the user typed right back out. Along with the length of the user's input.

I am using CLion. My build.zig file is the standard auto-generated one. My build configuration is normal except I added the "run" step to it in order to run my code.

const std = @import("std");

pub fn main() !void {
    var br = std.io.bufferedReader(std.io.getStdIn().reader());
    var bw = std.io.bufferedWriter(std.io.getStdOut().writer());
    const in = br.reader();
    const out = bw.writer();

    defer bw.flush() catch {};

    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();

    const alloc = arena.allocator();

    var input = std.ArrayList(u8).init(alloc);

    try in.readUntilDelimiterArrayList(&input, '\n', std.math.maxInt(usize));

    try out.print("{s}\n", .{input.items});
    try out.print("{}\n", .{input.items.len});

}

test "simple test" { 
    var list = std.ArrayList(i32).init(std.testing.allocator);
    defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
    try list.append(42);
    try std.testing.expectEqual(@as(i32, 42), list.pop());
}

When running the program, it works as expected.

run_output

However stepping through the same program in the debugger, I get a different output.

debug_output

Here is a picture of some of the variables in the middle of a debug run.

mid_debug

It seems that the debugger is replacing the windows "\n" with the mac "\r", as well as printing an extra hello somewhere.

Below is the output of another debug run when I changed the code to look for "\r" instead of "\n"

debug_mac_end_line

I also did a normal run where the code is looking for "\r" as the delimiter, and signaled end of file with ctrl-d which resulted in the following (expected) error.

run_mac_end_line

I also have a different, but similar, project that can't debug at all as it just hangs on trying to connect to the debugger. However I don't know the steps to reproduce yet as this bug distracted me first.

BucketOfNubbins commented 2 hours ago

I forgot to add the version of Zig I have installed 0.14.0-dev.1930+4a2a0f50c