Add --no-clear to uv venv to disable removal prompts (#15795)

<!--
Thank you for contributing to uv! To help us out with reviewing, please
consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

Closes #15485

---------

Co-authored-by: Aditya-PS-05 <adityapratapsjnhh7654@gmail.com>
Co-authored-by: Zanie Blue <contact@zanie.dev>
This commit is contained in:
Harshith VH 2025-09-16 18:54:57 +05:30 committed by GitHub
parent fb71b079d5
commit 2825ee3435
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 173 additions and 32 deletions

View file

@ -116,6 +116,22 @@ pub(crate) fn create(
} else {
"directory"
};
let hint = format!(
"Use the `{}` flag or set `{}` to replace the existing {name}",
"--clear".green(),
"UV_VENV_CLEAR=1".green()
);
// TODO(zanieb): We may want to consider omitting the hint in some of these cases, e.g.,
// when `--no-clear` is used do we want to suggest `--clear`?
let err = Err(Error::Io(io::Error::new(
io::ErrorKind::AlreadyExists,
format!(
"A {name} already exists at: {}\n\n{}{} {hint}",
location.user_display(),
"hint".bold().cyan(),
":".bold(),
),
)));
match on_existing {
OnExisting::Allow => {
debug!("Allowing existing {name} due to `--allow-existing`");
@ -131,15 +147,11 @@ pub(crate) fn create(
remove_virtualenv(&location)?;
fs::create_dir_all(&location)?;
}
OnExisting::Fail => {
let confirmation = if is_virtualenv {
confirm_clear(location, name)?
} else {
// Refuse to remove a non-virtual environment; don't even prompt.
Some(false)
};
match confirmation {
OnExisting::Fail => return err,
// If not a virtual environment, fail without prompting.
OnExisting::Prompt if !is_virtualenv => return err,
OnExisting::Prompt => {
match confirm_clear(location, name)? {
Some(true) => {
debug!("Removing existing {name} due to confirmation");
// Before removing the virtual environment, we need to canonicalize the
@ -151,22 +163,7 @@ pub(crate) fn create(
remove_virtualenv(&location)?;
fs::create_dir_all(&location)?;
}
Some(false) => {
let hint = format!(
"Use the `{}` flag or set `{}` to replace the existing {name}",
"--clear".green(),
"UV_VENV_CLEAR=1".green()
);
return Err(Error::Io(io::Error::new(
io::ErrorKind::AlreadyExists,
format!(
"A {name} already exists at: {}\n\n{}{} {hint}",
location.user_display(),
"hint".bold().cyan(),
":".bold(),
),
)));
}
Some(false) => return err,
// When we don't have a TTY, warn that the behavior will change in the future
None => {
warn_user_once!(
@ -637,10 +634,14 @@ pub fn remove_virtualenv(location: &Path) -> Result<(), Error> {
Ok(())
}
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum OnExisting {
/// Fail if the directory already exists and is non-empty.
/// Prompt before removing an existing directory.
///
/// If a TTY is not available, fail.
#[default]
Prompt,
/// Fail if the directory already exists and is non-empty.
Fail,
/// Allow an existing directory, overwriting virtual environment files while retaining other
/// files in the directory.
@ -650,13 +651,15 @@ pub enum OnExisting {
}
impl OnExisting {
pub fn from_args(allow_existing: bool, clear: bool) -> Self {
pub fn from_args(allow_existing: bool, clear: bool, no_clear: bool) -> Self {
if allow_existing {
Self::Allow
} else if clear {
Self::Remove
} else if no_clear {
Self::Fail
} else {
Self::default()
Self::Prompt
}
}
}