patch-shebangs: don't patch shebangs with bash builtins

'command -v builtin' returns 'builtin', which doesn't suit us since
we're looking for program in the given PATH. This could give us shebangs
like this:

    #!builtin

which is surprising.

Switch to 'type -P command' which always returns a path, even if command
is both a builtin and an executable (for example 'test'), or fail is
'command' is just a builtin.
This commit is contained in:
Minijackson 2024-07-26 16:20:14 +02:00
parent df41014acf
commit 24655f7804
No known key found for this signature in database
GPG Key ID: FEA888C9F5D64F62
2 changed files with 18 additions and 4 deletions

View File

@ -90,8 +90,8 @@ patchShebangs() {
if [[ $arg0 == "-S" ]]; then
arg0=${args%% *}
args=${args#* }
newPath="$(PATH="${!pathName}" command -v "env" || true)"
args="-S $(PATH="${!pathName}" command -v "$arg0" || true) $args"
newPath="$(PATH="${!pathName}" type -P "env" || true)"
args="-S $(PATH="${!pathName}" type -P "$arg0" || true) $args"
# Check for unsupported 'env' functionality:
# - options: something starting with a '-' besides '-S'
@ -100,7 +100,7 @@ patchShebangs() {
echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)" >&2
exit 1
else
newPath="$(PATH="${!pathName}" command -v "$arg0" || true)"
newPath="$(PATH="${!pathName}" type -P "$arg0" || true)"
fi
else
if [[ -z $oldPath ]]; then
@ -109,7 +109,7 @@ patchShebangs() {
oldPath="/bin/sh"
fi
newPath="$(PATH="${!pathName}" command -v "$(basename "$oldPath")" || true)"
newPath="$(PATH="${!pathName}" type -P "$(basename "$oldPath")" || true)"
args="$arg0 $args"
fi

View File

@ -87,6 +87,20 @@ let
};
};
dont-patch-builtins = stdenv.mkDerivation {
name = "dont-patch-builtins";
strictDeps = false;
dontUnpack = true;
installPhase = ''
mkdir -p $out/bin
echo "#!/usr/bin/builtin" > $out/bin/test
chmod +x $out/bin/test
dontPatchShebangs=
'';
passthru = {
assertion = "grep '^#!/usr/bin/builtin' $out/bin/test > /dev/null";
};
};
};
in
stdenv.mkDerivation {