On , I learnt ...

How ripgrep interprets .gitignore rules changes when a filepath argument is used

Consider this dummy static site structure:

$ mkdir -p docs/_build/
$ echo "<h1>hello</h1>" > docs/_build/index.html
$ echo "# hello" > docs/index.md

A ripgrep search for “hello” matches both files:

$ rg hello
docs/index.md
1:# hello

docs/_build/index.html
1:<h1>hello</h1>

Suppose we want to ignore generated HTML files in docs/_build/. We could add docs/_build/ to a .gitignore file:

$ echo "docs/_build/" > .gitignore

and searches ignore that folder:

$ rg hello
docs/index.md
1:# hello

But explicitly searching the docs folder matches files that we want ignored:

$ rg hello docs/
docs/_build/index.html
1:<h1>hello</h1>

docs/index.md
1:# hello

From man rg:

Paths specified explicitly on the command line override glob and ignore rules.

which, in practice, seems to mean that the .gitignore rules are applied against filepaths relative to the filepath argument.

So in the above example, we can get the behaviour we want by changing the .gitignore rule to **/_build.

$ echo "**/_build/" > .gitignore
$ rg hello docs/
docs/index.md
1:# hello

For a deeper discussion, see this ticket.