Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable fallback to PATH from rbenv shims #1446

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions libexec/rbenv-exec
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#!/usr/bin/env bash
#
# Summary: Run an executable with the selected Ruby version
# Summary: Run an executable with the current Ruby version
#
# Usage: rbenv exec <command> [arg1 arg2...]
# Usage: rbenv exec <command> [<args>...]
#
# Runs an executable by first preparing PATH so that the selected Ruby
# version's `bin' directory is at the front.
# Runs an executable by first prepending the current Ruby version's
# `bin' directory to PATH.
#
# For example, if the currently selected Ruby version is 1.9.3-p327:
# rbenv exec bundle install
# For example, this invocation:
#
# is equivalent to:
# PATH="$RBENV_ROOT/versions/1.9.3-p327/bin:$PATH" bundle install
# RBENV_VERSION=3.1.2 rbenv exec bundle install
#
# roughly translates to:
#
# PATH="$RBENV_ROOT/versions/3.1.2/bin:$PATH" bundle install
#
# See `rbenv help which' for information about the fallback mechanism.

set -e
[ -n "$RBENV_DEBUG" ] && set -x
Expand Down Expand Up @@ -41,7 +45,7 @@ for script in "${scripts[@]}"; do
done

shift 1
if [ "$RBENV_VERSION" != "system" ]; then
if [ "$RBENV_BIN_PATH" = "${RBENV_ROOT}/versions/${RBENV_VERSION}/bin" ]; then
export PATH="${RBENV_BIN_PATH}:${PATH}"
fi
exec -a "$RBENV_COMMAND" "$RBENV_COMMAND_PATH" "$@"
38 changes: 31 additions & 7 deletions libexec/rbenv-which
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
#
# Displays the full path to the executable that rbenv will invoke when
# you run the given command.
#
# If GEM_HOME is set, the result will be `$GEM_HOME/bin/<command>' if
# it exists.
#
# If the current Ruby version is "system", the command will be looked up
# in PATH.
#
# For other Ruby versions, the command is first looked up in
# `$RBENV_ROOT/versions/$RBENV_VERSION/bin'. If it does not exist and if
# the command is not one of the core Ruby executables (e.g. "ruby",
# "gem", "bundle"), it will be looked up in PATH.

set -e
[ -n "$RBENV_DEBUG" ] && set -x
Expand Down Expand Up @@ -36,15 +47,28 @@ fi

RBENV_VERSION="${RBENV_VERSION:-$(rbenv-version-name)}"

if [ "$RBENV_VERSION" = "system" ]; then
PATH="$(remove_from_path "${RBENV_ROOT}/shims")" \
RBENV_COMMAND_PATH="$(command -v "$RBENV_COMMAND" || true)"
if [[ -n "$GEM_HOME" && -x "${GEM_HOME}/bin/${RBENV_COMMAND}" ]]; then
RBENV_COMMAND_PATH="${GEM_HOME}/bin/${RBENV_COMMAND}"
elif [ "$RBENV_VERSION" = "system" ]; then
PATH="$(remove_from_path "${RBENV_ROOT}/shims")" RBENV_COMMAND_PATH="$(type -P "$RBENV_COMMAND" || true)"
else
RBENV_COMMAND_PATH="${RBENV_ROOT}/versions/${RBENV_VERSION}/bin/${RBENV_COMMAND}"
fi

if [[ ! -x "$RBENV_COMMAND_PATH" && -n "$GEM_HOME" && -x "${GEM_HOME}/bin/${RBENV_COMMAND}" ]]; then
RBENV_COMMAND_PATH="${GEM_HOME}/bin/${RBENV_COMMAND}"
if [ ! -x "$RBENV_COMMAND_PATH" ]; then
# Look up the command in PATH as fallback.
case "$RBENV_COMMAND" in
ruby | rake | gem | bundle | bundler | irb | rdoc | ri )
# Do not allow fallback to PATH for core Ruby commands. If any of these are missing
# in the current Ruby version, error out instead of allowing a potentially unrelated
# Ruby version to activate and potentially muck up the current project.
;;
* )
if PATH="$(remove_from_path "${RBENV_ROOT}/shims")" RBENV_COMMAND_FALLBACK="$(type -P "$RBENV_COMMAND")"; then
RBENV_COMMAND_PATH="$RBENV_COMMAND_FALLBACK"
fi
unset RBENV_COMMAND_FALLBACK
;;
esac
fi
fi

OLDIFS="$IFS"
Expand Down
32 changes: 28 additions & 4 deletions test/exec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ create_executable() {
}

@test "fails with invalid version" {
export RBENV_VERSION="2.0"
run rbenv-exec ruby -v
RBENV_VERSION="2.0" run rbenv-exec ruby -v
assert_failure "rbenv: version \`2.0' is not installed (set by RBENV_VERSION environment variable)"
}

Expand All @@ -29,6 +28,7 @@ create_executable() {
}

@test "completes with names of executables" {
# shellcheck disable=SC2030,SC2031
export RBENV_VERSION="2.0"
create_executable "ruby" "#!/bin/sh"
create_executable "rake" "#!/bin/sh"
Expand All @@ -49,13 +49,13 @@ hellos=(\$(printf "hello\\tugly world\\nagain"))
echo HELLO="\$(printf ":%s" "\${hellos[@]}")"
SH

export RBENV_VERSION=system
IFS=$' \t\n' run rbenv-exec env
RBENV_VERSION=system IFS=$' \t\n' run rbenv-exec env
assert_success
assert_line "HELLO=:hello:ugly:world:again"
}

@test "forwards all arguments" {
# shellcheck disable=SC2030,SC2031
export RBENV_VERSION="2.0"
create_executable "ruby" <<SH
#!$BASH
Expand All @@ -78,7 +78,31 @@ ${RBENV_ROOT}/versions/2.0/bin/ruby
OUT
}

@test "prepends Ruby to PATH" {
# shellcheck disable=SC2030,SC2031
export RBENV_VERSION="2.0"
Fixed Show fixed Hide fixed
create_executable "ruby" <<SH
#!$BASH
echo \$PATH
SH

run rbenv-exec ruby
assert_success
[[ $output == "${RBENV_ROOT}/versions/${RBENV_VERSION}/bin:"* ]]
}

@test "does not modify PATH for fallback" {
# shellcheck disable=SC2030,SC2031
export RBENV_VERSION="2.0"
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
create_executable "ruby" "#!/bin/sh"

# shellcheck disable=SC2016
run rbenv-exec bash -c 'echo $PATH'
Fixed Show fixed Hide fixed
assert_success "$PATH"
}

@test "supports ruby -S <cmd>" {
# shellcheck disable=SC2030,SC2031
export RBENV_VERSION="2.0"

# emulate `ruby -S' behavior
Expand Down