Skip to content

Commit

Permalink
logging: show heuristic information and decision
Browse files Browse the repository at this point in the history
When one does not provide any paths to ripgrep to search, it has to
guess between searching stdin and the current working directory. It is
possible for this guess to be wrong, and having the heuristics and the
choice in the debug logs is useful for diagnosing this.

The failure mode here is still pretty bad because you need to know to
reach for the `--debug` flag in the first place. Namely, the typical
failure mode is that ripgrep tries to search stdin while the intent is
for it to search the current working directory, and thus likely blocking
forever waiting for data on stdin.

(Arguably this is a problem with the process architecture that invokes
ripgrep. It shouldn't give ripgrep an open stdin handle that isn't
closed.)

Closes #2524
  • Loading branch information
BurntSushi committed Nov 25, 2023
1 parent a2907db commit ebb986e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Feature enhancements:
When `extra-verbose` mode is enabled in zsh, show extra file type info.
* [FEATURE #2409](https://github.com/BurntSushi/ripgrep/pull/2409):
Added installation instructions for `winget`.
* [FEATURE #2524](https://github.com/BurntSushi/ripgrep/issues/2524):
The `--debug` flag now indicates whether stdin or `./` is being searched.
* [FEATURE #2643](https://github.com/BurntSushi/ripgrep/issues/2643):
Make `-d` a short flag for `--max-depth`.

Expand Down
14 changes: 13 additions & 1 deletion crates/core/flags/hiargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,12 +1080,24 @@ impl Paths {
// mode, but there really is no good way to mitigate it. It's just a
// consequence of letting the user type 'rg foo' and "guessing" that
// they meant to search the CWD.
let use_cwd = !grep::cli::is_readable_stdin()
let is_readable_stdin = grep::cli::is_readable_stdin();
let use_cwd = !is_readable_stdin
|| state.stdin_consumed
|| !matches!(low.mode, Mode::Search(_));
log::debug!(
"using heuristics to determine whether to read from \
stdin or search ./ (\
is_readable_stdin={is_readable_stdin}, \
stdin_consumed={stdin_consumed}, \
mode={mode:?})",
stdin_consumed = state.stdin_consumed,
mode = low.mode,
);
let (path, is_one_file) = if use_cwd {
log::debug!("heuristic chose to search ./");
(PathBuf::from("./"), false)
} else {
log::debug!("heuristic chose to search stdin");
(PathBuf::from("-"), true)
};
Ok(Paths { paths: vec![path], has_implicit_path: true, is_one_file })
Expand Down

0 comments on commit ebb986e

Please sign in to comment.