diff --git a/libexec/pyenv-version-file b/libexec/pyenv-version-file index 91d8b0d3..8b1fe30b 100755 --- a/libexec/pyenv-version-file +++ b/libexec/pyenv-version-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" diff --git a/test/version-file.bats b/test/version-file.bats index e2fb941c..3c0d40a2 100644 --- a/test/version-file.bats +++ b/test/version-file.bats @@ -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" +}