Skip to content

Commit 683e5db

Browse files
esafakgoogle-labs-jules[bot]pre-commit-ci[bot]
authored
fix: handle StopIteration in discovery (#2913)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 153227f commit 683e5db

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

docs/changelog/2493.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When no discovery plugins are found, the application would crash with a StopIteration.
2+
This change catches the StopIteration and raises a RuntimeError with a more informative message.
3+
Contributed by :user:`esafak`.

src/virtualenv/run/plugin/discovery.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ def get_discover(parser, args):
1616
choices = _get_default_discovery(discover_types)
1717
# prefer the builtin if present, otherwise fallback to first defined type
1818
choices = sorted(choices, key=lambda a: 0 if a == "builtin" else 1)
19+
try:
20+
default_discovery = next(iter(choices))
21+
except StopIteration as e:
22+
msg = "No discovery plugin found. Try reinstalling virtualenv to fix this issue."
23+
raise RuntimeError(msg) from e
1924
discovery_parser.add_argument(
2025
"--discovery",
2126
choices=choices,
22-
default=next(iter(choices)),
27+
default=default_discovery,
2328
required=False,
2429
help="interpreter discovery method",
2530
)

tests/unit/config/test___main__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ def test_fail_no_traceback(raise_on_session_done, tmp_path, capsys):
5252
assert err == "err\n"
5353

5454

55+
def test_discovery_fails_no_discovery_plugin(mocker, tmp_path, capsys):
56+
mocker.patch("virtualenv.run.plugin.discovery.Discovery.entry_points_for", return_value={})
57+
with pytest.raises(SystemExit) as context:
58+
run_with_catch([str(tmp_path)])
59+
assert context.value.code == 1
60+
out, err = capsys.readouterr()
61+
assert "RuntimeError: No discovery plugin found. Try reinstalling virtualenv to fix this issue." in out
62+
assert not err
63+
64+
5565
def test_fail_with_traceback(raise_on_session_done, tmp_path, capsys):
5666
raise_on_session_done(TypeError("something bad"))
5767

0 commit comments

Comments
 (0)