0

I'm running ripgrep search on Windows inside emacs. My config is

(setq grep-command "rg -nS --no-heading "
      grep-use-null-device nil)

When I run command M-x grep and pass, for example, hello rg -nS --no-heading hello, it says that Grep finished with no matches found .... There should be matches, but it doesn't find anything no matter what I try.

I diagnosed it a bit, so there is a way of calling ripgrep providing a path: rg [OPTIONS] PATTERN [PATH ...]. I tried running rg -nS --no-heading hello . (with dot at the end) and it worked. That's a bit confusing, since both commands run fine in console from the same path. But in emacs only the second one seems to work. Can somebody explain why it works that way?

1 Answer 1

2

From ripgrep's man page:

       ripgrep  will  automatically  detect  if stdin exists and search
       stdin for a regex pattern, e.g. ls | rg foo.  In  some  environ‐
       ments,  stdin may exist when it shouldn't. To turn off stdin de‐
       tection, one can explicitly specify  the  directory  to  search,
       e.g. rg foo ./.

For whatever reason, the way in which you are running rg from within emacs advertises stdin as something readable. ripgrep detects this and tries to search it. If there's nothing there, then ripgrep returns no matches (or may even block indefinitely if a read on stdin blocks).

By passing an explicit path to search, heuristics used to determine whether stdin should be searched or not are disabled.

ripgrep's --debug output emits messages about this decision procedure. It will tell you precisely why stdin is being searched:

$ echo hi | rg hi --debug
rg: DEBUG|rg::flags::hiargs|crates/core/flags/hiargs.rs:1083: number of paths given to search: 0
rg: DEBUG|grep_cli|crates/cli/src/lib.rs:209: for heuristic stdin detection on Unix, found that is_file=false, is_fifo=true and is_socket=false, and thus concluded that is_stdin_readable=true
rg: DEBUG|rg::flags::hiargs|crates/core/flags/hiargs.rs:1108: using heuristics to determine whether to read from stdin or search ./ (is_readable_stdin=true, stdin_consumed=false, mode=Search(Standard))
rg: DEBUG|rg::flags::hiargs|crates/core/flags/hiargs.rs:1121: heuristic chose to search stdin
...
hi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.