Skip to content

Commit 269599a

Browse files
esafakgoogle-labs-jules[bot]gaborbernat
authored
Fix: Ignore missing absolute paths for python discovery (#2907)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Bernát Gábor <[email protected]>
1 parent 29e9698 commit 269599a

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

docs/changelog/2870.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ignore missing absolute paths for python discovery - by :user:`esafak`

src/virtualenv/discovery/builtin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ def propose_interpreters( # noqa: C901, PLR0912, PLR0915
116116
try:
117117
os.lstat(spec.path) # Windows Store Python does not work with os.path.exists, but does for os.lstat
118118
except OSError:
119-
if spec.is_abs:
120-
raise
119+
pass
121120
else:
122121
exe_raw = os.path.abspath(spec.path)
123122
exe_id = fs_path_id(exe_raw)

tests/unit/discovery/test_discovery.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
import os
5+
import subprocess
56
import sys
67
from argparse import Namespace
78
from pathlib import Path
@@ -146,6 +147,61 @@ def test_returns_second_python_specified_when_more_than_one_is_specified_and_env
146147
assert result == mocker.sentinel.python_from_cli
147148

148149

150+
def test_absolute_path_does_not_exist(tmp_path):
151+
"""
152+
Test that virtualenv does not fail when an absolute path that does not exist is provided.
153+
"""
154+
# Create a command that uses an absolute path that does not exist
155+
# and a valid python executable.
156+
command = [
157+
sys.executable,
158+
"-m",
159+
"virtualenv",
160+
"-p",
161+
"/this/path/does/not/exist",
162+
"-p",
163+
sys.executable,
164+
str(tmp_path / "dest"),
165+
]
166+
167+
# Run the command
168+
process = subprocess.run(
169+
command,
170+
capture_output=True,
171+
text=True,
172+
check=False,
173+
)
174+
175+
# Check that the command was successful
176+
assert process.returncode == 0, process.stderr
177+
178+
179+
def test_absolute_path_does_not_exist_fails(tmp_path):
180+
"""
181+
Test that virtualenv fails when a single absolute path that does not exist is provided.
182+
"""
183+
# Create a command that uses an absolute path that does not exist
184+
command = [
185+
sys.executable,
186+
"-m",
187+
"virtualenv",
188+
"-p",
189+
"/this/path/does/not/exist",
190+
str(tmp_path / "dest"),
191+
]
192+
193+
# Run the command
194+
process = subprocess.run(
195+
command,
196+
capture_output=True,
197+
text=True,
198+
check=False,
199+
)
200+
201+
# Check that the command failed
202+
assert process.returncode != 0, process.stderr
203+
204+
149205
@pytest.mark.usefixtures("mock_get_interpreter")
150206
def test_returns_first_python_specified_when_no_env_var_is_specified(mocker, monkeypatch, session_app_data):
151207
monkeypatch.delenv("VIRTUALENV_PYTHON", raising=False)

0 commit comments

Comments
 (0)