justinmayer / virtualfish

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

check_fish_version() and `tabs` in fish configuration #238

Closed melomac closed 9 months ago

melomac commented 1 year ago

On launching vf for the first time on macOS, I am getting an error:

noar@freakonomix ~> vf
Traceback (most recent call last):
  File "/Users/noar/Library/Python/3.9/bin/vf", line 8, in <module>
    sys.exit(main())
  File "/Users/noar/Library/Python/3.9/lib/python/site-packages/virtualfish/loader/cli.py", line 47, in main
    check_fish_version()
  File "/Users/noar/Library/Python/3.9/lib/python/site-packages/virtualfish/loader/cli.py", line 35, in check_fish_version
    if version.parse(fish_version) < version.parse(minimum_fish_version):
  File "/Users/noar/Library/Python/3.9/lib/python/site-packages/packaging/version.py", line 52, in parse
    return Version(version)
  File "/Users/noar/Library/Python/3.9/lib/python/site-packages/packaging/version.py", line 198, in __init__
    raise InvalidVersion(f"Invalid version: '{version}'")
packaging.version.InvalidVersion: Invalid version: '                                                                                                                                                                                                                       3.6.1'                                           

It turns out the shell command to get current fish version also intercepts control characters, apparently related to the size of the window:

import subprocess
from packaging import version
cmd = ["fish", "-c", "echo $version"]
fish_version = subprocess.check_output(cmd)
repr(fish_version)

I am leaving the output in bytes on purpose here (window dimensions 267x100):

"b'\\r\\x1b[3g\\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH\\r3.6.1-794-g36a7924fa\\n'"

Window dimensions 80x24:

"b'\\r\\x1b[3g\\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH    \\x1bH\\r3.6.1-794-g36a7924fa\\n'"

I am not sure where the root cause is, but an easy workaround is to run fish with the --no-config/-N option, ex:

import subprocess
from packaging import version
cmd = ["fish", "-N", "-c", "echo $version"]
fish_version = subprocess.check_output(cmd)
repr(fish_version)

Returns the expected output:

"b'3.6.1-794-g36a7924fa\\n'"

Would you please be so kind to consider integrating the following change to virtualfish?

diff --git a/virtualfish/loader/cli.py b/virtualfish/loader/cli.py
index d292ff2..5582c04 100644
--- a/virtualfish/loader/cli.py
+++ b/virtualfish/loader/cli.py
@@ -28,7 +28,7 @@ def check_fish_version():
         import subprocess
         from packaging import version

-        cmd = ["fish", "-c", "echo $version"]
+        cmd = ["fish", "-N", "-c", "echo $version"]
         fish_version = subprocess.check_output(cmd).decode("utf-8").strip()
         # Remove any extraneous hyphen-suffixed bits
         fish_version = fish_version.partition("-")[0]
melomac commented 1 year ago

I figured out where my configuration is messing up with fish output. To reproduce the problem, you can add this to ~/.config/fish/config.fish file:

tabs -4
justinmayer commented 9 months ago

Thank you for the detailed information, @melomac. The just-released VirtualFish 2.5.6 contains the fix you suggested. Thanks again! 😁