Add support for respecting VIRTUAL_ENV in project commands via --active (#11189)

I think `UV_PROJECT_ENVIRONMENT` is too complicated for use-cases where
the user wants to sync to the active environment. I don't see a
compelling reason not to make opt-in easier. I see a lot of questions
about how to deal with this warning in the issue tracker, but it seems
painful to collect them here for posterity.

A notable behavior here — we'll treat this as equivalent to
`UV_PROJECT_ENVIRONMENT` so... if you point us to a valid virtual
environment that needs to be recreated for some reason (e.g., new Python
version request), we'll happily delete it and start over.
This commit is contained in:
Zanie Blue 2025-02-05 10:12:19 -06:00 committed by GitHub
parent 6f8d9b85d8
commit 989b103171
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 368 additions and 33 deletions

View file

@ -2805,6 +2805,19 @@ pub struct RunArgs {
#[arg(long)]
pub isolated: bool,
/// Prefer the active virtual environment over the project's virtual environment.
///
/// If the project virtual environment is active or no virtual environment is active, this has
/// no effect.
#[arg(long, overrides_with = "no_active")]
pub active: bool,
/// Prefer project's virtual environment over an active environment.
///
/// This is the default behavior.
#[arg(long, overrides_with = "active", hide = true)]
pub no_active: bool,
/// Avoid syncing the virtual environment.
///
/// Implies `--frozen`, as the project dependencies will be ignored (i.e., the lockfile will not
@ -3004,6 +3017,19 @@ pub struct SyncArgs {
#[arg(long, overrides_with("inexact"), hide = true)]
pub exact: bool,
/// Prefer the active virtual environment over the project's virtual environment.
///
/// If the project virtual environment is active or no virtual environment is active, this has
/// no effect.
#[arg(long, overrides_with = "no_active")]
pub active: bool,
/// Prefer project's virtual environment over an active environment.
///
/// This is the default behavior.
#[arg(long, overrides_with = "active", hide = true)]
pub no_active: bool,
/// Do not install the current project.
///
/// By default, the current project is installed into the environment with all of its
@ -3247,6 +3273,19 @@ pub struct AddArgs {
#[arg(long, env = EnvVars::UV_FROZEN, value_parser = clap::builder::BoolishValueParser::new(), conflicts_with = "locked")]
pub frozen: bool,
/// Prefer the active virtual environment over the project's virtual environment.
///
/// If the project virtual environment is active or no virtual environment is active, this has
/// no effect.
#[arg(long, overrides_with = "no_active")]
pub active: bool,
/// Prefer project's virtual environment over an active environment.
///
/// This is the default behavior.
#[arg(long, overrides_with = "active", hide = true)]
pub no_active: bool,
#[command(flatten)]
pub installer: ResolverInstallerArgs,
@ -3313,6 +3352,19 @@ pub struct RemoveArgs {
#[arg(long, env = EnvVars::UV_NO_SYNC, value_parser = clap::builder::BoolishValueParser::new(), conflicts_with = "frozen")]
pub no_sync: bool,
/// Prefer the active virtual environment over the project's virtual environment.
///
/// If the project virtual environment is active or no virtual environment is active, this has
/// no effect.
#[arg(long, overrides_with = "no_active")]
pub active: bool,
/// Prefer project's virtual environment over an active environment.
///
/// This is the default behavior.
#[arg(long, overrides_with = "active", hide = true)]
pub no_active: bool,
/// Assert that the `uv.lock` will remain unchanged.
///
/// Requires that the lockfile is up-to-date. If the lockfile is missing or needs to be updated,