patch-shebangs: fix env -S with only one argument

In case `patchShebangs` encounters an `env -S` interpreter with only one
argument following, it would duplicate that argument and most likely invalidate
the resulting interpreter line.

Reproducer:

```nix
(import <nixpkgs> {}).writeTextFile {
  name = "patch-shebangs-env-s";
  text = ''
    #!/bin/env -S bash
  '';
  executable = true;
  checkPhase = ''
    patchShebangs $out
  '';
}
```

The resulting file would contain

```
#!/nix/store/pw…fk-coreutils-9.5/bin/env -S /nix/store/4f…g60-bash-5.2p37/bin/bash bash
```

instead of the correct

```
#!/nix/store/pw…fk-coreutils-9.5/bin/env -S /nix/store/4f…g60-bash-5.2p37/bin/bash
```
This commit is contained in:
Fabian Möller
2025-01-31 08:29:56 +01:00
parent 8a2dfc70b0
commit c5b890c413

View File

@@ -89,7 +89,7 @@ patchShebangs() {
if [[ "$oldPath" == *"/bin/env" ]]; then
if [[ $arg0 == "-S" ]]; then
arg0=${args%% *}
args=${args#* }
[[ "$args" == *" "* ]] && args=${args#* } || args=
newPath="$(PATH="${!pathName}" type -P "env" || true)"
args="-S $(PATH="${!pathName}" type -P "$arg0" || true) $args"