python: Prefer conda environments that match CONDA_PREFIX (#20702)
Release Notes: - Improved Python toolchain selection for conda environments
This commit is contained in:
parent
c03f5b351b
commit
f619a872b5
1 changed files with 22 additions and 0 deletions
|
@ -19,6 +19,7 @@ use pet_core::Configuration;
|
||||||
use project::lsp_store::language_server_settings;
|
use project::lsp_store::language_server_settings;
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use smol::{lock::OnceCell, process::Command};
|
use smol::{lock::OnceCell, process::Command};
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -425,11 +426,32 @@ impl ToolchainLister for PythonToolchainProvider {
|
||||||
.lock()
|
.lock()
|
||||||
.ok()
|
.ok()
|
||||||
.map_or(Vec::new(), |mut guard| std::mem::take(&mut guard));
|
.map_or(Vec::new(), |mut guard| std::mem::take(&mut guard));
|
||||||
|
|
||||||
toolchains.sort_by(|lhs, rhs| {
|
toolchains.sort_by(|lhs, rhs| {
|
||||||
env_priority(lhs.kind)
|
env_priority(lhs.kind)
|
||||||
.cmp(&env_priority(rhs.kind))
|
.cmp(&env_priority(rhs.kind))
|
||||||
|
.then_with(|| {
|
||||||
|
if lhs.kind == Some(PythonEnvironmentKind::Conda) {
|
||||||
|
environment
|
||||||
|
.get_env_var("CONDA_PREFIX".to_string())
|
||||||
|
.map(|conda_prefix| {
|
||||||
|
let is_match = |exe: &Option<PathBuf>| {
|
||||||
|
exe.as_ref().map_or(false, |e| e.starts_with(&conda_prefix))
|
||||||
|
};
|
||||||
|
match (is_match(&lhs.executable), is_match(&rhs.executable)) {
|
||||||
|
(true, false) => Ordering::Less,
|
||||||
|
(false, true) => Ordering::Greater,
|
||||||
|
_ => Ordering::Equal,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or(Ordering::Equal)
|
||||||
|
} else {
|
||||||
|
Ordering::Equal
|
||||||
|
}
|
||||||
|
})
|
||||||
.then_with(|| lhs.executable.cmp(&rhs.executable))
|
.then_with(|| lhs.executable.cmp(&rhs.executable))
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut toolchains: Vec<_> = toolchains
|
let mut toolchains: Vec<_> = toolchains
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|toolchain| {
|
.filter_map(|toolchain| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue