Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/toolset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl Toolset {
env.insert(PATH_KEY.to_string(), add_paths);
}
env.extend(config.env()?.clone());
if let Some(venv) = &*UV_VENV {
if let Ok(Some(venv)) = UV_VENV.try_lock().as_deref() {
for (k, v) in &venv.env {
env.insert(k.clone(), v.clone());
}
Expand Down Expand Up @@ -573,7 +573,7 @@ impl Toolset {
for p in config.path_dirs()?.clone() {
paths.insert(p);
}
if let Some(venv) = &*UV_VENV {
if let Ok(Some(venv)) = UV_VENV.try_lock().as_deref() {
paths.insert(venv.venv_path.clone());
}
if let Some(path) = self.env(config)?.get(&*PATH_KEY) {
Expand Down
12 changes: 7 additions & 5 deletions src/uv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ use crate::{dirs, file};
use eyre::Result;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::LazyLock as Lazy;
use std::sync::{LazyLock as Lazy, Mutex};

#[derive(Debug)]
pub struct Venv {
pub venv_path: PathBuf,
pub env: HashMap<String, String>,
}

pub static UV_VENV: Lazy<Option<Venv>> = Lazy::new(|| {
// use a mutex to prevent deadlocks that may occur due to recursive initialization
// when resolving the venv path or env vars
pub static UV_VENV: Lazy<Mutex<Option<Venv>>> = Lazy::new(|| {
Comment thread
risu729 marked this conversation as resolved.
if !SETTINGS.python.uv_venv_auto {
return None;
return Mutex::new(None);
}
if let (Some(venv_path), Some(uv_path)) = (venv_path(), uv_path()) {
match get_or_create_venv(venv_path, uv_path) {
Ok(venv) => return Some(venv),
Ok(venv) => return Mutex::new(Some(venv)),
Err(e) => {
warn!("uv venv failed: {e}");
}
}
}
None
Mutex::new(None)
});

fn get_or_create_venv(venv_path: PathBuf, uv_path: PathBuf) -> Result<Venv> {
Expand Down