authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2022-12-14 20:01:11-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-15 03:49:26-05:00
log88d927a511922efd82c4ec862c8e2aa83df84391
tree16449de5465989e1e3f8ae7894b5544cd9491109
parent83e0e23f8aa2d90495bbf9c3649aa68f7c55c926

std.debug.TTY: Fix colors not resetting on Windows

This fixes a regression introduced in #12298 where colors would never reset in a Windows console because the attributes would be queried on every `setColor` call, and then try to 'reset' the attributes to what it just queried (i.e. it was essentially doing a complicated no-op on .Reset). This fixes the problem while (I think) keeping with the spirit of the changes in #12298--that is, `TTY.Config` is not specifically tied to stderr like it was before #12298. To that end, detectTTYConfig now takes a `File` and that's what gets used to query the initial attributes to reset to. (for context, before #12298, the first `setColor` call is where the reset attributes would get queried and it would always use stderr to do it)

4 files changed, 35 insertions(+), 26 deletions(-)

lib/std/builtin.zig+1-1
......@@ -51,7 +51,7 @@ pub const StackTrace = struct {
5151 const debug_info = std.debug.getSelfDebugInfo() catch |err| {
5252 return writer.print("\nUnable to print stack trace: Unable to open debug info: {s}\n", .{@errorName(err)});
5353 };
54 const tty_config = std.debug.detectTTYConfig();
54 const tty_config = std.debug.detectTTYConfig(std.io.getStdErr());
5555 try writer.writeAll("\n");
5656 std.debug.writeStackTrace(self, writer, arena.allocator(), debug_info, tty_config) catch |err| {
5757 try writer.print("Unable to print stack trace: {s}\n", .{@errorName(err)});
lib/std/debug.zig+26-17
......@@ -109,17 +109,24 @@ pub fn getSelfDebugInfo() !*DebugInfo {
109109 }
110110}
111111
112pub fn detectTTYConfig() TTY.Config {
112pub fn detectTTYConfig(file: std.fs.File) TTY.Config {
113113 if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
114114 return .escape_codes;
115115 } else if (process.hasEnvVarConstant("NO_COLOR")) {
116116 return .no_color;
117117 } else {
118 const stderr_file = io.getStdErr();
119 if (stderr_file.supportsAnsiEscapeCodes()) {
118 if (file.supportsAnsiEscapeCodes()) {
120119 return .escape_codes;
121 } else if (native_os == .windows and stderr_file.isTty()) {
122 return .{ .windows_api = stderr_file };
120 } else if (native_os == .windows and file.isTty()) {
121 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
122 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) {
123 // TODO: Should this return an error instead?
124 return .no_color;
125 }
126 return .{ .windows_api = .{
127 .handle = file.handle,
128 .reset_attributes = info.wAttributes,
129 } };
123130 } else {
124131 return .no_color;
125132 }
......@@ -146,7 +153,7 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
146153 stderr.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)}) catch return;
147154 return;
148155 };
149 writeCurrentStackTrace(stderr, debug_info, detectTTYConfig(), start_addr) catch |err| {
156 writeCurrentStackTrace(stderr, debug_info, detectTTYConfig(io.getStdErr()), start_addr) catch |err| {
150157 stderr.print("Unable to dump stack trace: {s}\n", .{@errorName(err)}) catch return;
151158 return;
152159 };
......@@ -174,7 +181,7 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
174181 stderr.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)}) catch return;
175182 return;
176183 };
177 const tty_config = detectTTYConfig();
184 const tty_config = detectTTYConfig(io.getStdErr());
178185 printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return;
179186 var it = StackIterator.init(null, bp);
180187 while (it.next()) |return_address| {
......@@ -257,7 +264,7 @@ pub fn dumpStackTrace(stack_trace: std.builtin.StackTrace) void {
257264 stderr.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)}) catch return;
258265 return;
259266 };
260 writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, detectTTYConfig()) catch |err| {
267 writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, detectTTYConfig(io.getStdErr())) catch |err| {
261268 stderr.print("Unable to dump stack trace: {s}\n", .{@errorName(err)}) catch return;
262269 return;
263270 };
......@@ -600,7 +607,12 @@ pub const TTY = struct {
600607 pub const Config = union(enum) {
601608 no_color,
602609 escape_codes,
603 windows_api: File,
610 windows_api: if (native_os == .windows) WindowsContext else void,
611
612 pub const WindowsContext = struct {
613 handle: File.Handle,
614 reset_attributes: u16,
615 };
604616
605617 pub fn setColor(conf: Config, out_stream: anytype, color: Color) !void {
606618 nosuspend switch (conf) {
......@@ -617,19 +629,16 @@ pub const TTY = struct {
617629 };
618630 try out_stream.writeAll(color_string);
619631 },
620 .windows_api => |file| if (native_os == .windows) {
621 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
622 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE)
623 return error.FailedRetrievingTerminalInfo;
632 .windows_api => |ctx| if (native_os == .windows) {
624633 const attributes = switch (color) {
625634 .Red => windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY,
626635 .Green => windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY,
627636 .Cyan => windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY,
628637 .White, .Bold => windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY,
629638 .Dim => windows.FOREGROUND_INTENSITY,
630 .Reset => info.wAttributes,
639 .Reset => ctx.reset_attributes,
631640 };
632 try windows.SetConsoleTextAttribute(file.handle, attributes);
641 try windows.SetConsoleTextAttribute(ctx.handle, attributes);
633642 } else {
634643 unreachable;
635644 },
......@@ -2005,7 +2014,7 @@ test "#4353: std.debug should manage resources correctly" {
20052014 const writer = std.io.null_writer;
20062015 var di = try openSelfDebugInfo(testing.allocator);
20072016 defer di.deinit();
2008 try printSourceAtAddress(&di, writer, showMyTrace(), detectTTYConfig());
2017 try printSourceAtAddress(&di, writer, showMyTrace(), detectTTYConfig(std.io.getStdErr()));
20092018}
20102019
20112020noinline fn showMyTrace() usize {
......@@ -2065,7 +2074,7 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize
20652074 pub fn dump(t: @This()) void {
20662075 if (!enabled) return;
20672076
2068 const tty_config = detectTTYConfig();
2077 const tty_config = detectTTYConfig(std.io.getStdErr());
20692078 const stderr = io.getStdErr().writer();
20702079 const end = @min(t.index, size);
20712080 const debug_info = getSelfDebugInfo() catch |err| {
lib/std/testing.zig+1-1
......@@ -312,7 +312,7 @@ pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const
312312 const actual_window = actual[window_start..@min(actual.len, window_start + max_window_size)];
313313 const actual_truncated = window_start + actual_window.len < actual.len;
314314
315 const ttyconf = std.debug.detectTTYConfig();
315 const ttyconf = std.debug.detectTTYConfig(std.io.getStdErr());
316316 var differ = if (T == u8) BytesDiffer{
317317 .expected = expected_window,
318318 .actual = actual_window,
src/main.zig+7-7
......@@ -3431,7 +3431,7 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void
34313431
34323432 if (errors.list.len != 0) {
34333433 const ttyconf: std.debug.TTY.Config = switch (comp.color) {
3434 .auto => std.debug.detectTTYConfig(),
3434 .auto => std.debug.detectTTYConfig(std.io.getStdErr()),
34353435 .on => .escape_codes,
34363436 .off => .no_color,
34373437 };
......@@ -4252,7 +4252,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
42524252
42534253 try Compilation.AllErrors.addZir(arena_instance.allocator(), &errors, &file);
42544254 const ttyconf: std.debug.TTY.Config = switch (color) {
4255 .auto => std.debug.detectTTYConfig(),
4255 .auto => std.debug.detectTTYConfig(std.io.getStdErr()),
42564256 .on => .escape_codes,
42574257 .off => .no_color,
42584258 };
......@@ -4465,7 +4465,7 @@ fn fmtPathFile(
44654465
44664466 try Compilation.AllErrors.addZir(arena_instance.allocator(), &errors, &file);
44674467 const ttyconf: std.debug.TTY.Config = switch (fmt.color) {
4468 .auto => std.debug.detectTTYConfig(),
4468 .auto => std.debug.detectTTYConfig(std.io.getStdErr()),
44694469 .on => .escape_codes,
44704470 .off => .no_color,
44714471 };
......@@ -4586,7 +4586,7 @@ fn printErrsMsgToStdErr(
45864586 };
45874587
45884588 const ttyconf: std.debug.TTY.Config = switch (color) {
4589 .auto => std.debug.detectTTYConfig(),
4589 .auto => std.debug.detectTTYConfig(std.io.getStdErr()),
45904590 .on => .escape_codes,
45914591 .off => .no_color,
45924592 };
......@@ -5176,7 +5176,7 @@ pub fn cmdAstCheck(
51765176 var errors = std.ArrayList(Compilation.AllErrors.Message).init(arena);
51775177 try Compilation.AllErrors.addZir(arena, &errors, &file);
51785178 const ttyconf: std.debug.TTY.Config = switch (color) {
5179 .auto => std.debug.detectTTYConfig(),
5179 .auto => std.debug.detectTTYConfig(std.io.getStdErr()),
51805180 .on => .escape_codes,
51815181 .off => .no_color,
51825182 };
......@@ -5301,7 +5301,7 @@ pub fn cmdChangelist(
53015301 if (file.zir.hasCompileErrors()) {
53025302 var errors = std.ArrayList(Compilation.AllErrors.Message).init(arena);
53035303 try Compilation.AllErrors.addZir(arena, &errors, &file);
5304 const ttyconf = std.debug.detectTTYConfig();
5304 const ttyconf = std.debug.detectTTYConfig(std.io.getStdErr());
53055305 for (errors.items) |full_err_msg| {
53065306 full_err_msg.renderToStdErr(ttyconf);
53075307 }
......@@ -5340,7 +5340,7 @@ pub fn cmdChangelist(
53405340 if (file.zir.hasCompileErrors()) {
53415341 var errors = std.ArrayList(Compilation.AllErrors.Message).init(arena);
53425342 try Compilation.AllErrors.addZir(arena, &errors, &file);
5343 const ttyconf = std.debug.detectTTYConfig();
5343 const ttyconf = std.debug.detectTTYConfig(std.io.getStdErr());
53445344 for (errors.items) |full_err_msg| {
53455345 full_err_msg.renderToStdErr(ttyconf);
53465346 }