Skip to content

Commit 56b2453

Browse files
committed
Invert the logic for determining if a path is a base conda environment
1 parent 4a1813f commit 56b2453

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

crates/uv-python/src/lib.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ mod tests {
11981198
"We should allow the active conda python"
11991199
);
12001200

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

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

12261226
// Unless, system interpreters are included...
1227-
let python = context.run_with_vars(
1228-
&[
1229-
("CONDA_PREFIX", Some(baseenv.as_os_str())),
1230-
("CONDA_DEFAULT_ENV", Some(&OsString::from("base"))),
1231-
],
1232-
|| {
1233-
find_python_installation(
1234-
&PythonRequest::Default,
1235-
EnvironmentPreference::OnlySystem,
1236-
PythonPreference::OnlySystem,
1237-
&context.cache,
1238-
Preview::default(),
1239-
)
1240-
},
1241-
)??;
1227+
let python = context
1228+
.run_with_vars(
1229+
&[
1230+
("CONDA_PREFIX", Some(baseenv.as_os_str())),
1231+
("CONDA_DEFAULT_ENV", Some(&OsString::from("base"))),
1232+
],
1233+
|| {
1234+
find_python_installation(
1235+
&PythonRequest::Default,
1236+
EnvironmentPreference::OnlySystem,
1237+
PythonPreference::OnlySystem,
1238+
&context.cache,
1239+
Preview::default(),
1240+
)
1241+
},
1242+
)?
1243+
.unwrap();
12421244

12431245
assert_eq!(
12441246
python.interpreter().python_full_version().to_string(),
@@ -1247,21 +1249,23 @@ mod tests {
12471249
);
12481250

12491251
// If the environment name doesn't match the default, we should not treat it as system
1250-
let python = context.run_with_vars(
1251-
&[
1252-
("CONDA_PREFIX", Some(condaenv.as_os_str())),
1253-
("CONDA_DEFAULT_ENV", Some(&OsString::from("base"))),
1254-
],
1255-
|| {
1256-
find_python_installation(
1257-
&PythonRequest::Default,
1258-
EnvironmentPreference::OnlyVirtual,
1259-
PythonPreference::OnlySystem,
1260-
&context.cache,
1261-
Preview::default(),
1262-
)
1263-
},
1264-
)??;
1252+
let python = context
1253+
.run_with_vars(
1254+
&[
1255+
("CONDA_PREFIX", Some(condaenv.as_os_str())),
1256+
("CONDA_DEFAULT_ENV", Some(&OsString::from("condaenv"))),
1257+
],
1258+
|| {
1259+
find_python_installation(
1260+
&PythonRequest::Default,
1261+
EnvironmentPreference::OnlyVirtual,
1262+
PythonPreference::OnlySystem,
1263+
&context.cache,
1264+
Preview::default(),
1265+
)
1266+
},
1267+
)?
1268+
.unwrap();
12651269

12661270
assert_eq!(
12671271
python.interpreter().python_full_version().to_string(),

crates/uv-python/src/virtualenv.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,30 @@ impl CondaEnvironmentKind {
8282
/// When the base environment is used, `CONDA_DEFAULT_ENV` will be set to a name, i.e., `base` or
8383
/// `root` which does not match the prefix, e.g. `/usr/local` instead of
8484
/// `/usr/local/conda/envs/<name>`.
85+
///
86+
/// Note the name `CONDA_DEFAULT_ENV` is misleading, it's the current environment, not the base
87+
/// environment name.
8588
fn from_prefix_path(path: &Path) -> Self {
8689
// If we cannot read `CONDA_DEFAULT_ENV`, there's no way to know if the base environment
87-
let Ok(default_env) = env::var(EnvVars::CONDA_DEFAULT_ENV) else {
90+
let Ok(current_env) = env::var(EnvVars::CONDA_DEFAULT_ENV) else {
8891
return Self::Child;
8992
};
9093

9194
// These are the expected names for the base environment
92-
if default_env != "base" && default_env != "root" {
95+
if current_env != "base" && current_env != "root" {
9396
return Self::Child;
9497
}
9598

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

100-
if name.to_str().is_some_and(|name| name == default_env) {
101-
Self::Base
102-
} else {
103+
// If the environment is in a directory matching the name of the environment, it's not
104+
// usually a base environment.
105+
if name.to_str().is_some_and(|name| name == current_env) {
103106
Self::Child
107+
} else {
108+
Self::Base
104109
}
105110
}
106111
}

0 commit comments

Comments
 (0)