From 35c59e69dd9bf44429280b2427428535fbf6ca24 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Wed, 7 May 2025 13:09:38 +1000 Subject: [PATCH] Don't report conda mgrs for conda installed in env --- crates/pet-conda/src/environments.rs | 16 +++++---- crates/pet-conda/src/manager.rs | 50 ++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/crates/pet-conda/src/environments.rs b/crates/pet-conda/src/environments.rs index 9a9fe4de..399bac68 100644 --- a/crates/pet-conda/src/environments.rs +++ b/crates/pet-conda/src/environments.rs @@ -84,7 +84,7 @@ pub fn get_conda_environment_info( if let Some(conda_dir) = &conda_install_folder { if conda_dir.exists() { trace!( - "Conda install folder {}, found, & will not be used for the Conda Env: {}", + "Conda install folder {}, found, & will be used for the Conda Env: {}", env_path.display(), conda_dir.display() ); @@ -140,11 +140,6 @@ pub fn get_conda_environment_info( * This function returns the path to the conda installation that was used to create the environment. */ pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Option { - // Possible the env_path is the root conda install folder. - if is_conda_install(env_path) { - return Some(env_path.to_path_buf()); - } - // If this environment is in a folder named `envs`, then the parent directory of `envs` is the root conda install folder. if let Some(parent) = env_path.ancestors().nth(2) { if is_conda_install(parent) { @@ -152,6 +147,8 @@ pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Optio } } + // First look for the conda-meta/history file in the environment folder. + // This could be a conda envirment (not root) but has `conda` installed in it. let conda_meta_history = env_path.join("conda-meta").join("history"); if let Ok(reader) = std::fs::read_to_string(conda_meta_history.clone()) { if let Some(line) = reader.lines().map(|l| l.trim()).find(|l| { @@ -169,7 +166,12 @@ pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Optio } } - None + // Possible the env_path is the root conda install folder. + if is_conda_install(env_path) { + Some(env_path.to_path_buf()) + } else { + None + } } fn get_conda_dir_from_cmd(cmd_line: String) -> Option { diff --git a/crates/pet-conda/src/manager.rs b/crates/pet-conda/src/manager.rs index 2bcc5ab3..61b7ed75 100644 --- a/crates/pet-conda/src/manager.rs +++ b/crates/pet-conda/src/manager.rs @@ -2,10 +2,13 @@ // Licensed under the MIT License. use crate::{ - conda_info::CondaInfo, env_variables::EnvVariables, - environments::get_conda_installation_used_to_create_conda_env, package::CondaPackageInfo, - utils::is_conda_env, + conda_info::CondaInfo, + env_variables::EnvVariables, + environments::get_conda_installation_used_to_create_conda_env, + package::CondaPackageInfo, + utils::{is_conda_env, is_conda_install}, }; +use log::trace; use pet_core::{manager::EnvManager, manager::EnvManagerType}; use std::{ env, @@ -78,20 +81,41 @@ impl CondaManager { if !is_conda_env(path) { return None; } - if let Some(manager) = get_conda_manager(path) { - Some(manager) + + // If this environment is in a folder named `envs`, then the parent directory of `envs` is the root conda install folder. + if let Some(parent) = path.ancestors().nth(2) { + if is_conda_install(parent) { + if let Some(manager) = get_conda_manager(parent) { + return Some(manager); + } + } + } + + // Possible this is a conda environment in some other location + // Such as global env folders location configured via condarc file + // Or a conda env created using `-p` flag. + // Get the conda install folder from the history file. + // Or its in a location such as `~/.conda/envs` or `~/miniconda3/envs` where the conda install folder is not a parent of this path. + if let Some(conda_install_folder) = get_conda_installation_used_to_create_conda_env(path) { + get_conda_manager(&conda_install_folder) } else { - // Possible this is a conda environment in the `envs` folder - let path = path.parent()?.parent()?; + // If this is a conda env and the parent is `.conda/envs`, then this is definitely NOT a root conda install folder. + // Hence never use conda installs from these env paths. + if let Some(parent) = path.parent() { + if parent.ends_with(".conda/envs") || parent.ends_with(".conda\\envs") { + trace!( + "Parent path ends with .conda/envs, not a root conda install folder: {:?}", + parent + ); + return None; + } + } + if let Some(manager) = get_conda_manager(path) { Some(manager) } else { - // Possible this is a conda environment in some other location - // Such as global env folders location configured via condarc file - // Or a conda env created using `-p` flag. - // Get the conda install folder from the history file. - let conda_install_folder = get_conda_installation_used_to_create_conda_env(path)?; - get_conda_manager(&conda_install_folder) + trace!("No conda manager found for path: {:?}", path); + None } } }