From 6faeef6dc2e072eeb18d06075be69596b8736cec Mon Sep 17 00:00:00 2001 From: dave caruso Date: Fri, 26 Jan 2024 20:55:48 -0800 Subject: [PATCH] Workaround https://github.com/microsoft/terminal/issues/16606 (#8506) Fixes #8504 --- src/bun.js/webcore.zig | 3 +-- src/bun.zig | 6 ++++++ src/cli/init_command.zig | 2 +- src/main.zig | 2 ++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/bun.js/webcore.zig b/src/bun.js/webcore.zig index 341ccdb73e..78d9a737cf 100644 --- a/src/bun.js/webcore.zig +++ b/src/bun.js/webcore.zig @@ -244,8 +244,7 @@ pub const Prompt = struct { bun.Output.flush(); // 7. Pause while waiting for the user's response. - var stdin = std.io.getStdIn(); - var reader = stdin.reader(); + const reader = bun.buffered_stdin.reader(); const first_byte = reader.readByte() catch { // 8. Let result be null if the user aborts, or otherwise the string diff --git a/src/bun.zig b/src/bun.zig index b67ca73bb8..50743be682 100644 --- a/src/bun.zig +++ b/src/bun.zig @@ -2453,3 +2453,9 @@ pub fn getUserName(output_buffer: []u8) ?[]const u8 { copy(u8, output_buffer[0..size], user[0..size]); return output_buffer[0..size]; } + +/// This struct is a workaround a Windows terminal bug. +/// TODO: when https://github.com/microsoft/terminal/issues/16606 is resolved, revert this commit. +pub var buffered_stdin = std.io.BufferedReader(4096, std.fs.File.Reader){ + .unbuffered_reader = std.fs.File.Reader{ .context = .{ .handle = if (Environment.isWindows) undefined else 0 } }, +}; diff --git a/src/cli/init_command.zig b/src/cli/init_command.zig index a74044f986..fa982cf214 100644 --- a/src/cli/init_command.zig +++ b/src/cli/init_command.zig @@ -39,7 +39,7 @@ pub const InitCommand = struct { Output.flush(); - var input = try std.io.getStdIn().reader().readUntilDelimiterAlloc(alloc, '\n', 1024); + var input = try bun.buffered_stdin.reader().readUntilDelimiterAlloc(alloc, '\n', 1024); if (strings.endsWithChar(input, '\r')) { input = input[0 .. input.len - 1]; } diff --git a/src/main.zig b/src/main.zig index a0b60db496..8f42868a7f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -39,6 +39,8 @@ pub fn main() void { bun.win32.STDERR_FD = bun.toFD(std.io.getStdErr().handle); bun.win32.STDIN_FD = bun.toFD(std.io.getStdIn().handle); + bun.buffered_stdin.unbuffered_reader.context.handle = std.io.getStdIn().handle; + const w = std.os.windows; // https://learn.microsoft.com/en-us/windows/console/setconsoleoutputcp