This works well for directories in recognized projects. When visiting a non-project directory with a venv, however, I run into a problem with pet-virtualenv-root returning the cached venv value even when leaving to another directory with no venv. I traced this to pet-virtualenv-root caching the venv location with a root key of nil (since pet-project-root returns nil). Since this root is used for all non-project dirs, pet-virtualenv-root returns an incorrect value for all the non-project dirs visited after the first one with a recognized venv.
For example, to reproduce with the above code, I can run:
cd /tmp
python -m venv venv
which python3 # /tmp/venv/bin/python3, as expected
cd /
which python3 # still /tmp/venv/bin/python3, expected system python
To fix, I just added a default of the current default-directory to the value of root used in pet-virtualenv-root, which causes it to correctly associate the found venv with the searched directory, regardless of whether it is in a project or not. Alternatively, we could not cache venvs with a root of nil, but I figured it would be better like this since root is only used by the caching mechanism here anyways.
Thanks for this package, I am using it in
eshell
to automatically "activate" venvs for the current eshell dir with the following snippet:This works well for directories in recognized projects. When visiting a non-project directory with a venv, however, I run into a problem with
pet-virtualenv-root
returning the cached venv value even when leaving to another directory with no venv. I traced this topet-virtualenv-root
caching the venv location with aroot
key ofnil
(sincepet-project-root
returns nil). Since this root is used for all non-project dirs,pet-virtualenv-root
returns an incorrect value for all the non-project dirs visited after the first one with a recognized venv.For example, to reproduce with the above code, I can run:
To fix, I just added a default of the current default-directory to the value of
root
used inpet-virtualenv-root
, which causes it to correctly associate the found venv with the searched directory, regardless of whether it is in a project or not. Alternatively, we could not cache venvs with aroot
of nil, but I figured it would be better like this sinceroot
is only used by the caching mechanism here anyways.