Improve debug I/O color detection/handling for WASI and Emscripten

When linking libc, these targets can get their environment variables
from `std.c.environ`. Additionally, it's okay for WASI to use ANSI
escape sequences; nothing in the relevant specs claim otherwise.
This commit is contained in:
Carl Åstholm
2026-02-02 01:24:39 +01:00
committed by Andrew Kelley
parent 3d48264365
commit 3223d3a1ac
3 changed files with 16 additions and 11 deletions
-7
View File
@@ -8729,13 +8729,6 @@ fn supportsAnsiEscapeCodes(file: File) Io.Cancelable!bool {
}
}
if (native_os == .wasi) {
// WASI sanitizes stdout when fd is a tty so ANSI escape codes will not
// be interpreted as actual cursor commands, and stderr is always
// sanitized.
return false;
}
if (try isTty(file)) return true;
return false;
+1 -1
View File
@@ -24,7 +24,7 @@ pub const empty: Environ = .{ .block = .empty };
/// operating system `void` is also used.
pub const Block = switch (native_os) {
.windows => GlobalBlock,
.wasi => switch (builtin.link_libc) {
.wasi, .emscripten => switch (builtin.link_libc) {
false => GlobalBlock,
true => PosixBlock,
},
+15 -3
View File
@@ -664,10 +664,22 @@ fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) cal
fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.c) c_int {
const argv = @as([*][*:0]u8, @ptrCast(c_argv))[0..@intCast(c_argc)];
if (@sizeOf(std.Io.Threaded.Argv0) != 0) {
if (std.Options.debug_threaded_io) |t| t.argv0.value = argv[0];
const environ: [:null]?[*:0]u8 = switch (builtin.os.tag) {
.wasi, .emscripten => environ: {
const c_environ = std.c.environ;
var env_count: usize = 0;
while (c_environ[env_count] != null) : (env_count += 1) {}
break :environ c_environ[0..env_count :null];
},
else => &.{},
};
const env_block: std.process.Environ.Block = .{ .slice = environ };
if (std.Options.debug_threaded_io) |t| {
if (@sizeOf(std.Io.Threaded.Argv0) != 0) t.argv0.value = argv[0];
t.environ = .{ .process_environ = .{ .block = env_block } };
t.environ_initialized = env_block.isEmpty();
}
return callMain(argv, .empty);
return callMain(argv, env_block);
}
/// General error message for a malformed return type