grep: strip trailing (os error N) from file error messages to match GNU (#6)

This commit is contained in:
Sylvestre Ledru
2026-05-29 22:24:20 +02:00
committed by GitHub
parent c50c0458cb
commit ff918e4a63
2 changed files with 17 additions and 1 deletions
+7 -1
View File
@@ -8,6 +8,7 @@ use crate::context_buffer::LineView;
use std::ffi::OsStr;
use std::io::{self, BufWriter, StdoutLock, Write};
use std::path::Path;
use uucore::error::strip_errno;
#[cfg(target_pointer_width = "64")]
const BUF_SIZE: usize = 128 * 1024;
@@ -195,7 +196,12 @@ impl<'a> OutputWriter<'a> {
/// Write an IO error to stderr.
pub fn report_io_error(&self, label: &OsStr, err: &io::Error) {
if !self.config.no_messages && !self.config.quiet {
eprintln!("grep: {label}: {err}", label = label.to_string_lossy());
// Strip the trailing " (os error XX)" so the message matches GNU grep.
eprintln!(
"grep: {label}: {err}",
label = label.to_string_lossy(),
err = strip_errno(err)
);
}
}
+10
View File
@@ -1030,6 +1030,16 @@ fn nonexistent_file_is_error() {
.stderr_contains("does-not-exist");
}
#[test]
fn nonexistent_file_error_has_no_os_error_suffix() {
// GNU prints "grep: <file>: No such file or directory" with no
// " (os error 2)" suffix; strip_errno keeps us byte-compatible.
let (_s, mut c) = ucmd();
c.args(&["x", "does-not-exist"])
.fails_with_code(2)
.stderr_is("grep: does-not-exist: No such file or directory\n");
}
#[test]
fn dash_argument_means_stdin() {
let (_s, mut c) = ucmd();