Skip to content

Commit

Permalink
Only allow virtual environment interpreters from the PATH if a `pyven…
Browse files Browse the repository at this point in the history
…v.cfg` can be found
  • Loading branch information
zanieb committed Feb 4, 2025
1 parent ac10042 commit 1467172
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ fn interpreter_satisfies_environment_preference(
/// This is useful as a pre-filtering step. Use of [`interpreter_satisfies_environment_preference`]
/// is required to determine if an [`Interpreter`] satisfies the preference.
///
/// The interpreter path is only used for debug messages.
/// For [`PythonSource::SearchPath`], this uses the interpreter path to sniff if the executable is
/// present in a virtual environment by searching for a `pyvenv.cfg` file.
fn source_satisfies_environment_preference(
source: PythonSource,
interpreter_path: &Path,
Expand All @@ -723,7 +724,16 @@ fn source_satisfies_environment_preference(
EnvironmentPreference::Any => true,
EnvironmentPreference::OnlyVirtual => {
if source.is_maybe_virtualenv() {
true
if matches!(source, PythonSource::SearchPath) {
interpreter_path
.parent()
.and_then(Path::parent)
.is_some_and(|path| {
path.join("pyvenv.cfg").try_exists().unwrap_or_default()
})
} else {
true
}
} else {
debug!(
"Ignoring Python interpreter at `{}`: only virtual environments allowed",
Expand Down Expand Up @@ -1624,21 +1634,16 @@ impl PythonSource {
}

/// Whether this source **could** be a virtual environment.
///
/// This excludes the [`PythonSource::SearchPath`] although it could be in a virtual
/// environment; pragmatically, that's not common and saves us from querying a bunch of system
/// interpreters for no reason. It seems dubious to consider an interpreter in the `PATH` as a
/// target virtual environment if it's not discovered through our virtual environment-specific
/// patterns.
pub(crate) fn is_maybe_virtualenv(self) -> bool {
match self {
Self::ProvidedPath
| Self::ActiveEnvironment
| Self::DiscoveredEnvironment
| Self::CondaPrefix
| Self::BaseCondaPrefix
| Self::ParentInterpreter => true,
Self::Managed | Self::SearchPath | Self::Registry | Self::MicrosoftStore => false,
| Self::ParentInterpreter
| Self::SearchPath => true,
Self::Managed | Self::Registry | Self::MicrosoftStore => false,
}
}

Expand Down

0 comments on commit 1467172

Please sign in to comment.