From 7aa4ae18c2b6268dedbee72073bf859b9d781337 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Wed, 7 May 2025 20:06:45 +1000 Subject: [PATCH] Always determine conda manager for a given env --- crates/pet-conda/src/environments.rs | 14 ++++---------- crates/pet-conda/src/lib.rs | 25 ++++++++++++++----------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/crates/pet-conda/src/environments.rs b/crates/pet-conda/src/environments.rs index 399bac68..c4ce1d9a 100644 --- a/crates/pet-conda/src/environments.rs +++ b/crates/pet-conda/src/environments.rs @@ -30,11 +30,7 @@ impl CondaEnvironment { get_conda_environment_info(path, manager) } - pub fn to_python_environment( - &self, - conda_dir: Option, - conda_manager: Option, - ) -> PythonEnvironment { + pub fn to_python_environment(&self, conda_manager: Option) -> PythonEnvironment { #[allow(unused_assignments)] let mut name: Option = None; if is_conda_install(&self.prefix) { @@ -48,7 +44,7 @@ impl CondaEnvironment { // if the conda install folder is parent of the env folder, then we can use named activation. // E.g. conda env is = /envs/ // Then we can use `/bin/conda activate -n ` - if let Some(conda_dir) = conda_dir { + if let Some(conda_dir) = &self.conda_dir { if !self.prefix.starts_with(conda_dir) { name = None; } @@ -76,10 +72,8 @@ pub fn get_conda_environment_info( return None; } // If we know the conda install folder, then we can use it. - let mut conda_install_folder = manager - .clone() - .and_then(|m| m.conda_dir) - .or_else(|| get_conda_installation_used_to_create_conda_env(env_path)); + let mut conda_install_folder = get_conda_installation_used_to_create_conda_env(env_path) + .or_else(|| manager.clone().and_then(|m| m.conda_dir)); if let Some(conda_dir) = &conda_install_folder { if conda_dir.exists() { diff --git a/crates/pet-conda/src/lib.rs b/crates/pet-conda/src/lib.rs index 6e035a1a..59fe084e 100644 --- a/crates/pet-conda/src/lib.rs +++ b/crates/pet-conda/src/lib.rs @@ -174,8 +174,15 @@ impl CondaLocator for Conda { if environments.contains_key(&conda_env.prefix) { continue; } - let env = conda_env - .to_python_environment(Some(conda_dir.clone()), Some(manager.to_manager())); + + // Get the right manager for this conda env. + // Possible the manager is different from the one we got from the conda_dir. + let manager = conda_env + .clone() + .conda_dir + .and_then(|p| CondaManager::from(&p)) + .unwrap_or(manager.clone()); + let env = conda_env.to_python_environment(Some(manager.to_manager())); environments.insert(conda_env.prefix.clone(), env.clone()); reporter.report_manager(&manager.to_manager()); reporter.report_environment(&env); @@ -248,10 +255,7 @@ impl Locator for Conda { if let Some(env) = get_conda_environment_info(path, &None) { if let Some(conda_dir) = &env.conda_dir { if let Some(manager) = self.get_manager(conda_dir) { - let env = env.to_python_environment( - Some(conda_dir.clone()), - Some(manager.to_manager()), - ); + let env = env.to_python_environment(Some(manager.to_manager())); environments.insert(path.clone(), env.clone()); return Some(env); } else { @@ -259,7 +263,7 @@ impl Locator for Conda { // This might seem incorrect, however the tool is about discovering environments. // The client can activate this env either using another conda manager or using the activation scripts error!("Unable to find Conda Manager for env (even though we have a conda_dir): {:?}", env); - let env = env.to_python_environment(Some(conda_dir.clone()), None); + let env = env.to_python_environment(None); environments.insert(path.clone(), env.clone()); return Some(env); } @@ -268,7 +272,7 @@ impl Locator for Conda { // This might seem incorrect, however the tool is about discovering environments. // The client can activate this env either using another conda manager or using the activation scripts error!("Unable to find Conda Manager for env: {:?}", env); - let env = env.to_python_environment(None, None); + let env = env.to_python_environment(None); environments.insert(path.clone(), env.clone()); return Some(env); } @@ -301,7 +305,7 @@ impl Locator for Conda { // The client can activate this env either using another conda manager or using the activation scripts error!("Unable to find Conda Manager for the Conda env: {:?}", env); let prefix = env.prefix.clone(); - let env = env.to_python_environment(None, None); + let env = env.to_python_environment(None); let mut environments = self.environments.lock().unwrap(); environments.insert(prefix, env.clone()); reporter.report_environment(&env); @@ -340,7 +344,6 @@ impl Locator for Conda { // 5. Report this env. if let Some(manager) = manager { let env = env.to_python_environment( - manager.conda_dir.clone(), Some(manager.to_manager()), ); let mut environments = self.environments.lock().unwrap(); @@ -352,7 +355,7 @@ impl Locator for Conda { // This might seem incorrect, however the tool is about discovering environments. // The client can activate this env either using another conda manager or using the activation scripts error!("Unable to find Conda Manager for Conda env (even though we have a conda_dir {:?}): Env Details = {:?}", conda_dir, env); - let env = env.to_python_environment(Some(conda_dir.clone()), None); + let env = env.to_python_environment(None); let mut environments = self.environments.lock().unwrap(); environments.insert(prefix.clone(), env.clone()); reporter.report_environment(&env);