From 9776ebfa0fbb9bfb4acc0075def45edc31779f3e Mon Sep 17 00:00:00 2001 From: Jerry <48524572+JerryImMouse@users.noreply.github.com> Date: Tue, 8 Jul 2025 23:42:22 +0500 Subject: [PATCH] Send errors to stderr (#3336) * fix: send errors to stderr by default (#2561) Closes #2561 * chore: add changelog entry * chore: change PR id * chore: add github username * chore: cargo fmt... * chore: move changelog line to bugfixes --- CHANGELOG.md | 1 + src/error.rs | 8 +++++++- tests/integration_tests.rs | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be3b5d6c..c64258c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Make highlight tests fail when new syntaxes don't have fixtures PR #3255 (@dan-hipschman) - Fix crash for multibyte characters in file path, see issue #3230 and PR #3245 (@HSM95) - Add missing mappings for various bash/zsh files, see PR #3262 (@AdamGaskins) +- Send all bat errors to stderr by default, see #3336 (@JerryImMouse) ## Other diff --git a/src/error.rs b/src/error.rs index 007737b0..e5db81cc 100644 --- a/src/error.rs +++ b/src/error.rs @@ -67,7 +67,13 @@ pub fn default_error_handler(error: &Error, output: &mut dyn Write) { .ok(); } _ => { - writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok(); + writeln!( + &mut std::io::stderr().lock(), + "{}: {}", + Red.paint("[bat error]"), + error + ) + .ok(); } }; } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 5a97c517..540f85e8 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -450,6 +450,16 @@ fn stdin_to_stdout_cycle() -> io::Result<()> { Ok(()) } +#[cfg(unix)] +#[test] +fn bat_error_to_stderr() { + bat() + .arg("/tmp") + .assert() + .failure() + .stderr(predicate::str::contains("[bat error]")); +} + #[cfg(unix)] #[test] fn no_args_doesnt_break() {