diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f679045..a5e2d3b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,7 +53,10 @@ ## Bugfixes +- Send all bat error messages to stderr, see #2827 (@deboard) + - Fix long file name wrapping in header, see #2835 (@FilipRazek) +- - Fix `NO_COLOR` support, see #2767 (@acuteenvy) - Fix handling of inputs with OSC ANSI escape sequences, see #2541 and #2544 (@eth-p) - Fix handling of inputs with combined ANSI color and attribute sequences, see #2185 and #2856 (@eth-p) diff --git a/src/error.rs b/src/error.rs index 007737b0..24c1409f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -67,7 +67,15 @@ pub fn default_error_handler(error: &Error, output: &mut dyn Write) { .ok(); } _ => { - writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok(); + // default - always write [bat error] to stderr + let stderr = std::io::stderr(); + writeln!( + &mut stderr.lock(), + "{}: {}", + Red.paint("[bat error]"), + error + ) + .ok(); } }; } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 97dbd550..970c0d3d 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -861,6 +861,16 @@ fn env_var_bat_pager_value_bat() { .stderr(predicate::str::contains("bat as a pager is disallowed")); } +#[test] +fn bat_error_to_stderr() { + bat() + .env("BAT_PAGER", "bat") + .arg("/tmp") + .assert() + .failure() + .stderr(predicate::str::contains("[bat error]")); +} + #[test] fn pager_value_bat() { bat() @@ -1358,6 +1368,9 @@ fn can_print_file_starting_with_cache() { .stderr(""); } +#[test] +fn send_all_bat_error_to_stderr() {} + #[test] fn does_not_print_unwanted_file_named_cache() { bat_with_config().arg("cach").assert().failure(); diff --git a/tests/scripts/stderr1.sh b/tests/scripts/stderr1.sh new file mode 100755 index 00000000..73abc0d4 --- /dev/null +++ b/tests/scripts/stderr1.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +## test for issue 2561 + +OUTPUT=$(mktemp) +BAT=bat +code=$($BAT /tmp 2> $OUTPUT; cat $OUTPUT | grep error; echo $?) + +if [[ $code == 1 ]]; then + echo "stderr test fsil" + exit 1 +fi + +exit 0