Subject: Re: bug#68864: 30.0.50; project-find-regexp fails on Alpine

From: john muhl <[email-masked]>
Date: Thu, 01 Feb 2024 11:13:56 -0600

Dmitry Gutov <[email-masked]> writes:

> On 01/02/2024 05:38, john muhl via Bug reports for GNU Emacs, the
> Swiss army knife of text editors wrote:
>> The grep on Alpine does not support the --null option.
>>    $ grep --null test *
>>    grep: unrecognized option: null
>>    BusyBox v1.36.1 (2024-01-16 17:10:30 UTC) multi-call binary.
>> To reproduce:
>>    emacs -Q
>>    M-: (project-find-regexp "test")
>> Debugger entered--Lisp error: (user-error "Search failed with status
>> 123: grep: unrecognized option: null")
>>    signal(user-error ("Search failed with status 123: grep: unrecognized option: null"))
>
> Hi!
>
> That's a problem: apparently it does indeed not support --null or -Z:
> https://boxmatrix.info/wiki/Property:grep
>
> There is another flag we could use, which seems to have a similar
> enough effect: -z. But from what I can tell, it would make OpenBSD
> unsupported: https://man.openbsd.org/grep

Even -z is a compile time option:

  $ busybox grep -h
  BusyBox v1.36.1 (2024-01-16 17:10:30 UTC) multi-call binary.

  Usage: grep [-HhnlLoqvsrRiwFE] [-m N] [-A|B|C N] …

https://git.busybox.net/busybox/tree/findutils/grep.c#n85

I couldn’t get busybox to compile with the EXTRA_COMPAT option
so can’t say if it helps or not.

> Perhaps it would be best to just file a feature request for busybox's
> support for --null/-Z. Better ideas welcome.
>
> In the meantime, you can customize the entry for 'grep' in
> xref-search-program-alist to use -z.

Would it be possible to have xref check
grep-use-null-filename-separator and only use --null when
available? I tried with this crude patch and the results look
good enough:

  --- a/lisp/progmodes/xref.el
  +++ b/lisp/progmodes/xref.el
  @@ -1855,7 +1855,11 @@ xref-search-program-alist
       `((grep
          .
          ;; '-s' because 'git ls-files' can output broken symlinks.
  -       ,(concat "xargs -0 " xargs-max-chars "grep <C> --null -snHE -e <R>"))
  +       ,(concat "xargs -0 " xargs-max-chars "grep <C>"
  +                (when (and (grep-compute-defaults)
  +                           grep-use-null-filename-separator)
  +                  " --null")
  +                " -snHE -e <R>"))
         (ripgrep
          .
          ;; '!*/' is there to filter out dirs (e.g. submodules).