Skip to content

Commit 9636b82

Browse files
authored
Always determine conda manager for a given env (#216)
1 parent ac86ec2 commit 9636b82

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

crates/pet-conda/src/environments.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ impl CondaEnvironment {
3030
get_conda_environment_info(path, manager)
3131
}
3232

33-
pub fn to_python_environment(
34-
&self,
35-
conda_dir: Option<PathBuf>,
36-
conda_manager: Option<EnvManager>,
37-
) -> PythonEnvironment {
33+
pub fn to_python_environment(&self, conda_manager: Option<EnvManager>) -> PythonEnvironment {
3834
#[allow(unused_assignments)]
3935
let mut name: Option<String> = None;
4036
if is_conda_install(&self.prefix) {
@@ -48,7 +44,7 @@ impl CondaEnvironment {
4844
// if the conda install folder is parent of the env folder, then we can use named activation.
4945
// E.g. conda env is = <conda install>/envs/<env name>
5046
// Then we can use `<conda install>/bin/conda activate -n <env name>`
51-
if let Some(conda_dir) = conda_dir {
47+
if let Some(conda_dir) = &self.conda_dir {
5248
if !self.prefix.starts_with(conda_dir) {
5349
name = None;
5450
}
@@ -76,10 +72,8 @@ pub fn get_conda_environment_info(
7672
return None;
7773
}
7874
// If we know the conda install folder, then we can use it.
79-
let mut conda_install_folder = manager
80-
.clone()
81-
.and_then(|m| m.conda_dir)
82-
.or_else(|| get_conda_installation_used_to_create_conda_env(env_path));
75+
let mut conda_install_folder = get_conda_installation_used_to_create_conda_env(env_path)
76+
.or_else(|| manager.clone().and_then(|m| m.conda_dir));
8377

8478
if let Some(conda_dir) = &conda_install_folder {
8579
if conda_dir.exists() {

crates/pet-conda/src/lib.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,15 @@ impl CondaLocator for Conda {
174174
if environments.contains_key(&conda_env.prefix) {
175175
continue;
176176
}
177-
let env = conda_env
178-
.to_python_environment(Some(conda_dir.clone()), Some(manager.to_manager()));
177+
178+
// Get the right manager for this conda env.
179+
// Possible the manager is different from the one we got from the conda_dir.
180+
let manager = conda_env
181+
.clone()
182+
.conda_dir
183+
.and_then(|p| CondaManager::from(&p))
184+
.unwrap_or(manager.clone());
185+
let env = conda_env.to_python_environment(Some(manager.to_manager()));
179186
environments.insert(conda_env.prefix.clone(), env.clone());
180187
reporter.report_manager(&manager.to_manager());
181188
reporter.report_environment(&env);
@@ -248,18 +255,15 @@ impl Locator for Conda {
248255
if let Some(env) = get_conda_environment_info(path, &None) {
249256
if let Some(conda_dir) = &env.conda_dir {
250257
if let Some(manager) = self.get_manager(conda_dir) {
251-
let env = env.to_python_environment(
252-
Some(conda_dir.clone()),
253-
Some(manager.to_manager()),
254-
);
258+
let env = env.to_python_environment(Some(manager.to_manager()));
255259
environments.insert(path.clone(), env.clone());
256260
return Some(env);
257261
} else {
258262
// We will still return the conda env even though we do not have the manager.
259263
// This might seem incorrect, however the tool is about discovering environments.
260264
// The client can activate this env either using another conda manager or using the activation scripts
261265
error!("Unable to find Conda Manager for env (even though we have a conda_dir): {:?}", env);
262-
let env = env.to_python_environment(Some(conda_dir.clone()), None);
266+
let env = env.to_python_environment(None);
263267
environments.insert(path.clone(), env.clone());
264268
return Some(env);
265269
}
@@ -268,7 +272,7 @@ impl Locator for Conda {
268272
// This might seem incorrect, however the tool is about discovering environments.
269273
// The client can activate this env either using another conda manager or using the activation scripts
270274
error!("Unable to find Conda Manager for env: {:?}", env);
271-
let env = env.to_python_environment(None, None);
275+
let env = env.to_python_environment(None);
272276
environments.insert(path.clone(), env.clone());
273277
return Some(env);
274278
}
@@ -301,7 +305,7 @@ impl Locator for Conda {
301305
// The client can activate this env either using another conda manager or using the activation scripts
302306
error!("Unable to find Conda Manager for the Conda env: {:?}", env);
303307
let prefix = env.prefix.clone();
304-
let env = env.to_python_environment(None, None);
308+
let env = env.to_python_environment(None);
305309
let mut environments = self.environments.lock().unwrap();
306310
environments.insert(prefix, env.clone());
307311
reporter.report_environment(&env);
@@ -340,7 +344,6 @@ impl Locator for Conda {
340344
// 5. Report this env.
341345
if let Some(manager) = manager {
342346
let env = env.to_python_environment(
343-
manager.conda_dir.clone(),
344347
Some(manager.to_manager()),
345348
);
346349
let mut environments = self.environments.lock().unwrap();
@@ -352,7 +355,7 @@ impl Locator for Conda {
352355
// This might seem incorrect, however the tool is about discovering environments.
353356
// The client can activate this env either using another conda manager or using the activation scripts
354357
error!("Unable to find Conda Manager for Conda env (even though we have a conda_dir {:?}): Env Details = {:?}", conda_dir, env);
355-
let env = env.to_python_environment(Some(conda_dir.clone()), None);
358+
let env = env.to_python_environment(None);
356359
let mut environments = self.environments.lock().unwrap();
357360
environments.insert(prefix.clone(), env.clone());
358361
reporter.report_environment(&env);

0 commit comments

Comments
 (0)