Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions crates/uv-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ mod tests {
"We should allow the active conda python"
);

let baseenv = context.tempdir.child("base");
let baseenv = context.tempdir.child("conda");
TestContext::mock_conda_prefix(&baseenv, "3.12.1")?;

// But not if it's a base environment
Expand All @@ -1224,21 +1224,23 @@ mod tests {
);

// Unless, system interpreters are included...
let python = context.run_with_vars(
&[
("CONDA_PREFIX", Some(baseenv.as_os_str())),
("CONDA_DEFAULT_ENV", Some(&OsString::from("base"))),
],
|| {
find_python_installation(
&PythonRequest::Default,
EnvironmentPreference::OnlySystem,
PythonPreference::OnlySystem,
&context.cache,
Preview::default(),
)
},
)??;
let python = context
.run_with_vars(
&[
("CONDA_PREFIX", Some(baseenv.as_os_str())),
("CONDA_DEFAULT_ENV", Some(&OsString::from("base"))),
],
|| {
find_python_installation(
&PythonRequest::Default,
EnvironmentPreference::OnlySystem,
PythonPreference::OnlySystem,
&context.cache,
Preview::default(),
)
},
)?
.unwrap();

assert_eq!(
python.interpreter().python_full_version().to_string(),
Expand All @@ -1247,21 +1249,23 @@ mod tests {
);

// If the environment name doesn't match the default, we should not treat it as system
let python = context.run_with_vars(
&[
("CONDA_PREFIX", Some(condaenv.as_os_str())),
("CONDA_DEFAULT_ENV", Some(&OsString::from("base"))),
],
|| {
find_python_installation(
&PythonRequest::Default,
EnvironmentPreference::OnlyVirtual,
PythonPreference::OnlySystem,
&context.cache,
Preview::default(),
)
},
)??;
let python = context
.run_with_vars(
&[
("CONDA_PREFIX", Some(condaenv.as_os_str())),
("CONDA_DEFAULT_ENV", Some(&OsString::from("condaenv"))),
],
|| {
find_python_installation(
&PythonRequest::Default,
EnvironmentPreference::OnlyVirtual,
PythonPreference::OnlySystem,
&context.cache,
Preview::default(),
)
},
)?
.unwrap();

assert_eq!(
python.interpreter().python_full_version().to_string(),
Expand Down
15 changes: 10 additions & 5 deletions crates/uv-python/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,30 @@ impl CondaEnvironmentKind {
/// When the base environment is used, `CONDA_DEFAULT_ENV` will be set to a name, i.e., `base` or
/// `root` which does not match the prefix, e.g. `/usr/local` instead of
/// `/usr/local/conda/envs/<name>`.
///
/// Note the name `CONDA_DEFAULT_ENV` is misleading, it's the current environment, not the base
/// environment name.
fn from_prefix_path(path: &Path) -> Self {
// If we cannot read `CONDA_DEFAULT_ENV`, there's no way to know if the base environment
let Ok(default_env) = env::var(EnvVars::CONDA_DEFAULT_ENV) else {
let Ok(current_env) = env::var(EnvVars::CONDA_DEFAULT_ENV) else {
return Self::Child;
};

// These are the expected names for the base environment
if default_env != "base" && default_env != "root" {
if current_env != "base" && current_env != "root" {
return Self::Child;
}

let Some(name) = path.file_name() else {
return Self::Child;
};

if name.to_str().is_some_and(|name| name == default_env) {
Self::Base
} else {
// If the environment is in a directory matching the name of the environment, it's not
// usually a base environment.
if name.to_str().is_some_and(|name| name == current_env) {
Self::Child
} else {
Self::Base
}
}
}
Expand Down
Loading