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

From: Dmitry Gutov <[email-masked]>
Date: Thu, 1 Feb 2024 22:54:53 +0200

On 01/02/2024 19:13, john muhl wrote:
> 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

That's... unexpected.

> 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).

That would slow down the loading of xref.el, which seems unfortunate, 
even if it's only by 0.05s (just measured on my machine; on many others 
it will be more).

What we could do, I suppose, is check whether 
grep-use-null-filename-separator is set to some explicit value (not 
'auto-detect'), and if so, apply it. That would work if xref.el is 
loaded late, or if you customized the variable explicitly.

I was also thinking of adding another template parameter like <N>, but 
it would be used for 'grep'--would seem odd.

We also could just remove "--null" inside xref-matches-in-files from 
grep's command line if the variable is set to this or that. It would be 
unfortunate to encounter some false positives, though (though I can't 
imagine them now).