Use readarray in bash v4+ to avoid rbenv init hanging

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.
This commit is contained in:
Mislav Marohnić 2025-01-07 23:40:54 +01:00
parent efeab7f8ee
commit 2e3ef01abb
No known key found for this signature in database
2 changed files with 12 additions and 2 deletions

View File

@ -26,7 +26,12 @@ elif [ "$1" = "--no-sh" ]; then
shift shift
fi 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 shopt -s nullglob

View File

@ -154,7 +154,12 @@ print_usage() {
if [ "$1" = "--complete-commands" ]; then if [ "$1" = "--complete-commands" ]; then
command_prefix="${2:-}" command_prefix="${2:-}"
seen=() 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 shopt -s nullglob
for path in "${paths[@]}"; do for path in "${paths[@]}"; do
for command in "${path}/rbenv-${command_prefix}"*; do for command in "${path}/rbenv-${command_prefix}"*; do