From 9eccb62a072a431794f3e40a16861f6c6a16f902 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Fri, 27 Nov 2026 21:09:25 -0800 Subject: [PATCH] nvx: build the CLI's console outputs and forced stderr writes once ConsoleOutput::new and ConsoleOutput::stderr built the same structure with its Windows console fields for two streams, and print_stderr and end_stderr_line each locked standard error, wrote, flushed, and ignored the failure. Build both outputs with ConsoleOutput::to_stream, and make both forced writes with force_stderr, so that the Windows console fields and the reason for ignoring a failure each live in one place. Behavior is unchanged: the same stream decides whether the output is a console, and the forced writes lock, write, flush, and ignore a failure as before. A new test checks that output to a stream that is not a terminal passes its bytes, invalid UTF-8 included, unchanged. Base: 9f4ab27b26250eb6e69a1d648e353e85d28c9fa8. On Windows, cargo test --locked --features cli --lib -- host::cli::output host::cli::run::tests host::cli::session::tests::windows passed 18 with 2 ignored before, and 19 with 2 ignored after; clippy --all-targets -D warnings is clean. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- nvx/src/host/cli/output.rs | 54 +++++++++++++++++--------------- nvx/src/host/cli/output/tests.rs | 21 +++++++++++++ 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/nvx/src/host/cli/output.rs b/nvx/src/host/cli/output.rs index 812338708..b75d261e3 100644 --- a/nvx/src/host/cli/output.rs +++ b/nvx/src/host/cli/output.rs @@ -12,9 +12,7 @@ //! [`print_stderr`], which reports how the command ended, Ctrl-C included. On Windows, Python //! cannot end a write that waits either, so such a write runs to its end there. -#[cfg(windows)] -use std::io::IsTerminal; -use std::io::{self, Write}; +use std::io::{self, IsTerminal, Write}; #[cfg(unix)] use std::os::fd::AsFd; @@ -35,28 +33,29 @@ pub(super) fn print_stdout(text: &str) -> io::Result<()> { write_stdout(line(text).as_bytes()) } -/// Prints `text` and a line end to standard error, even after Ctrl-C. Like Python, which prints -/// errors on a stream that it cannot write anyway, it ignores a failure. +/// Prints `text` and a line end to standard error, even after Ctrl-C, as [`force_stderr`] writes. pub(super) fn print_stderr(text: &str) { - let mut stderr = io::stderr().lock(); - let _ = stderr - .write_all(line(text).as_bytes()) - .and_then(|()| stderr.flush()); + force_stderr(line(text).as_bytes()); } /// Ends the line that `written`, a workload's output that went to standard error last, left /// open, so that what [`print_stderr`] prints next starts a line of its own. It writes even after -/// Ctrl-C, as [`print_stderr`] does, and ignores a failure. +/// Ctrl-C, as [`force_stderr`] writes. pub(super) fn end_stderr_line(written: &[u8]) { let end = line_end_after(written); if !end.is_empty() { - let mut stderr = io::stderr().lock(); - let _ = stderr - .write_all(end.as_bytes()) - .and_then(|()| stderr.flush()); + force_stderr(end.as_bytes()); } } +/// Writes `data` to standard error and flushes it, even after Ctrl-C, since it reports how the +/// command ended. Like Python, which prints errors on a stream that it cannot write anyway, it +/// ignores a failure: no stream is left to report it on. +fn force_stderr(data: &[u8]) { + let mut stderr = io::stderr().lock(); + let _ = stderr.write_all(data).and_then(|()| stderr.flush()); +} + /// Returns what ends the line that `written` left open: nothing after no output or a line end, /// and [`LINE_SEPARATOR`] otherwise. fn line_end_after(written: &[u8]) -> &'static str { @@ -101,23 +100,28 @@ pub(super) struct ConsoleOutput { impl ConsoleOutput { /// Returns the output to standard output. pub(super) fn new() -> Self { - Self { - write: write_stdout, - #[cfg(windows)] - console: io::stdout().is_terminal(), - #[cfg(windows)] - pending: [0; 3], - #[cfg(windows)] - pending_len: 0, - } + Self::to_stream(write_stdout, &io::stdout()) } /// Returns the output to standard error. pub(super) fn stderr() -> Self { + Self::to_stream(write_stderr, &io::stderr()) + } + + /// Returns the output that `write` makes to `stream`, the stream that it writes, which is a + /// console if `stream` is a terminal. Only Windows treats a console apart. + fn to_stream( + write: fn(&[u8]) -> io::Result<()>, + #[cfg_attr( + not(windows), + expect(unused_variables, reason = "only Windows treats a console apart") + )] + stream: &impl IsTerminal, + ) -> Self { Self { - write: write_stderr, + write, #[cfg(windows)] - console: io::stderr().is_terminal(), + console: stream.is_terminal(), #[cfg(windows)] pending: [0; 3], #[cfg(windows)] diff --git a/nvx/src/host/cli/output/tests.rs b/nvx/src/host/cli/output/tests.rs index 98cfc1f4a..4455f14a1 100644 --- a/nvx/src/host/cli/output/tests.rs +++ b/nvx/src/host/cli/output/tests.rs @@ -17,3 +17,24 @@ fn only_output_that_leaves_its_last_line_open_gets_a_line_end() { assert_eq!(line_end_after(ended), "", "{ended:?}"); } } + +#[test] +fn output_to_a_stream_that_is_not_a_terminal_passes_its_bytes_unchanged() { + thread_local! { + static WRITTEN: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; + } + fn capture(data: &[u8]) -> io::Result<()> { + WRITTEN.with_borrow_mut(|written| written.extend_from_slice(data)); + Ok(()) + } + + let file = tempfile::tempfile().unwrap(); + assert!(!file.is_terminal()); + let mut output = ConsoleOutput::to_stream(capture, &file); + let chunks = [&b"caf\xc3"[..], b"\xa9 \xff|", b"\xe2\x82"]; + for chunk in chunks { + output.write_all(chunk).unwrap(); + } + output.finish().unwrap(); + assert_eq!(WRITTEN.take(), chunks.concat()); +}