Skip to content

Fix the "No matches with empty patterns file and invert match" bug #3041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

xenoninja
Copy link

@xenoninja xenoninja commented May 8, 2025

This PR addresses #1332 and #3001.
The problem is when an empty pattern file is given, ripgrep sees the pattern as "an empty regex" not "zero regexes".

pattern file grep's output rg's output
empty everything nothing
newline nothing nothing

Now the fix works out as below

pattern file grep's output rg's output
empty everything everything
newline nothing nothing

@BurntSushi BurntSushi added rollup A PR that has been merged with many others in a rollup. and removed rollup A PR that has been merged with many others in a rollup. labels Jul 27, 2025
@BurntSushi
Copy link
Owner

Thank you for this PR. Unfortunately, this was not quite correct. The optimization needs to be a hair smarter and the PCRE2 regex builder had to be fixed to correctly construct a pattern that can't match when there are zero patterns. And this should also come with a regression test.

I have this fixed in a branch that I'll release at some point soon.

BurntSushi added a commit that referenced this pull request Jul 27, 2025
Previously, when `file` is empty (literally empty, as in, zero byte),
`rg -f file` and `rg -vf file` would behave identically. This is odd
and also doesn't match how GNU grep behaves. It's also not logically
correct. An empty file means _zero_ patterns which is an empty set. An
empty set matches nothing. Inverting the empty set should result in
matching everything.

This was because of an errant optimization that lets ripgrep quit early
if it can statically detect that no matches are possible.

Moreover, there was *also* a bug in how we constructed the PCRE2 pattern
when there are zero patterns. PCRE2 doesn't have a concept of sets of
patterns (unlike the `regex` crate), so we need to fake it with an empty
character class.

Fixes #1332, Fixes #3001, Closes #3041
@xenoninja
Copy link
Author

Oh, sorry about the mistaken PR. I've seen the fix commit. Thanks for your effort!

@xenoninja xenoninja closed this Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rollup A PR that has been merged with many others in a rollup.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants