Skip to content

grep: read --exclude-from=- from standard input - #186

Open
bfs2021 wants to merge 3 commits into
uutils:mainfrom
bfs2021:fix-exclude-from-stdin
Open

bfs2021 wants to merge 3 commits into
uutils:mainfrom
bfs2021:fix-exclude-from-stdin

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 9, 2026

Copy link
Copy Markdown

Problem

GNU grep reads the exclude list from standard input when the --exclude-from operand is -:

$ printf '*.log\n' | grep -rl --exclude-from=- PAT dir
dir/b.txt

uu_grep instead opened a file literally named -: with no such file it exits 2 with grep: -: No such file or directory, and if a file named - happens to exist it silently uses that file and never drains the pipe. -f - already reads standard input correctly.

Fixes #167

Root cause

uumain in src/lib.rs has a - branch for the -f pattern files (src/lib.rs:243-252) but the --exclude-from loop a few lines below read every operand with std::fs::read_to_string(path) unconditionally, so "-" was treated as a path.

Fix

The --exclude-from loop now uses the same - branch as -f: read the list from stdin and label read errors (standard input). Nothing else changes — the patterns still enter the glob list at their command-line position.

Testing

  • New exclude_from_dash_reads_standard_input in tests/test_grep.rs: -r -l --exclude-from=- with *.log\n piped on stdin keeps a.txt and drops b.log. It fails on the previous code (grep: -: No such file or directory, exit 2) and passes with the fix.
  • cargo test: 98 passed, 2 failed — nonexistent_file_error_has_no_os_error_suffix and quiet_match_overrides_file_error fail identically on a clean checkout here because this machine's Windows is localized (the OS message is 系统找不到指定的文件。, the tests expect the English text). Pre-existing, unrelated to this change.
  • cargo fmt --check and cargo clippy --all-targets: clean.

GNU grep treats `-` as standard input for --exclude-from, as it already
does for -f. uu_grep read the operand as a path, so
`printf '*.log\n' | grep -rl --exclude-from=- PAT dir` failed with
"grep: -: No such file or directory" (exit 2), and silently used a file
literally named `-` when one happened to exist.

Use the same `-` branch as the -f loop: read stdin and label read errors
"(standard input)".

Fixes uutils#167

Signed-off-by: bfs2021 <101915083+bfs2021@users.noreply.github.com>
@codspeed

codspeed Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing bfs2021:fix-exclude-from-stdin (37443be) with main (feed2e5)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread src/lib.rs Outdated
.map_err(|err| read_error("(standard input)", &err))?;
buf
} else {
std::fs::read_to_string(path).map_err(|err| read_error(path, &err))?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please handle the result error in a single place instead of calling read_error twice.

main's GlobSet refactor (one GlobSet for --include, --exclude and
--exclude-dir) restructured the surrounding code; resolve the conflict
on top of it and address review feedback by reporting read errors from
the --exclude-from loop in a single place.

Signed-off-by: bfs2021 <101915083+bfs2021@users.noreply.github.com>
@bfs2021

bfs2021 commented Nov 9, 2026

Copy link
Copy Markdown
Author

Merged main (the GlobSet refactor) to clear the conflict, and reworked the --exclude-from loop to a single read_error call site: both read paths now produce io::Result<String> and the error is mapped once with the label chosen up front ("(standard input)" for -).

Comment thread src/lib.rs Outdated
) {
for (i, path) in indices.zip(paths) {
let contents = std::fs::read_to_string(path).map_err(|err| read_error(path, &err))?;
let label = if path == "-" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path == "-" is tested twice, could be a single if returning the label and the result, no?
and it is almost the same code as the -f branch above, could be a small shared helper

Comment thread tests/test_grep.rs Outdated
let (scene, mut c) = ucmd();
build_tree(&scene);
c.args(&["-r", "-l", "--exclude-from=-", "grep", "tree"])
.pipe_in(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use .pipe_in("*.log\n"), the multi-line literal is hard to read

`-f` and `--exclude-from` each carried their own `-` branch; a shared
helper returns the error label and the contents, so the operand is tested
once.
@bfs2021

bfs2021 commented Nov 10, 2026

Copy link
Copy Markdown
Author

Both points taken in 37443be: -f and --exclude-from now share read_pattern_file, which returns the error label together with the contents, so - is tested once. The test uses .pipe_in("*.log\n").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep - --exclude-from=- looks for a file named - instead of reading standard input

3 participants