Subject: bug#23590: 25.0.94; Errors in default lgrep command

From: John Mastro <[email-masked]>
Date: Fri, 22 Jul 2016 19:16:27 -0700

Alex <[email-masked]> wrote:
> The default lgrep command (`all' for the FILES argument) gives
> off unexpected errors.
>
> * When there are no hidden (dot) files in the given directory, then I
>   get the following error:
>
> zsh:1: no matches found: *.
> Grep exited abnormally with code 1 at ...
>
> By default lgrep uses my default shell (zsh). Should this be the case?
> Setting `shell-file-name' to "bash" fixes this error, but it still
> produces the next error.

Thanks for reporting this. I've seen it too.

The problem seems to be caused by a difference in how zsh handles globs
that don't match anything, compared to bash. I'm not sure what the right
way would be to accommodate it in Emacs. Hopefully someone will be along
shortly with ideas for that. However, there are a couple things you can
do to work around it in the meantime.

First, you could change the command used by `lgrep' to enable zsh's
NULL_GLOB option, of which the zsh documentation[1] says: "If a pattern
for filename generation has no matches, delete the pattern from the
argument list instead of reporting an error". To do that, you could use
something like:

(with-eval-after-load 'grep
  (grep-apply-setting
   'grep-template
   "setopt null_glob; grep <X> <C> -n -e <R> <F>"))

Second, you could use advice on `lgrep' so that it invokes grep via bash
rather than zsh. That would look like:

(defun grep-use-bash (original &rest args)
  (let ((shell-file-name (executable-find "bash")))
    (apply original args)))

(with-eval-after-load 'grep
  (advice-add 'lgrep :around #'grep-use-bash))

I tested both options only briefly; apologies if I missed any issues.

> * When there are directories in the given directory (this includes . and
>   ..) then lgrep produces an error for each directory. For example:
>
>
> grep: .: Is a directory
> grep: ..: Is a directory
> grep: .emacs.d: Is a directory
>
> Grep exited abnormally with code 2 at ...

GNU Grep has an option (-d ACTION or --directories=ACTION) that can be
used to skip over directories (with "skip" as the ACTION), but it's not
in POSIX so I doubt we can use it in Emacs. If you know it will be
available on your system(s), you could add it to your `grep-template'
using the same technique as above.

[1] http://www.cs.elte.hu/zsh-manual/zsh_16.html

        John