Skip to content

Commit 1467172

Browse files
committed
Only allow virtual environment interpreters from the PATH if a pyvenv.cfg can be found
1 parent ac10042 commit 1467172

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

crates/uv-python/src/discovery.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ fn interpreter_satisfies_environment_preference(
713713
/// This is useful as a pre-filtering step. Use of [`interpreter_satisfies_environment_preference`]
714714
/// is required to determine if an [`Interpreter`] satisfies the preference.
715715
///
716-
/// The interpreter path is only used for debug messages.
716+
/// For [`PythonSource::SearchPath`], this uses the interpreter path to sniff if the executable is
717+
/// present in a virtual environment by searching for a `pyvenv.cfg` file.
717718
fn source_satisfies_environment_preference(
718719
source: PythonSource,
719720
interpreter_path: &Path,
@@ -723,7 +724,16 @@ fn source_satisfies_environment_preference(
723724
EnvironmentPreference::Any => true,
724725
EnvironmentPreference::OnlyVirtual => {
725726
if source.is_maybe_virtualenv() {
726-
true
727+
if matches!(source, PythonSource::SearchPath) {
728+
interpreter_path
729+
.parent()
730+
.and_then(Path::parent)
731+
.is_some_and(|path| {
732+
path.join("pyvenv.cfg").try_exists().unwrap_or_default()
733+
})
734+
} else {
735+
true
736+
}
727737
} else {
728738
debug!(
729739
"Ignoring Python interpreter at `{}`: only virtual environments allowed",
@@ -1624,21 +1634,16 @@ impl PythonSource {
16241634
}
16251635

16261636
/// Whether this source **could** be a virtual environment.
1627-
///
1628-
/// This excludes the [`PythonSource::SearchPath`] although it could be in a virtual
1629-
/// environment; pragmatically, that's not common and saves us from querying a bunch of system
1630-
/// interpreters for no reason. It seems dubious to consider an interpreter in the `PATH` as a
1631-
/// target virtual environment if it's not discovered through our virtual environment-specific
1632-
/// patterns.
16331637
pub(crate) fn is_maybe_virtualenv(self) -> bool {
16341638
match self {
16351639
Self::ProvidedPath
16361640
| Self::ActiveEnvironment
16371641
| Self::DiscoveredEnvironment
16381642
| Self::CondaPrefix
16391643
| Self::BaseCondaPrefix
1640-
| Self::ParentInterpreter => true,
1641-
Self::Managed | Self::SearchPath | Self::Registry | Self::MicrosoftStore => false,
1644+
| Self::ParentInterpreter
1645+
| Self::SearchPath => true,
1646+
Self::Managed | Self::Registry | Self::MicrosoftStore => false,
16421647
}
16431648
}
16441649

0 commit comments

Comments
 (0)