fix: pyenv-rehash | do NOT create conda_exclusion_list if conda is not installed.

I was doing some debugging with PYENV_DEBUG=1 and noticed that a lot of
work was being done in conda.bash, even though I had not installed any
conda versions like `mambaforge`.

The solution is pretty simple, put all the code inside an if-block.
This aligns with what @varikin found in his pull request #3037

Additionally, I have refactored the code slightly for readability
(created function `build_conda_exclusion_list`), and added comments
which should make it easier to understand what's going on.

This commit simplifies debugging and should reduce the execution time of

```bash
eval "$(pyenv init -)"
```

slightly.
This commit is contained in:
Christian Fredrik Johnsen 2024-12-23 14:47:36 +03:00 committed by Ivan Pozdeev
parent 4c6b0e9c3b
commit 09fbed1d4f

View File

@ -11,18 +11,34 @@ conda_exists() {
[ -n "${condas}" ]
}
if conda_exists; then
# Reads the list of `blacklisted` conda binaries
# from `conda.d/default.list` and creates a function
# `conda_shim` to skip creating shims for those binaries.
build_conda_exclusion_list() {
shims=()
shopt -s nullglob
for shim in $(cat "${BASH_SOURCE%/*}/conda.d/"*".list" | sort | uniq | sed -e 's/#.*$//' | sed -e '/^[[:space:]]*$/d'); do
for shim in $(cat "${BASH_SOURCE%/*}/conda.d/"*".list" | sort -u | sed -e 's/#.*$//' | sed -e '/^[[:space:]]*$/d'); do
if [ -n "${shim##*/}" ]; then
shims[${#shims[*]}]="${shim})return 0;;"
fi
done
shopt -u nullglob
eval "conda_shim(){ case \"\${1##*/}\" in ${shims[@]} *)return 1;;esac;}"
eval \
"conda_shim() {
case \"\${1##*/}\" in
${shims[@]}
*) return 1;;
esac
}"
}
# override `make_shims` to avoid conflict between pyenv-virtualenv's `envs.bash`
# https://github.com/pyenv/pyenv-virtualenv/blob/v20160716/etc/pyenv.d/rehash/envs.bash
# The only difference between this `make_shims` and the `make_shims` defined
# in `libexec/pyenv-rehash` is that this one calls `conda_shim` to check
# if shim is blacklisted. If blacklisted -> skip creating shim.
make_shims() {
local file shim
for file do
@ -53,6 +69,6 @@ deregister_conda_shims() {
fi
}
if conda_exists; then
build_conda_exclusion_list
deregister_conda_shims
fi