Skip to content

Commit 3c1abc0

Browse files
authored
Give preference to conda in root over conda in env (#210)
Fixes #184
1 parent f3e47eb commit 3c1abc0

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

crates/pet-conda/src/environments.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn get_conda_environment_info(
8484
if let Some(conda_dir) = &conda_install_folder {
8585
if conda_dir.exists() {
8686
trace!(
87-
"Conda install folder {}, found, & will not be used for the Conda Env: {}",
87+
"Conda install folder {}, found, & will be used for the Conda Env: {}",
8888
env_path.display(),
8989
conda_dir.display()
9090
);
@@ -140,18 +140,15 @@ pub fn get_conda_environment_info(
140140
* This function returns the path to the conda installation that was used to create the environment.
141141
*/
142142
pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Option<PathBuf> {
143-
// Possible the env_path is the root conda install folder.
144-
if is_conda_install(env_path) {
145-
return Some(env_path.to_path_buf());
146-
}
147-
148143
// If this environment is in a folder named `envs`, then the parent directory of `envs` is the root conda install folder.
149144
if let Some(parent) = env_path.ancestors().nth(2) {
150145
if is_conda_install(parent) {
151146
return Some(parent.to_path_buf());
152147
}
153148
}
154149

150+
// First look for the conda-meta/history file in the environment folder.
151+
// This could be a conda envirment (not root) but has `conda` installed in it.
155152
let conda_meta_history = env_path.join("conda-meta").join("history");
156153
if let Ok(reader) = std::fs::read_to_string(conda_meta_history.clone()) {
157154
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
169166
}
170167
}
171168

172-
None
169+
// Possible the env_path is the root conda install folder.
170+
if is_conda_install(env_path) {
171+
Some(env_path.to_path_buf())
172+
} else {
173+
None
174+
}
173175
}
174176

175177
fn get_conda_dir_from_cmd(cmd_line: String) -> Option<PathBuf> {

crates/pet-conda/src/manager.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// Licensed under the MIT License.
33

44
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},
810
};
11+
use log::trace;
912
use pet_core::{manager::EnvManager, manager::EnvManagerType};
1013
use std::{
1114
env,
@@ -78,20 +81,41 @@ impl CondaManager {
7881
if !is_conda_env(path) {
7982
return None;
8083
}
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)
83101
} 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+
86114
if let Some(manager) = get_conda_manager(path) {
87115
Some(manager)
88116
} 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
95119
}
96120
}
97121
}

0 commit comments

Comments
 (0)