From 1a32fd44aa21e6bb76fb12cd282a4b24adc29e76 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Thu, 14 Aug 2025 11:05:19 -0500 Subject: [PATCH] Set `VIRTUAL_ENV` in `uv tool run` --- crates/uv/src/commands/tool/run.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/uv/src/commands/tool/run.rs b/crates/uv/src/commands/tool/run.rs index 64628e56a..248ac6af3 100644 --- a/crates/uv/src/commands/tool/run.rs +++ b/crates/uv/src/commands/tool/run.rs @@ -369,6 +369,13 @@ pub(crate) async fn run( .context("Failed to build new PATH variable")?; process.env(EnvVars::PATH, new_path); + // If we're not in a virtual environment already, set `VIRTUAL_ENV`. + // This is a little defense, e.g., we could always set `VIRTUAL_ENV` but it could break a tool + // that you're using to target your current virtual environment. + if std::env::var_os(EnvVars::VIRTUAL_ENV).is_none() { + process.env(EnvVars::VIRTUAL_ENV, environment.root()); + } + // Spawn and wait for completion // Standard input, output, and error streams are all inherited let space = if args.is_empty() { "" } else { " " };