Skip to content

Commit 8a475ce

Browse files
authored
Fix edge case with windows gitbash and environment variables having backslash (#138)
* Fix edge case with windows gitbash and environment variables having backslash
1 parent 2fabe41 commit 8a475ce

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

news/138.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix edge case with windows git bash and environment variables having backslash.

src/pythonfinder/environment.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,27 @@ def is_type_checking():
1717
return TYPE_CHECKING
1818

1919

20+
def possibly_convert_to_windows_style_path(path):
21+
if not isinstance(path, str):
22+
path = str(path)
23+
# Check if the path is in Unix-style (Git Bash)
24+
if os.name == 'nt':
25+
if path.startswith('/'):
26+
drive, tail = re.match(r"^/([a-zA-Z])/(.*)", path).groups()
27+
revised_path = drive.upper() + ":\\" + tail.replace('/', '\\')
28+
return revised_path
29+
elif path.startswith('\\'):
30+
drive, tail = re.match(r"^\\([a-zA-Z])\\(.*)", path).groups()
31+
revised_path = drive.upper() + ":\\" + tail.replace('\\', '\\')
32+
return revised_path
33+
34+
return path
35+
36+
2037
PYENV_ROOT = os.path.expanduser(
2138
os.path.expandvars(os.environ.get("PYENV_ROOT", "~/.pyenv"))
2239
)
23-
# Check if the path is in Unix-style (Git Bash)
24-
if PYENV_ROOT.startswith('/') and os.name == 'nt':
25-
# Convert to Windows-style path
26-
drive, tail = re.match(r"^/([a-zA-Z])/(.*)", PYENV_ROOT).groups()
27-
PYENV_ROOT = drive.upper() + ":\\" + tail.replace('/', '\\')
40+
PYENV_ROOT = possibly_convert_to_windows_style_path(PYENV_ROOT)
2841
PYENV_INSTALLED = shutil.which("pyenv") != None
2942
ASDF_DATA_DIR = os.path.expanduser(
3043
os.path.expandvars(os.environ.get("ASDF_DATA_DIR", "~/.asdf"))

src/pythonfinder/models/path.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,7 @@ def _run_setup(self) -> SystemPath:
176176
if self.global_search and "PATH" in os.environ:
177177
path_order = path_order + os.environ["PATH"].split(os.pathsep)
178178
path_order = list(dedup(path_order))
179-
path_instances = [
180-
ensure_path(p.strip('"'))
181-
for p in path_order
182-
]
179+
path_instances = [ensure_path(p.strip('"')) for p in path_order]
183180
self.paths.update(
184181
{
185182
p.as_posix(): PathEntry.create(
@@ -199,15 +196,16 @@ def _run_setup(self) -> SystemPath:
199196
if self.check_for_asdf() and "asdf" not in self.finders:
200197
self._setup_asdf()
201198
venv = os.environ.get("VIRTUAL_ENV")
199+
if venv:
200+
venv = ensure_path(venv)
202201
if os.name == "nt":
203202
bin_dir = "Scripts"
204203
else:
205204
bin_dir = "bin"
206205
if venv and (self.system or self.global_search):
207-
p = ensure_path(venv)
208-
path_order = [(p / bin_dir).as_posix(), *self.path_order]
206+
path_order = [(venv / bin_dir).as_posix(), *self.path_order]
209207
self.path_order = path_order
210-
self.paths[p] = self.get_path(p.joinpath(bin_dir))
208+
self.paths[venv] = self.get_path(venv.joinpath(bin_dir))
211209
if self.system:
212210
syspath = Path(sys.executable)
213211
syspath_bin = syspath.parent

src/pythonfinder/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from packaging.version import InvalidVersion, Version
1515

16-
from .environment import PYENV_ROOT
16+
from .environment import PYENV_ROOT, possibly_convert_to_windows_style_path
1717
from .exceptions import InvalidPythonVersion
1818

1919
version_re_str = (
@@ -234,7 +234,7 @@ def ensure_path(path: Path | str) -> Path:
234234
:type path: str or :class:`~pathlib.Path`
235235
:return: A fully expanded Path object.
236236
"""
237-
237+
path = possibly_convert_to_windows_style_path(path)
238238
if isinstance(path, Path):
239239
return path
240240
path = Path(os.path.expandvars(path))

0 commit comments

Comments
 (0)