Commit graph

4 commits

Author SHA1 Message Date
emilylange c47f245253
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'.
```
2023-09-16 14:20:56 +02:00
Ivan Trubach ebd4619053 patch-shebangs: add a flag to update shebangs with store paths
This change adds a flag to update shebang paths that point to the Nix
store. This is particularly useful when a cross-compiled package uses
same script at compile-time and run-time, but the interpreter must be
changed since hostPlatform != buildPlatform.
2023-06-19 09:01:21 +03:00
Artturin 883daacbaa tests.stdenv: add hooks.patch-shebangs.split-string & tweak tests 2023-02-17 22:23:35 +02:00
Artturin 8876a5c91f tests.stdenv: move patch-shebangs test 2023-02-17 22:23:33 +02:00
Renamed from pkgs/test/patch-shebangs/default.nix (Browse further)