justinmayer / virtualfish

Fish shell tool for managing Python virtual environments
MIT License
1.07k stars 101 forks source link

Not autoactivating virtualenv when using tmux split pane #226

Closed yujinyuz closed 2 years ago

yujinyuz commented 2 years ago

Issue

So, I'm currently using asdf-vm with fish shell and tmux.

When I'm inside tmux, I typically press <Prefix> - to open a split pane below

bind-key - split-window -v -c '#{pane_current_path}'

Problem:

When I'm inside a directory where auto activation is enabled via vf connect myvenv,

type python produces ~/.virtualenvs/myvenv/python

$PATH looks like

~/.virtualenvs/myvenv/bin
~/.asdf/shims
~/.asdf/bin

But if I open a new split pane via <Prefix>-,

type python produces ~/.asdf/shims/python $PATH looks like

~/.asdf/shims
~/.asdf/bin
~/.virtualenvs/myvenv/bin

One reason probably is that ~/.config/fish/config.fish is sourced everytime I press <Prefix>-, thus running the source ~/.asdf/asdf.fish and re-modifying the path.

I'm not quite sure what to do at this point so if anyone could point me in the right direction, I would really appreciate it!

The $VIRTUAL_ENV variable is being set properly. The problem is with the $PATH since it gets jumbled thus not using the virtualenvs' path appropriately

ammgws commented 2 years ago

Have you tried guarding the call to source asdf in config.fish?

In my case fish is my login shell so guarding it like this would mean it is only sourced once:

if status is-login
    source ~/.asdf/asdf.fish
end
yujinyuz commented 2 years ago

@ammgws I did try adding if status is-login but it is still getting sourced when opening a new split using bind-key - split-window -v -c '#{pane_current_path}'

I think it's because the auto activation feature is relying on a PWD change variable

function __vfsupport_auto_activate --on-variable PWD

and since PWD is not being changed when opening a tmux split pane with the current path, it doesn't trigger the auto activation

yujinyuz commented 2 years ago

My current workaround is

# config.fish
# Load asdf-vm
if status is-login
    source ~/.asdf/asdf.fish
    emit asdf_loaded
end

Then I modified my auto_activation.fish

function __vfsupport_auto_activate --on-event asdf_loaded --on-variable PWD
    ....
    some code here
    ....
    if test $new_virtualenv_name != ""
        # if the virtualenv in the file is different, switch to it
        # if begin; not set -q VIRTUAL_ENV; or test $new_virtualenv_name != (basename $VIRTUAL_ENV); end
        if begin; or test $new_virtualenv_name != (basename $VIRTUAL_ENV); end
            vf activate $new_virtualenv_name
            set -g VF_AUTO_ACTIVATED $activation_root
        end
yujinyuz commented 2 years ago

For some reason, things started working again. I'm not sure how and why..