Skip to content

Make grep regex type match newlines in paths - #1055

Open
Wooohaaa wants to merge 3 commits into
uutils:mainfrom
Wooohaaa:fix/grep-newline-clean
Open

Wooohaaa wants to merge 3 commits into
uutils:mainfrom
Wooohaaa:fix/grep-newline-clean

Conversation

@Wooohaaa

Copy link
Copy Markdown

Fixes #1046.

Oniguruma's grep syntax is line-oriented by default, so with -regextype grep, '.' and negated brackets never match a newline in a path. GNU's grep type does.

Enables REGEX_OPTION_MULTILINE (dot matches newline) and REGEX_OPTION_NEGATE_SINGLELINE (negated brackets match newline) for the Grep type in both find -regex and locate --regex. Adds an integration test with a newline-containing filename.

Note: cargo is unavailable locally, so build/tests could not be run here.

@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.32%. Comparing base (3d12105) to head (4b712c8).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/locate/mod.rs 75.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1055      +/-   ##
==========================================
+ Coverage   92.29%   92.32%   +0.02%     
==========================================
  Files          38       38              
  Lines        8714     8717       +3     
  Branches      429      431       +2     
==========================================
+ Hits         8043     8048       +5     
+ Misses        532      530       -2     
  Partials      139      139              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 15 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

✅ 20 untouched benchmarks


Comparing Wooohaaa:fix/grep-newline-clean (cbe2ef1) with main (7a9df76)

Open in CodSpeed

// clear SINGLELINE (so negated brackets also match newline).
if regex_type == RegexType::Grep {
options.insert(RegexOptions::REGEX_OPTION_MULTILINE);
options.insert(RegexOptions::REGEX_OPTION_NEGATE_SINGLELINE);

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.

are you sure this makes [^...] match a newline?
i think grep syntax excludes it through a syntax flag, not SINGLELINE
please add a test with a negated bracket, like .*/new[^a]line\.php

Comment thread src/locate/mod.rs
};
// Match GNU's "grep" regex type: let '.' match newline and clear
// SINGLELINE so negated brackets match newline too.
if ty == RegexType::Grep {

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 a locate test too (tests/db_tests.rs)

Comment thread src/locate/mod.rs
},
syntax,
)
let mut options = if config.ignore_case {

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 is now the same code as in RegexMatcher::new, could be dedup, no?

@Wooohaaa
Wooohaaa force-pushed the fix/grep-newline-clean branch from 4b712c8 to 3153f18 Compare November 25, 2026 10:42
@Wooohaaa

Copy link
Copy Markdown
Author

Thanks for the review. Addressed the comments:

  1. Added a negated-bracket case (.*/new[^a]line\.php) to find_grep_regex_matches_newline_in_path, so the [^...] behaviour is now covered.
  2. Added a make_regex unit test in src/locate/mod.rs covering the same pattern for locate.
  3. On dedup: I left the two option builders separate for now because RegexMatcher::new and make_regex take different inputs (a RegexType value vs. a Config); happy to factor out a shared helper if you prefer.

@github-actions

Copy link
Copy Markdown

Commit fb3980f has test result changes:

bfs testsuite:

Test results comparison:
  Current:   TOTAL: 317 / PASSED: 285 / FAILED: 26 / SKIPPED: 6
  Reference: TOTAL: 317 / PASSED: 286 / FAILED: 25 / SKIPPED: 6

Changes from main branch:
  TOTAL: +0
  PASSED: -1
  FAILED: +1

New test failures (1):
  - gnu/regextype_grep

@Wooohaaa

Copy link
Copy Markdown
Author

CI note: the failing cargo test/coverage/fakeroot jobs all report error[E0428]: the name find_printf_reserved_directives_are_errors is defined multiple times, which comes from the two duplicated tests already present on current main (commits 1253322 and 7a9df76). External-testsuites and Release pass; this PR's own changes pass local cargo test --all-features, rustfmt and clippy.

Oniguruma's grep syntax is line-oriented by default, so with -regextype
grep, '.' and negated brackets never match a newline in a path. GNU's
grep type does. Enable REGEX_OPTION_MULTILINE (dot matches newline) and
REGEX_OPTION_NEGATE_SINGLELINE (negated brackets match newline) for the
Grep type in both find -regex and locate --regex.

Adds an integration test with a filename containing a newline.

Fixes uutils#1046
The previous fix enabled MULTILINE and NEGATE_SINGLELINE but kept onig's
line-oriented grep syntax, under which negated brackets still do not match a
newline. Use GNU regex syntax for RegexType::Grep in both find and locate so
'.' and '[^...]' match newlines as GNU find does.

Also import Mode/ExistenceMode in the locate test module and keep rustfmt
clean. All tests pass locally (294 lib + integration suites).

Fixes uutils#1046
Switching to gnu_regex syntax fixed newline matching but broke the GNU BFS
regextype_grep suite by changing grep's other regex semantics. Instead, clone
onig's grep syntax and disable only SYNTAX_BEHAVIOR_NOT_NEWLINE_IN_NEGATIVE_CC,
which is the single behavior that excluded newlines from [^...].

All local tests pass (294 lib tests + integration suites), and rustfmt/clippy
are clean.
@sylvestre
sylvestre force-pushed the fix/grep-newline-clean branch from 73be2a8 to cbe2ef1 Compare November 25, 2026 18:21
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.

find / locate - -regextype grep: . and [^...] never match a newline in the path

2 participants