version-file: Fix infinite loop for relative paths

find_local_version_file reduces its target with ${root%/*}, but that leaves a slash-less relative argument such as "." unchanged, leading to an infinite loop

Signed-off-by: 付典 <fudianchn@gmail.com>
This commit is contained in:
付典 2026-08-08 20:35:35 +03:00 committed by Ivan Pozdeev
parent b7e7adf4ec
commit 195b9fe940
2 changed files with 15 additions and 0 deletions

View file

@ -8,6 +8,9 @@ target_dir="$1"
find_local_version_file() {
local root="$1"
# Nonexistent paths are UB as of this writing.
# Relative ones cause a failure but absolute ones don't
[[ $root != /* ]] && root=$(CDPATH= cd -- "$root" && pwd)
while ! [[ "$root" =~ ^//[^/]*$ ]]; do
if [ -f "${root}/.python-version" ]; then
echo "${root}/.python-version"

View file

@ -73,3 +73,15 @@ create_file() {
run pyenv-version-file "$PWD"
assert_failure ""
}
@test "walks up beyond cwd for a relative path" {
create_file ".python-version"
mkdir -p project/subdir
cd project
# Run with CPU-time limit because this used to loop forever
run bash -c 'ulimit -t 5; pyenv-version-file .'
assert_success "${PYENV_TEST_DIR}/.python-version"
run pyenv-version-file ./subdir
assert_success "${PYENV_TEST_DIR}/.python-version"
}