Skip to content

Commit

Permalink
Use readarray in bash v4+ to avoid rbenv init hanging
Browse files Browse the repository at this point in the history
For just a handful of people, rbenv init would hang indefinitely. Turning on
debugging output suggested that the `read` expression to split PATH into a bash
array was hanging, but I could never reproduce this myself. Instead, this uses
bash v4+ `readarray` if it's available, or falls back to bash v3 `read` with the
default DELIM being a newline character. This will cause a regression if any PATH
entries contain a literal newline character, but hopefully people are not relying
on such paths.
  • Loading branch information
mislav committed Jan 7, 2025
1 parent efeab7f commit 2e3ef01
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion libexec/rbenv-commands
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ elif [ "$1" = "--no-sh" ]; then
shift
fi

IFS=: read -d '' -r -a paths <<<"$PATH" || true
if [ "$(type -t readarray)" = "builtin" ]; then
readarray -d : -t paths < <(printf "%s" "$PATH")
else
# bash 3.x compatibility
IFS=: read -r -a paths <<<"$PATH" || true
fi

shopt -s nullglob

Expand Down
7 changes: 6 additions & 1 deletion libexec/rbenv-help
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ print_usage() {
if [ "$1" = "--complete-commands" ]; then
command_prefix="${2:-}"
seen=()
IFS=: read -d '' -r -a paths <<<"$PATH" || true
if [ "$(type -t readarray)" = "builtin" ]; then
readarray -d : -t paths < <(printf "%s" "$PATH")
else
# bash 3.x compatibility
IFS=: read -r -a paths <<<"$PATH" || true
fi
shopt -s nullglob
for path in "${paths[@]}"; do
for command in "${path}/rbenv-${command_prefix}"*; do
Expand Down

0 comments on commit 2e3ef01

Please sign in to comment.