Repository: sharkdp/fd (language: rust)

Issue:
When a write error occurs on stdout fd just exits with 1
**Describe the bug you encountered:**
When a write error occurs on stdout fd just exits with 1

**Describe what you expected to happen:**
I expected a similar behavior to find eg
`find > /dev/full` 
prints
```
find: ‘standard output’: No space left on device
find: write error
``` 

**What version of `fd` are you using?**
<!-- paste the output of `fd --version` here -->
`fd 8.2.1`

**Which operating system / distribution are you on?**
<!--
Unix:    paste the output of `uname -srm` and `lsb_release -a` here.
Windows: please tell us your Windows version
-->
`Linux 5.11.2-arch1-1 x86_64`



Discussion / hints:
Thank you for reporting this!

Fix patch:
diff --git a/src/output.rs b/src/output.rs
index 57050878a..471b8fb9d 100644
--- a/src/output.rs
+++ b/src/output.rs
@@ -6,6 +6,7 @@ use std::sync::Arc;
 
 use lscolors::{LsColors, Style};
 
+use crate::error::print_error;
 use crate::exit_codes::ExitCode;
 use crate::filesystem::strip_current_dir;
 use crate::options::Options;
@@ -33,9 +34,14 @@ pub fn print_entry(
         print_entry_uncolorized(stdout, path, config)
     };
 
-    if r.is_err() {
-        // Probably a broken pipe. Exit gracefully.
-        process::exit(ExitCode::GeneralError.into());
+    if let Err(e) = r {
+        if e.kind() == ::std::io::ErrorKind::BrokenPipe {
+            // Exit gracefully in case of a broken pipe (e.g. 'fd ... | head -n 3').
+            process::exit(0);
+        } else {
+            print_error(format!("Could not write to output: {}", e));
+            process::exit(ExitCode::GeneralError.into());
+        }
     }
 }
 


Test patch:
diff --git a/tests/tests.rs b/tests/tests.rs
index 056a064ad..783420ad6 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -1663,7 +1663,7 @@ fn test_base_directory() {
     let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);
     let abs_base_dir = &format!("{abs_path}/one/two", abs_path = &abs_path);
     te.assert_output(
-        &["--base-directory", &abs_base_dir, "foo", &abs_path],
+        &["--base-directory", abs_base_dir, "foo", &abs_path],
         &format!(
             "{abs_path}/a.foo
             {abs_path}/one/b.foo