justinmayer / virtualfish

Fish shell tool for managing Python virtual environments
MIT License
1.06k stars 100 forks source link

feat(auto_activation): After `vf connect`, de-activate when leaving PWD #164

Closed justinmayer closed 4 years ago

justinmayer commented 4 years ago

Closes #163

zx8 commented 4 years ago

Elegant solution, I like it! 😄

Seems to work as expected, but I'm being warned that my prompt doesn't contain the virutalenv (when it does):

zx8 · /tmp/tmp.W9VCUmkcb5
$ cd (mktemp -d)

zx8 · /var/folders/2r/fs7qk8ts6250vqgckkq90h8w0000gn/T/tmp.GikFEgyq7m
$ vf new foo
Creating foo via python3 …
Virtual environment activated but not shown in shell prompt. To fix, see:
<https://virtualfish.readthedocs.io/en/latest/install.html#customizing-your-fish-prompt>

(foo) zx8 · /var/folders/2r/fs7qk8ts6250vqgckkq90h8w0000gn/T/tmp.GikFEgyq7m
$ vf connect

(foo) zx8 · /var/folders/2r/fs7qk8ts6250vqgckkq90h8w0000gn/T/tmp.GikFEgyq7m
$ cd ..

zx8 · /var/folders/2r/fs7qk8ts6250vqgckkq90h8w0000gn/T
$ cd -
Virtual environment activated but not shown in shell prompt. To fix, see:
<https://virtualfish.readthedocs.io/en/latest/install.html#customizing-your-fish-prompt>

(foo) zx8 · /var/folders/2r/fs7qk8ts6250vqgckkq90h8w0000gn/T
$ 
justinmayer commented 4 years ago

Hmm. I can't replicate that. And I imagine it's unrelated to the changes in this PR. Perhaps try on current master and confirm you can replicate that warning there, too?

zx8 commented 4 years ago

Hmm, indeed, it happens on master too.

justinmayer commented 4 years ago

As you can see from the relevant commit, after activating a virtual environment (which also happens right after creating a new environment), the current value of fish_prompt is checked to see whether it contains the argument passed to __vf_activate, which presumably should be the equivalent of (basename $VIRTUAL_ENV). Here are some steps to run to try to validate this logic:

~ ➤  vf new foo
Creating foo via /usr/local/bin/python3 …
(foo) ~ ➤ string match -- "*bar*" (eval fish_prompt); echo $status
1              ⬅️  No matching line is displayed, with exit status = 1
(foo) ~ ➤ string match -- "*foo*" (eval fish_prompt); echo $status
(foo) ~ ➤      ⬅️  Matching line is displayed…
0              ⬅️  …with exit status = 0

As can be seen above, this functions as expected on my system. It would be nice to understand why it doesn't on yours. Perhaps experiment with the above steps and see if you can track down why you are seeing that warning?

zx8 commented 4 years ago

Thanks. Figured it out. It's because I have a separate function to set up my virtualenv prompt, and it is invoked --on-event fish_postexec.

I've added --on-event virtualenv_will_activate as an additional listener and all's well.

Sorry for the noise on this PR.