Skip to content

Improve error message when discovery plugin is not available - #3032

Merged
gaborbernat merged 3 commits into
pypa:mainfrom
veeceey:fix/issue-2896-error-message
Feb 16, 2026
Merged

Improve error message when discovery plugin is not available#3032
gaborbernat merged 3 commits into
pypa:mainfrom
veeceey:fix/issue-2896-error-message

Conversation

@veeceey

@veeceey veeceey commented Feb 13, 2026

Copy link
Copy Markdown
Contributor

When VIRTUALENV_DISCOVERY is set via environment variable to a plugin that isn't installed, the previous behavior was a raw KeyError:

$ VIRTUALENV_DISCOVERY=pyenv virtualenv myenv
KeyError: 'pyenv'

This was confusing, especially for users running virtualenv through tox where the full traceback made it hard to understand the actual problem.

Now it raises a clear RuntimeError with a helpful message:

$ VIRTUALENV_DISCOVERY=pyenv virtualenv myenv
RuntimeError: discovery 'pyenv' is not available. Available discovery methods: builtin. Is the plugin installed?

Note that when --discovery is passed as a CLI argument, argparse already validates the choice and gives a reasonable error. This fix covers the env var (and ini config) code path where the value bypasses argparse's choice validation since it's set as a default.

Fixes #2896

@gaborbernat
gaborbernat enabled auto-merge (squash) February 15, 2026 05:13

@gaborbernat gaborbernat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to rebase to fix merge conflcits.

auto-merge was automatically disabled February 16, 2026 00:13

Head branch was pushed to by a user without write access

@veeceey
veeceey force-pushed the fix/issue-2896-error-message branch from 7dbe318 to 229c13a Compare February 16, 2026 00:13
veeceey and others added 3 commits February 15, 2026 18:55
When VIRTUALENV_DISCOVERY is set via environment variable to a plugin
that isn't installed, the previous behavior was a raw KeyError which
was confusing and unhelpful. Now raises a RuntimeError with a clear
message showing the requested plugin name, available discovery methods,
and a hint to check if the plugin is installed.

Fixes pypa#2896
The test_invalid_discovery_method_via_env test was expecting a ValueError
with the old error format from PR pypa#3031, but our changes raise a
RuntimeError with improved messaging. Update the test assertions to match.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@veeceey
veeceey force-pushed the fix/issue-2896-error-message branch from fbfedc3 to 20fc4d2 Compare February 16, 2026 02:55
@gaborbernat
gaborbernat merged commit 44bce57 into pypa:main Feb 16, 2026
99 of 100 checks passed
This was referenced Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crude error message if using VIRTUALENV_DISCOVERY=pyenv

2 participants