use std::path::{Path, PathBuf};
use std::{env, io};
use fs_err as fs;
use uv_fs::read_venv_redirect;
use uv_preview::PreviewFeature;
use uv_python_interpreter::VirtualEnvError as Error;
use uv_static::EnvVars;
use uv_warnings::warn_user_once;
pub(crate) fn virtualenv_from_env() -> Option<PathBuf> {
if let Some(dir) = env::var_os(EnvVars::VIRTUAL_ENV).filter(|value| !value.is_empty()) {
return Some(PathBuf::from(dir));
}
None
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub(crate) enum CondaEnvironmentKind {
Base,
Child,
}
impl CondaEnvironmentKind {
fn from_prefix_path(path: &Path) -> Self {
if is_pixi_environment(path) {
return Self::Child;
}
if let Ok(conda_root) = env::var(EnvVars::CONDA_ROOT) {
if path == Path::new(&conda_root) {
return Self::Base;
}
}
let Ok(current_env) = env::var(EnvVars::CONDA_DEFAULT_ENV) else {
return Self::Child;
};
if path == Path::new(¤t_env) {
return Self::Child;
}
let Some(name) = path.file_name() else {
return Self::Child;
};
if name.to_str().is_some_and(|name| name == current_env) {
Self::Child
} else {
Self::Base
}
}
}
fn is_pixi_environment(path: &Path) -> bool {
path.join("conda-meta").join("pixi").is_file()
}
pub(crate) fn conda_environment_from_env(kind: CondaEnvironmentKind) -> Option<PathBuf> {
let dir = env::var_os(EnvVars::CONDA_PREFIX).filter(|value| !value.is_empty())?;
let path = PathBuf::from(dir);
if kind != CondaEnvironmentKind::from_prefix_path(&path) {
return None;
}
Some(path)
}
pub(crate) fn virtualenv_from_working_dir() -> Result<Option<PathBuf>, Error> {
let current_dir = crate::current_dir()?;
for dir in current_dir.ancestors() {
if uv_fs::is_virtualenv_base(dir) {
return Ok(Some(dir.to_path_buf()));
}
let dot_venv = dir.join(".venv");
let metadata = match fs::symlink_metadata(&dot_venv) {
Ok(metadata) => metadata,
Err(err) if err.kind() == io::ErrorKind::NotFound => continue,
Err(err) => return Err(err.into()),
};
let redirect = dot_venv.is_file();
let root = if redirect {
read_venv_redirect(&dot_venv)?
} else if metadata.is_dir() || metadata.is_symlink() {
dot_venv
} else {
continue;
};
if !uv_fs::is_virtualenv_base(&root) {
return Err(Error::MissingPyVenvCfg(root));
}
if redirect && !uv_preview::is_enabled(PreviewFeature::VenvRedirectFiles) {
warn_user_once!(
"Using `.venv` redirect files is experimental and may change without warning. Pass `--preview-features venv-redirect-files` to disable this warning."
);
}
return Ok(Some(root));
}
Ok(None)
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::OsStr;
use temp_env::with_vars;
use tempfile::tempdir;
#[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
);
});
}
}