Skip to content

ignore: fix -g overrides not matching their own root directory - #3581

Open
Jbansal2 wants to merge 1 commit into
BurntSushi:masterfrom
Jbansal2:fix-override-root-dir-glob-match
Open

Jbansal2 wants to merge 1 commit into
BurntSushi:masterfrom
Jbansal2:fix-override-root-dir-glob-match

Conversation

@Jbansal2

Copy link
Copy Markdown

Summary

Fixes #1764.

-g/--glob overrides are matched via ignore::overrides::Override, which is built rooted at the current working directory and strips that root prefix from every path before matching (see Gitignore::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.:

cd /tmp/foo/dir1
rg --files -g '!dir1/' /tmp/foo

Here the override's root is /tmp/foo/dir1. When the walker visits the dir1 directory entry itself, stripping the root prefix from /tmp/foo/dir1 yields an empty string, which can never match the glob dir1/. Because the directory isn't recognized as ignored, the walker descends into it and the override is silently defeated -- d inside dir1 is 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:

Limitations

I wasn't able to run cargo test in 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 against Gitignore::strip/matched_stripped and checked the change against all existing tests in overrides.rs to make sure none of them exercise path == 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.

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
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.

Issue with -g "!dirname/" when the CWD is "dirname"

1 participant