Skip to content
This repository was archived by the owner on Sep 25, 2025. It is now read-only.

Commit 40ffc2e

Browse files
committed
Search venv directories from bottom to top
1 parent e75c7d1 commit 40ffc2e

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The project was forked because I wanted to add a few features and improve the pl
1111
* The plugin did not work with the new version of fish, and I wanted to keep it as simple as possible.
1212
* The plugin looked for virtual environments in the current directory, but not in the specified sub-directory. For example I store virtual env in a sub-directory called `.venv`.
1313
* Apply virtual environment when run a terminal in a directory with a custom virtual environment.
14+
* Search for virtual environments in the parent directories from bottom to top, instead of top to bottom it was implemented in the original plugin.
1415

1516
## Install
1617
Installation with [Fisher][fisher]:

conf.d/autoenv.fish

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ function applyAutoenv
1414
and return
1515
# We start by splitting our CWD path into individual elements and iterating over each element.
1616
# If our CWD is `/opt/my/hovercraft/eels` we split it into a variable containing 4 entries:
17-
# `opt`, `my`, `hovercraft` and `eels`. We then build back up the path by iterating over the list
18-
# and adding each element to the previous one. (We start with `/opt`, then `/opt/my` and so on.)
19-
# During each run through the loop we test for a sub-directory/file called `bin/activate.fish`. If a
20-
# venv is found we go ahead and break out of the loop, otherwise continue. We go through all of this
21-
# instead of just checking the CWD to handle cases where the user moves into a sub-directory of the venv.
22-
for _dir in (string split -n '/' "$PWD")
23-
set -l _tree "$_tree/$_dir"
24-
17+
# `opt`, `my`, `hovercraft` and `eels`. We then iterate over each entry and check to see if it
18+
# contains a `bin/activate.fish` file. If a venv is found we go ahead and break out of the loop,
19+
# otherwise continue. We go through all of this instead of just checking the CWD to handle cases
20+
# where the user moves into a sub-directory of the venv.
21+
set _tree (pwd)
22+
while test $_tree != "/"
2523
set -l _activate (string join '/' "$_tree" "$autovenv_dir" "bin/activate.fish")
2624
set -l _activate (string replace -a "//" "/" "$_activate")
2725

@@ -33,6 +31,9 @@ function applyAutoenv
3331
end
3432
break
3533
end
34+
35+
36+
set _tree (dirname $_tree)
3637
end
3738
# If we're *not* in an active venv and the venv source dir exists we activate it and return.
3839
if test -z "$VIRTUAL_ENV" -a -e "$_source"
@@ -43,7 +44,7 @@ function applyAutoenv
4344
# Next we check to see if we're already in an active venv. If so we proceed with further tests.
4445
else if test -n "$VIRTUAL_ENV"
4546
# Check to see if our CWD is inside the venv directory.
46-
set _dir (string match -n "$VIRTUAL_ENV*" "$PWD")
47+
set _dir (string match -n "$VIRTUAL_ENV*" "$_source")
4748
# If we're no longer inside the venv dirctory deactivate it and return.
4849
if test -z "$_dir" -a ! -e "$_source"
4950
deactivate

0 commit comments

Comments
 (0)