From 399f43c3d5fe91db3a2d6968d8ec30a4d6fbf285 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Sat, 7 Apr 2018 22:38:45 -0400 Subject: [PATCH 1/3] nix-shell: Fixes use with ruby shebangs. The ported code in 80ebc553eca19dafc64c47420cd49ddd506bc9b7 was incorrectly ported. ``` - $envCommand = "exec $execArgs $interpreter -e 'load(\"$script\")' -- ${\(join ' ', (map shellEscape, @savedArgs))}"; ... + envCommand = (format("exec %1% %2% -e 'load(\"%3%\") -- %4%") % execArgs % interpreter % script % joined.str()).str(); ``` The single-quote finishing the small ruby snippet was lost in translation. --- src/nix-build/nix-build.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index cf628519c..a63b3e07a 100755 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -212,7 +212,7 @@ void mainWrapped(int argc, char * * argv) // read the shebang to understand which packages to read from. Since // this is handled via nix-shell -p, we wrap our ruby script execution // in ruby -e 'load' which ignores the shebangs. - envCommand = (format("exec %1% %2% -e 'load(\"%3%\") -- %4%") % execArgs % interpreter % script % joined.str()).str(); + envCommand = (format("exec %1% %2% -e 'load(\"%3%\")' -- %4%") % execArgs % interpreter % script % joined.str()).str(); } else { envCommand = (format("exec %1% %2% %3% %4%") % execArgs % interpreter % script % joined.str()).str(); } From 9478f886810b8db87b7ebfe14343f77159c86a24 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Sun, 8 Apr 2018 00:45:28 -0400 Subject: [PATCH 2/3] Adds `nix-shell` test for special-cased ruby interpreter. The test fakes the interpreter only to verify the arguments it would be given. --- tests/nix-shell.sh | 9 +++++++++ tests/shell.nix | 7 +++++++ tests/shell.shebang.rb | 7 +++++++ 3 files changed, 23 insertions(+) create mode 100644 tests/shell.shebang.rb diff --git a/tests/nix-shell.sh b/tests/nix-shell.sh index 063e97ce2..7f14b6c31 100644 --- a/tests/nix-shell.sh +++ b/tests/nix-shell.sh @@ -39,3 +39,12 @@ chmod a+rx $TEST_ROOT/shell.shebang.sh output=$($TEST_ROOT/shell.shebang.sh abc def) [ "$output" = "foo bar abc def" ] + +# Test nix-shell shebang mode for ruby +# This uses a fake interpreter that returns the arguments passed +# This, in turn, verifies the `rc` script is valid and the `load()` script (given using `-e`) is as expected. +sed -e "s|@ENV_PROG@|$(type -p env)|" shell.shebang.rb > $TEST_ROOT/shell.shebang.rb +chmod a+rx $TEST_ROOT/shell.shebang.rb + +output=$($TEST_ROOT/shell.shebang.rb abc ruby) +[ "$output" = '-e load("'"$TEST_ROOT"'/shell.shebang.rb") -- abc ruby' ] diff --git a/tests/shell.nix b/tests/shell.nix index 5845d36fc..eb39f9039 100644 --- a/tests/shell.nix +++ b/tests/shell.nix @@ -45,5 +45,12 @@ let pkgs = rec { bash = shell; + # ruby "interpreter" that outputs "$@" + ruby = runCommand "ruby" {} '' + mkdir -p $out/bin + echo 'printf -- "$*"' > $out/bin/ruby + chmod a+rx $out/bin/ruby + ''; + inherit pkgs; }; in pkgs diff --git a/tests/shell.shebang.rb b/tests/shell.shebang.rb new file mode 100644 index 000000000..ea3953ae2 --- /dev/null +++ b/tests/shell.shebang.rb @@ -0,0 +1,7 @@ +#! @ENV_PROG@ nix-shell +#! ruby +#! nix-shell -I nixpkgs=shell.nix --no-substitute +#! nix-shell --pure -p ruby -i ruby + +# Contents doesn't matter. +abort("This shouldn't be executed.") From a4c9b2595e6e22250e4e744a35fff3c7f9cd85d9 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Mon, 9 Apr 2018 09:36:54 -0400 Subject: [PATCH 3/3] Hardcodes `nix-shell` instead of `/usr/bin/env nix-shell`... ... in the ruby shebang test. --- tests/nix-shell.sh | 2 +- tests/shell.shebang.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/nix-shell.sh b/tests/nix-shell.sh index 7f14b6c31..d25c456ce 100644 --- a/tests/nix-shell.sh +++ b/tests/nix-shell.sh @@ -43,7 +43,7 @@ output=$($TEST_ROOT/shell.shebang.sh abc def) # Test nix-shell shebang mode for ruby # This uses a fake interpreter that returns the arguments passed # This, in turn, verifies the `rc` script is valid and the `load()` script (given using `-e`) is as expected. -sed -e "s|@ENV_PROG@|$(type -p env)|" shell.shebang.rb > $TEST_ROOT/shell.shebang.rb +sed -e "s|@SHELL_PROG@|$(type -p nix-shell)|" shell.shebang.rb > $TEST_ROOT/shell.shebang.rb chmod a+rx $TEST_ROOT/shell.shebang.rb output=$($TEST_ROOT/shell.shebang.rb abc ruby) diff --git a/tests/shell.shebang.rb b/tests/shell.shebang.rb index ea3953ae2..ea67eb09c 100644 --- a/tests/shell.shebang.rb +++ b/tests/shell.shebang.rb @@ -1,4 +1,4 @@ -#! @ENV_PROG@ nix-shell +#! @SHELL_PROG@ #! ruby #! nix-shell -I nixpkgs=shell.nix --no-substitute #! nix-shell --pure -p ruby -i ruby