ripgrep/tests
Andrew Gallant b610d1cb15 ignore: fix global gitignore bug that arises with absolute paths
The `ignore` crate currently handles two different kinds of "global"
gitignore files: gitignores from `~/.gitconfig`'s `core.excludesFile`
and gitignores passed in via `WalkBuilder::add_ignore` (corresponding to
ripgrep's `--ignore-file` flag).

In contrast to any other kind of gitignore file, these gitignore files
should have their patterns interpreted relative to the current working
directory. (Arguably there are other choices we could make here, e.g.,
based on the paths given. But the `ignore` infrastructure can't handle
that, and it's not clearly correct to me.) Normally, a gitignore file
has its patterns interpreted relative to where the gitignore file is.
This relative interpretation matters for patterns like `/foo`, which are
anchored to _some_ directory.

Previously, we would generally get the global gitignores correct because
it's most common to use ripgrep without providing a path. Thus, it
searches the current working directory. In this case, no stripping of
the paths is needed in order for the gitignore patterns to be applied
directly.

But if one provides an absolute path (or something else) to ripgrep to
search, the paths aren't stripped correctly. Indeed, in the core, I had
just given up and not provided a "root" path to these global gitignores.
So it had no hope of getting this correct.

We fix this assigning the CWD to the `Gitignore` values created from
global gitignore files. This was a painful thing to do because we'd
ideally:

1. Call `std::env::current_dir()` at most once for each traversal.
2. Provide a way to avoid the library calling `std::env::current_dir()`
   at all. (Since this is global process state and folks might want to
   set it to different values for $reasons.)

The `ignore` crate's internals are a total mess. But I think I've
addressed the above 2 points in a semver compatible manner.

Fixes #3179
2025-10-15 19:44:23 -04:00
..
data tests: fix tests for buffer size change 2021-03-23 18:14:18 -04:00
binary.rs cli: document that -c/--count can be inconsistent with -l/--files-with-matches 2025-09-22 20:24:53 -04:00
feature.rs test: attempt to fix flaky time-reliant test 2025-10-10 22:06:59 -04:00
hay.rs tests: re-tool integration tests 2018-08-20 07:10:19 -04:00
json.rs style: simplify string formatting 2025-09-19 21:08:19 -04:00
macros.rs style: rustfmt everything 2020-02-17 19:24:53 -05:00
misc.rs test: fix Command::current_dir API 2025-10-10 22:06:59 -04:00
multiline.rs printer: vimgrep now only prints one line 2021-05-31 21:51:18 -04:00
regression.rs ignore: fix global gitignore bug that arises with absolute paths 2025-10-15 19:44:23 -04:00
tests.rs binary: rejigger ripgrep's handling of binary files 2019-04-14 19:29:27 -04:00
util.rs test: fix Command::current_dir API 2025-10-10 22:06:59 -04:00