mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-27 01:13:05 +00:00
c47f245253
This fixes a bug where `patchShebangs` crashes when trying to patch
files that contain only a shebang (e.g. `#!/bin/bash`) (and nothing
else) and do not end with a newline.
Such file can be produced using `printf "#!/bin/bash" > example` or
`echo -n "#!/bin/bash" > example`.
I don't understand why one would want to create such files, as they do
literally nothing, but the chromium tarball we are using started
shipping some 🫠
Full reproducer:
```nix
with import <nixpkgs> { };
stdenv.mkDerivation {
dontUnpack = true;
name = "patch-shebangs-no-trailing-newline-reproducer";
postPatch = ''
printf "#!/bin/bash" > reproducer
chmod +x reproducer
patchShebangs reproducer
'';
}
```
```
❯ nix-build reproducer.nix
this derivation will be built:
/nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv
building '/nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv'...
patching sources
patching script interpreter paths in reproducer
/nix/store/vr6wwdxkmyy44sg0gwxi10b8fc5zhwz0-stdenv-linux/setup: line 144: pop_var_context: head of shell_variables not a function context
error: builder for '/nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv' failed with exit code 1;
last 3 log lines:
> patching sources
> patching script interpreter paths in reproducer
> /nix/store/vr6wwdxkmyy44sg0gwxi10b8fc5zhwz0-stdenv-linux/setup: line 144: pop_var_context: head of shell_variables not a function context
For full logs, run 'nix log /nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv'.
```
131 lines
3.5 KiB
Nix
131 lines
3.5 KiB
Nix
{ lib, stdenv, pkgs }:
|
|
|
|
# since the tests are using a early stdenv, the stdenv will have dontPatchShebangs=1, so it has to be unset
|
|
# https://github.com/NixOS/nixpkgs/blob/768a982bfc9d29a6bd3beb963ed4b054451ce3d0/pkgs/stdenv/linux/default.nix#L148-L153
|
|
|
|
# strictDeps has to be disabled because the shell isn't in buildInputs
|
|
|
|
let
|
|
tests = {
|
|
bad-shebang = stdenv.mkDerivation {
|
|
name = "bad-shebang";
|
|
strictDeps = false;
|
|
dontUnpack = true;
|
|
installPhase = ''
|
|
mkdir -p $out/bin
|
|
echo "#!/bin/bash" > $out/bin/test
|
|
echo "echo -n hello" >> $out/bin/test
|
|
chmod +x $out/bin/test
|
|
dontPatchShebangs=
|
|
'';
|
|
passthru = {
|
|
assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
|
|
};
|
|
};
|
|
|
|
ignores-nix-store = stdenv.mkDerivation {
|
|
name = "ignores-nix-store";
|
|
strictDeps = false;
|
|
dontUnpack = true;
|
|
installPhase = ''
|
|
mkdir -p $out/bin
|
|
echo "#!$NIX_STORE/path/to/bash" > $out/bin/test
|
|
echo "echo -n hello" >> $out/bin/test
|
|
chmod +x $out/bin/test
|
|
dontPatchShebangs=
|
|
'';
|
|
passthru = {
|
|
assertion = "grep \"^#!$NIX_STORE/path/to/bash\" $out/bin/test > /dev/null";
|
|
};
|
|
};
|
|
|
|
updates-nix-store = stdenv.mkDerivation {
|
|
name = "updates-nix-store";
|
|
strictDeps = false;
|
|
dontUnpack = true;
|
|
installPhase = ''
|
|
mkdir -p $out/bin
|
|
echo "#!$NIX_STORE/path/to/bash" > $out/bin/test
|
|
echo "echo -n hello" >> $out/bin/test
|
|
chmod +x $out/bin/test
|
|
patchShebangs --update $out/bin/test
|
|
dontPatchShebangs=1
|
|
'';
|
|
passthru = {
|
|
assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
|
|
};
|
|
};
|
|
|
|
split-string = stdenv.mkDerivation {
|
|
name = "split-string";
|
|
strictDeps = false;
|
|
dontUnpack = true;
|
|
installPhase = ''
|
|
mkdir -p $out/bin
|
|
echo "#!/usr/bin/env -S bash --posix" > $out/bin/test
|
|
echo "echo -n hello" >> $out/bin/test
|
|
chmod +x $out/bin/test
|
|
dontPatchShebangs=
|
|
'';
|
|
passthru = {
|
|
assertion = "grep -v '^#!${pkgs.coreutils}/bin/env -S ${stdenv.shell} --posix' $out/bin/test > /dev/null";
|
|
};
|
|
};
|
|
|
|
without-trailing-newline = stdenv.mkDerivation {
|
|
name = "without-trailing-newline";
|
|
strictDeps = false;
|
|
dontUnpack = true;
|
|
installPhase = ''
|
|
mkdir -p $out/bin
|
|
printf "#!/bin/bash" > $out/bin/test
|
|
chmod +x $out/bin/test
|
|
dontPatchShebangs=
|
|
'';
|
|
passthru = {
|
|
assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
|
|
};
|
|
};
|
|
|
|
};
|
|
in
|
|
stdenv.mkDerivation {
|
|
name = "test-patch-shebangs";
|
|
passthru = { inherit (tests) bad-shebang ignores-nix-store updates-nix-store split-string without-trailing-newline; };
|
|
buildCommand = ''
|
|
validate() {
|
|
local name=$1
|
|
local testout=$2
|
|
local assertion=$3
|
|
|
|
echo -n "... $name: " >&2
|
|
|
|
local rc=0
|
|
(out=$testout eval "$assertion") || rc=1
|
|
|
|
if [ "$rc" -eq 0 ]; then
|
|
echo "yes" >&2
|
|
else
|
|
echo "no" >&2
|
|
fi
|
|
|
|
return "$rc"
|
|
}
|
|
|
|
echo "checking whether patchShebangs works properly... ">&2
|
|
|
|
fail=
|
|
${lib.concatStringsSep "\n" (lib.mapAttrsToList (_: test: ''
|
|
validate "${test.name}" "${test}" ${lib.escapeShellArg test.assertion} || fail=1
|
|
'') tests)}
|
|
|
|
if [ "$fail" ]; then
|
|
echo "failed"
|
|
exit 1
|
|
else
|
|
echo "succeeded"
|
|
touch $out
|
|
fi
|
|
'';
|
|
}
|