|
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | use crate::{ |
5 | | - conda_info::CondaInfo, env_variables::EnvVariables, |
6 | | - environments::get_conda_installation_used_to_create_conda_env, package::CondaPackageInfo, |
7 | | - utils::is_conda_env, |
| 5 | + conda_info::CondaInfo, |
| 6 | + env_variables::EnvVariables, |
| 7 | + environments::get_conda_installation_used_to_create_conda_env, |
| 8 | + package::CondaPackageInfo, |
| 9 | + utils::{is_conda_env, is_conda_install}, |
8 | 10 | }; |
| 11 | +use log::trace; |
9 | 12 | use pet_core::{manager::EnvManager, manager::EnvManagerType}; |
10 | 13 | use std::{ |
11 | 14 | env, |
@@ -78,20 +81,41 @@ impl CondaManager { |
78 | 81 | if !is_conda_env(path) { |
79 | 82 | return None; |
80 | 83 | } |
81 | | - if let Some(manager) = get_conda_manager(path) { |
82 | | - Some(manager) |
| 84 | + |
| 85 | + // If this environment is in a folder named `envs`, then the parent directory of `envs` is the root conda install folder. |
| 86 | + if let Some(parent) = path.ancestors().nth(2) { |
| 87 | + if is_conda_install(parent) { |
| 88 | + if let Some(manager) = get_conda_manager(parent) { |
| 89 | + return Some(manager); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Possible this is a conda environment in some other location |
| 95 | + // Such as global env folders location configured via condarc file |
| 96 | + // Or a conda env created using `-p` flag. |
| 97 | + // Get the conda install folder from the history file. |
| 98 | + // Or its in a location such as `~/.conda/envs` or `~/miniconda3/envs` where the conda install folder is not a parent of this path. |
| 99 | + if let Some(conda_install_folder) = get_conda_installation_used_to_create_conda_env(path) { |
| 100 | + get_conda_manager(&conda_install_folder) |
83 | 101 | } else { |
84 | | - // Possible this is a conda environment in the `envs` folder |
85 | | - let path = path.parent()?.parent()?; |
| 102 | + // If this is a conda env and the parent is `.conda/envs`, then this is definitely NOT a root conda install folder. |
| 103 | + // Hence never use conda installs from these env paths. |
| 104 | + if let Some(parent) = path.parent() { |
| 105 | + if parent.ends_with(".conda/envs") || parent.ends_with(".conda\\envs") { |
| 106 | + trace!( |
| 107 | + "Parent path ends with .conda/envs, not a root conda install folder: {:?}", |
| 108 | + parent |
| 109 | + ); |
| 110 | + return None; |
| 111 | + } |
| 112 | + } |
| 113 | + |
86 | 114 | if let Some(manager) = get_conda_manager(path) { |
87 | 115 | Some(manager) |
88 | 116 | } else { |
89 | | - // Possible this is a conda environment in some other location |
90 | | - // Such as global env folders location configured via condarc file |
91 | | - // Or a conda env created using `-p` flag. |
92 | | - // Get the conda install folder from the history file. |
93 | | - let conda_install_folder = get_conda_installation_used_to_create_conda_env(path)?; |
94 | | - get_conda_manager(&conda_install_folder) |
| 117 | + trace!("No conda manager found for path: {:?}", path); |
| 118 | + None |
95 | 119 | } |
96 | 120 | } |
97 | 121 | } |
|
0 commit comments