Skip to content
Merged
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
38 changes: 38 additions & 0 deletions crates/uv-python/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ impl CondaEnvironmentKind {
/// `/usr/local/conda/envs/<name>`. Note the name `CONDA_DEFAULT_ENV` is misleading, it's the
/// active environment name, not a constant base environment name.
fn from_prefix_path(path: &Path) -> Self {
// Pixi never creates true "base" envs and names project envs "default", confusing our
// heuristics, so treat Pixi prefixes as child envs outright.
if is_pixi_environment(path) {
return Self::Child;
}

// If `_CONDA_ROOT` is set and matches `CONDA_PREFIX`, it's the base environment.
if let Ok(conda_root) = env::var(EnvVars::CONDA_ROOT) {
if path == Path::new(&conda_root) {
Expand Down Expand Up @@ -127,6 +133,11 @@ impl CondaEnvironmentKind {
}
}

/// Detect whether the current `CONDA_PREFIX` belongs to a Pixi-managed environment.
fn is_pixi_environment(path: &Path) -> bool {
path.join("conda-meta").join("pixi").is_file()
}

/// Locate an active conda environment by inspecting environment variables.
///
/// If `base` is true, the active environment must be the base environment or `None` is returned,
Expand Down Expand Up @@ -317,10 +328,37 @@ impl PyVenvConfiguration {

#[cfg(test)]
mod tests {
use std::ffi::OsStr;

use indoc::indoc;
use temp_env::with_vars;
use tempfile::tempdir;

use super::*;

#[test]
fn pixi_environment_is_treated_as_child() {
let tempdir = tempdir().unwrap();
let prefix = tempdir.path();
let conda_meta = prefix.join("conda-meta");

fs::create_dir_all(&conda_meta).unwrap();
fs::write(conda_meta.join("pixi"), []).unwrap();

let vars = [
(EnvVars::CONDA_ROOT, None),
(EnvVars::CONDA_PREFIX, Some(prefix.as_os_str())),
(EnvVars::CONDA_DEFAULT_ENV, Some(OsStr::new("example"))),
];

with_vars(vars, || {
assert_eq!(
CondaEnvironmentKind::from_prefix_path(prefix),
CondaEnvironmentKind::Child
);
});
}

#[test]
fn test_set_existing_key() {
let content = indoc! {"
Expand Down
Loading