Subject: Re: [PATCH v2 2/2] meson: prefer POSIX-specified shell path

From: Toon Claes <[email-masked]>
Date: Fri, 25 Apr 2025 10:35:01 +0200

Patrick Steinhardt <[email-masked]> writes:

> Meson detects the path of the target shell via `find_program("sh")`,
> which essentially does a lookup via `PATH`. This may easily lead to a
> subtly-broken Git distribution when the build host has its shell in a
> non-standard location that the target host doesn't know about.
>
> Fix the issue by appending "/bin" to the custom program path, which
> causes us to prefer "/bin/sh" over a `PATH` lookup. As this location is
> specified by POSIX this should make us pick a better default shell path
> on all POSIX-compliant systems.
>
> Note that we intentionally append, not prepend, to the custom program
> path. This is because the program path can be configured by the user via
> the `-Dsane_tool_path=` build option, which should take precedence over
> any defaults we pick for the user.
>
> Signed-off-by: Patrick Steinhardt <[email-masked]>
> ---
>  meson.build | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/meson.build b/meson.build
> index a180c66ee69..c0d0982b00f 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -236,7 +236,10 @@ sed = find_program('sed', dirs: program_path, native: true)
>  shell = find_program('sh', dirs: program_path, native: true)
>  tar = find_program('tar', dirs: program_path, native: true)
>  
> -target_shell = find_program('sh', dirs: program_path, native: false)
> +# Detect the target shell that is used by Git at runtime. Note that we prefer
> +# '/bin/sh' over a PATH-based lookup given that '/bin/sh' is the location
> +# specified by POSIX. This lookup can be overridden via `program_path`.
> +target_shell = find_program('sh', dirs: program_path + [ '/bin' ], native: false)

It was not instantly obvious to me, but this is what the docs[1] say
about the use of `dirs` in `find_program()`:

   extra list of absolute paths where to look for program names

So the function *first* looks in `dirs` *before* it looks in $PATH. I
wasn't fully aware what `program_path` contained, but I assumed it had
to contain the dirs in $PATH. But it seems $PATH is searched if the
program is not found in `dirs`.

So I agree with these changes.

[1]: https://mesonbuild.com/Reference-manual_functions.html#find_program

--
Toon