The why verb
rf why <pattern> <file> [--root <dir>] [--json]
why answers one question about one file: does pattern match in file, and
if a default search would not show it, which filter hid it? Use it when you
know the file and want the reason, not a tree search.
why accepts the match-mode flags (--fixed-strings, --word,
--ignore-case, --case-sensitive) and the evidence flags (--matches,
--max-matches). They work as on content.
Example
cache/ is in .gitignore, and cache/build.py contains timeout.
rf why timeout cache/build.py
why 'timeout' cache/build.py [regex any sensitive]: MATCH - hidden by vcs_ignore
ignore rule: .gitignore:1 (cache/)
! IGNORE_VCS: match hidden from a default tree search; add -u (ignore .gitignore/.ignore rules)
$ 'rg' '-u' '-e' 'timeout' '--' '.'
The same result as JSON:
{
"ok": true,
"data": {
"file": "cache/build.py",
"pattern": "timeout",
"matched": true,
"surfaced_by": "vcs_ignore",
"hidden_from_default": true,
"hiding_filter": { "code": "IGNORE_VCS", "layer": "vcs_ignore", "rg_flags": "-u" },
"ignore_source": { "class": "gitignore", "source_file": ".gitignore", "line": 1, "pattern": "cache/" },
"matches": null
}
}
What the fields mean
matched— whether the pattern occurs in the file.surfaced_by— the layer that shows the match. The values are the same as on content.hidden_from_defaultandhiding_filter— whether a default search hides the file, the warning code for that filter, and thergflags that remove it.ignore_source— for a file hidden by an ignore rule: the rule’s file, line, and pattern. Ifrfcannot identify the rule, theIGNORE_SOURCE_UNRESOLVEDwarning says so.
Errors
A file that does not exist is exit 1 with NOT_FOUND. A file that exists and
cannot be read is exit 3 with IO_FAILURE. See
Exit codes.