Repository navigation
Conversation
When an override's root directory (the CWD, by default) is itself visited while walking one of its ancestors -- e.g. running 'rg -g "!dir1/" /some/root' from inside /some/root/dir1 -- stripping the root prefix from that directory's path yields an empty candidate, which can never match any glob. This silently defeats the override and causes the directory (and everything inside it) to be searched anyway. Fix this by falling back to matching the root directory's own base name in that specific case, without changing how any other path is stripped and matched. Closes BurntSushi#1764
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1764.
-g/--globoverrides are matched viaignore::overrides::Override, which is built rooted at the current working directory and strips that root prefix from every path before matching (seeGitignore::strip, reused internally).That stripping breaks when the path being matched is exactly the override's own root directory -- which happens whenever that directory is itself visited while walking one of its ancestors, e.g.:
Here the override's root is
/tmp/foo/dir1. When the walker visits thedir1directory entry itself, stripping the root prefix from/tmp/foo/dir1yields an empty string, which can never match the globdir1/. Because the directory isn't recognized as ignored, the walker descends into it and the override is silently defeated --dinsidedir1is still printed, even though the pattern clearly names that exact directory.This matches the original report's repro case exactly (both "BUG" cases in the issue are this same root-equals-path scenario; the "OK" cases are all paths where CWD and the glob's named directory differ, which were never affected).
Fix
In
Override::matched, when the path given is exactly the override's root directory, fall back to testing the glob against the root's own base name. This only changes behavior for that one specific edge case (path == root); every other path is stripped and matched exactly as before, so normal root-relative anchoring (e.g./foo,src/*.rs) is unaffected.Testing
Added two focused unit tests in
crates/ignore/src/overrides.rs::tests:root_dir_name_matches_itself: the exact scenario from Issue with -g "!dirname/" when the CWD is "dirname" #1764.root_dir_name_does_not_match_unrelated_pattern: confirms the fallback doesn't cause false positives for unrelated patterns.Limitations
I wasn't able to run
cargo testin the environment I used to write this fix (no working MSVC/mingw linker available), so this hasn't been compiled or executed. I traced the matching logic carefully by hand againstGitignore::strip/matched_strippedand checked the change against all existing tests inoverrides.rsto make sure none of them exercisepath == root(none do), so I don't believe this regresses anything, but I want to flag that this PR has not been built or test-run locally.