Printing bug overwrites Zig output message

I’m on Zig version 0.13.0-dev.351+64ef45eb0

Looks like I’m getting a small bug here when I’m try to print to the console (happens with std.log.info too). Here’s a minimal example to recreate it:

const ValueEnum = enum(u2) {
    r16 = 0,
    r32 = 1,
    r64 = 2,    
};

const Value = union(ValueEnum) {
    r16: f16, 
    r32: f32,    
    r64: f64,
    pub fn type_id(self: Value) usize {
        return @intFromEnum(std.meta.activeTag(self));
    }
};

pub fn main() !void {
    const x: Value = .{ .r32 = 2.0 };
    std.debug.print("ID - {}\n", .{ x.type_id() });
}

It first begins printing:

LLVM Emit Object

And eventually overwrites part of that message with:

ID - 1mit Object

Which is the correct value, but it’s not flushing the prior text? Anyone else getting this issue?

I have the same issue, it seems the build system leaves that last thing printed to the terminal intact and ends with a character return \r. So the next thing you print simply overrides what’s there. When you invoke the binary directly via ./zig-out/bin/executable it won’t show up, it’s just an issue with ‘zig build’ leaving garbage in your terminal.

You can print a newline at the top of your program if you wish to avoid it. Otherwise it needs a fix in the zig build system (I’m guessing).

2 Likes

it does this so it can overwrite the line repeatedly (update it essentially).

Depending on your system: zig build | cat might fix that since it should detect a pipe instead of a tty and change its formatting to be more basic.

edit this does not work - i’d consider it a bug since programs to be good citizens are supposed to detect if they are going to a tty or not.

wtf: zig appears to write to stderr, but when I redirect stderr to stdout and dump stdout to a file, i get nothing. what am i doing wrong?

> make clean; zig build test > o 2>&1
> ll o
-rw-r--r-- 1 jason jason 0 Jun  2 15:08 o

I get nothing when I redirect:

> make clean; zig build test 2> o2 1> o1
rm -rf zig-out zig-cache *.a *.a.o
> ll o*
-rw-r--r-- 1 jason jason 0 Jun  2 15:12 o1
-rw-r--r-- 1 jason jason 0 Jun  2 15:12 o2

I know I definitely had this bug in the past but now that I’m trying the latest 13-dev release I can’t recreate it. It seems the new progress bar for the build system may have fixed this (on windows 10 fwiw).

Interesting - I’m running it on Ubuntu. Your explanation makes sense though. I’ve been having issues with printing stuff to console ever since I switched to 13.

Come to think of it, I think you’re right - it’s always when doing a command like: zig run main.zig but running a binary directly doesn’t have the same issue.

The most recent released version of zig is 0.12.0 and the tip of master branch is currently 0.13.0-dev.354+0c2cd8381. There has not yet been a 13.0 or 0.13.0 release.

Yeah, I should specify the full version - here’s what I’m running:

andrew@Machine:~/ZigCode$ zig version
0.13.0-dev.351+64ef45eb0

That’s where I’m currently finding the issue.

Thanks - are you using zig run? Because I just fixed that 3 commits after the version you’re on.

Indeed I am - good to know!