Skip to content

grep: name the -f file and line of a pattern that fails to compile - #189

Open
sarankumar1325 wants to merge 3 commits into
uutils:mainfrom
sarankumar1325:grep-pattern-file-error-location
Open

sarankumar1325 wants to merge 3 commits into
uutils:mainfrom
sarankumar1325:grep-pattern-file-error-location

Conversation

@sarankumar1325

Copy link
Copy Markdown
Contributor

Fixes #179.

When a pattern read with -f FILE fails to compile, GNU grep names the file and line it came from (grep: pats:2: Invalid range end, and -:2: for -f -). uu_grep printed only the message, so with a long pattern file you couldn't tell which line was wrong.

Change

  • uumain records where each pattern came from (-f file and 1-based line, or nothing for -e and positional patterns) in a new Config::pattern_origins list, kept in step with patterns.
  • CompiledPattern::compile puts FILE:LINE: in front of the regex compile error when the pattern came from a file.
  • As in GNU grep 3.11, there's no prefix for -e or positional patterns, for the [:space:] check (GNU's dfa reports that one without a location), or for -P errors (GNU's PCRE backend doesn't add one either).

Compared with GNU grep 3.11 (LC_ALL=C):

$ printf 'foo\n[z-a]\n' > pats; printf 'foo\n' | grep -f pats
grep: pats:2: Invalid range end            # exit 2
$ printf 'x\n[z-a]\n' | grep -f -
grep: -:2: Invalid range end
$ printf 'foo\n' | grep -E -f ok -f bad     # ok = foo\nbar, bad = [z-a]
grep: bad:1: Invalid range end
$ printf 'foo\n' | grep -e '[z-a]'
grep: Invalid range end

The wording of other Oniguruma errors is still different from GNU's (that's #185), but they now carry the same location prefix.

Tests

New test pattern_file_compile_error_names_file_and_line covers -f FILE, -f -, line numbers counted per file with -E, and no prefix for -e or -P. It fails on main and passes with this change. cargo test, cargo fmt --check and cargo clippy --all-targets -- -D warnings pass.

I wrote this change with help from an AI assistant. I reviewed and tested it myself.

GNU grep reports a syntax error in a pattern read with -f FILE as
"FILE:LINE: message" ("-:LINE:" for -f -), so the user can tell which
line of the pattern file to fix. Patterns from -e or the command line and
errors from the PCRE backend keep the plain message.

Fixes uutils#179
Copilot AI balanced review requested due to automatic review settings November 9, 2026 17:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@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 sarankumar1325:grep-pattern-file-error-location (e2e64e0) with main (e123c36)

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/matcher.rs Outdated
for raw in config.patterns {
patterns.push(CompiledPattern::compile(raw, config)?);
for (index, raw) in config.patterns.iter().enumerate() {
let origin = config.pattern_origins.get(index).copied().flatten();

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.

.get(index)...flatten() hides a length mismatch between the two vecs
could we zip them, or keep (pattern, origin) together in one vec?

Comment thread src/matcher.rs Outdated
// Like GNU grep, a regex syntax error in a pattern read with `-f FILE`
// names the file and line (`FILE:LINE: message`). GNU's PCRE backend
// reports its errors without this location.
let location = match origin {

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.

this formats a string for every pattern, even the ones that compile
could you please pass origin to compile_with and format only in the error path?

Comment thread tests/test_grep.rs Outdated
// GNU grep prefixes a syntax error in a pattern read with `-f FILE` with
// `FILE:LINE: `, so the user knows which line of the file to fix.
let (scene, mut c) = ucmd();
scene.fixtures.write("pats", "foo\n[z-a]\n");

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 add an empty line and a duplicate to the fixture, so we check they are counted too

…on error

Store patterns as (text, origin) pairs so the two can't drift apart, pass
the origin to compile_with and build the FILE:LINE prefix only when a
pattern fails to compile. Add an empty line and a duplicate to the test
fixture so they are counted as lines, and ignore the stdin write error
in cases where grep exits before reading its input.
Copilot AI balanced review requested due to automatic review settings November 9, 2026 19:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Errors returned through uucore are prefixed with the binary name as
invoked, which is grep.exe on Windows, so the exact stderr checks in
pattern_file_compile_error_names_file_and_line failed there. Build the
expected message with the platform's name.
Copilot AI balanced review requested due to automatic review settings November 11, 2026 03:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

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 - an invalid pattern read with -f FILE is reported without GNU's FILE:LINE: prefix

3 participants