Skip to content

Commit 3db7558

Browse files
authored
Ignore non-conda envs in Conda locator (#49)
Fixes #48
1 parent f3750eb commit 3db7558

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pet-conda/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ msvc_spectre_libs = { version = "0.1.1", features = ["error"] }
99
[dependencies]
1010
pet-fs = { path = "../pet-fs" }
1111
pet-python-utils = { path = "../pet-python-utils" }
12-
pet-virtualenv = { path = "../pet-virtualenv" }
1312
serde = { version = "1.0.152", features = ["derive"] }
1413
serde_json = "1.0.93"
1514
lazy_static = "1.4.0"

crates/pet-conda/src/lib.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ use pet_core::{
1414
Locator, LocatorResult,
1515
};
1616
use pet_python_utils::env::PythonEnv;
17-
use pet_virtualenv::is_virtualenv;
1817
use std::{
1918
collections::HashMap,
2019
path::{Path, PathBuf},
2120
sync::{Arc, Mutex},
2221
thread,
2322
};
24-
use utils::is_conda_install;
23+
use utils::{is_conda_env, is_conda_install};
2524

2625
mod conda_info;
2726
pub mod conda_rc;
@@ -151,15 +150,29 @@ impl Locator for Conda {
151150
vec![PythonEnvironmentCategory::Conda]
152151
}
153152
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
154-
// Assume we create a virtual env from a conda python install,
155-
// Doing this is a big no no, but people do this.
156-
// Then the exe in the virtual env bin will be a symlink to the homebrew python install.
157-
// Hence the first part of the condition will be true, but the second part will be false.
158-
if is_virtualenv(env) {
159-
return None;
153+
// Possible we do not have the prefix, but this exe is in the bin directory and its a conda env or root conda install.
154+
let mut prefix = env.prefix.clone();
155+
if prefix.is_none() {
156+
if let Some(parent_dir) = &env.executable.parent() {
157+
if is_conda_env(parent_dir) {
158+
// This is a conda env (most likely root conda env as the exe is in the same directory (generally on windows))
159+
prefix = Some(parent_dir.to_path_buf());
160+
} else if parent_dir.ends_with("bin") || parent_dir.ends_with("Scripts") {
161+
if let Some(parent_dir) = parent_dir.parent() {
162+
if is_conda_env(parent_dir) {
163+
// This is a conda env
164+
prefix = Some(parent_dir.to_path_buf());
165+
}
166+
}
167+
}
168+
}
160169
}
161170

162-
if let Some(ref path) = env.prefix {
171+
if let Some(ref path) = prefix {
172+
if !is_conda_env(path) {
173+
return None;
174+
}
175+
163176
let mut environments = self.environments.lock().unwrap();
164177

165178
// Do we already have an env for this.

0 commit comments

Comments
 (0)