about summary refs log tree commit diff
path: root/pkgs/test/stdenv
diff options
context:
space:
mode:
authoremilylange <git@emilylange.de>2023-09-16 14:20:56 +0200
committeremilylange <git@emilylange.de>2023-09-16 14:20:56 +0200
commitc47f2452530b9cdb58de578f1c44c37e8aa616f0 (patch)
tree0ea9eb10b68f9e468a4191c62037c96b389e8aef /pkgs/test/stdenv
parent7f935747068f59c55c6102676926ea418ebe1c64 (diff)
patch-shebangs: fix crash with shebang without trailing newline
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'.
```
Diffstat (limited to 'pkgs/test/stdenv')
-rw-r--r--pkgs/test/stdenv/patch-shebangs.nix17
1 files changed, 16 insertions, 1 deletions
diff --git a/pkgs/test/stdenv/patch-shebangs.nix b/pkgs/test/stdenv/patch-shebangs.nix
index 888d4a53a2733..db9ca2fcaafef 100644
--- a/pkgs/test/stdenv/patch-shebangs.nix
+++ b/pkgs/test/stdenv/patch-shebangs.nix
@@ -72,11 +72,26 @@ let
       };
     };
 
+    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; };
+  passthru = { inherit (tests) bad-shebang ignores-nix-store updates-nix-store split-string without-trailing-newline; };
   buildCommand = ''
     validate() {
       local name=$1