Skip to content

Commit 872b465

Browse files
committed
Warn on existing directory if not tty
1 parent 0b88e16 commit 872b465

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/uv-virtualenv/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ uv-pypi-types = { workspace = true }
2727
uv-python = { workspace = true }
2828
uv-shell = { workspace = true }
2929
uv-version = { workspace = true }
30+
uv-warnings = { workspace = true }
3031

3132
console = { workspace = true }
3233
fs-err = { workspace = true }

crates/uv-virtualenv/src/virtualenv.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use uv_python::managed::{PythonMinorVersionLink, create_link_to_executable};
1919
use uv_python::{Interpreter, VirtualEnvironment};
2020
use uv_shell::escape_posix_for_single_quotes;
2121
use uv_version::version;
22+
use uv_warnings::warn_user_once;
2223

2324
use crate::{Error, Prompt};
2425

@@ -118,17 +119,27 @@ pub(crate) fn create(
118119
{
119120
debug!("Ignoring empty directory");
120121
} else {
121-
return Err(Error::Io(io::Error::new(
122-
io::ErrorKind::AlreadyExists,
123-
format!(
124-
"The directory `{}` exists. \n\n{}{} Use `{}` to remove the directory first or `{}` to write to the directory without clearing",
125-
location.user_display(),
126-
"hint".bold().cyan(),
127-
":".bold(),
128-
"--clear".green(),
129-
"--allow-existing".green(),
130-
),
131-
)));
122+
if Term::stderr().is_term() {
123+
return Err(Error::Io(io::Error::new(
124+
io::ErrorKind::AlreadyExists,
125+
format!(
126+
"The directory `{}` exists. \n\n{}{} Use `{}` to remove the directory first or `{}` to write to the directory without clearing",
127+
location.user_display(),
128+
"hint".bold().cyan(),
129+
":".bold(),
130+
"--clear".green(),
131+
"--allow-existing".green(),
132+
),
133+
)));
134+
}
135+
136+
// If this is not a tty, warn for now.
137+
warn_user_once!(
138+
"The directory `{}` exists. In the future, uv will require `{}` to remove the directory first or `{}` to write to the directory without clearing",
139+
location.user_display(),
140+
"--clear".green(),
141+
"--allow-existing".green(),
142+
);
132143
}
133144
}
134145
}

crates/uv/tests/it/venv.rs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,15 @@ fn create_venv() {
3636
.arg(context.venv.as_os_str())
3737
.arg("--python")
3838
.arg("3.12"), @r"
39-
success: false
40-
exit_code: 1
39+
success: true
40+
exit_code: 0
4141
----- stdout -----
4242
4343
----- stderr -----
4444
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
4545
Creating virtual environment at: .venv
46-
uv::venv::creation
47-
48-
× Failed to create virtualenv
49-
╰─▶ The directory `.venv` exists.
50-
51-
hint: Use `--clear` to remove the directory first or `--allow-existing` to write to the directory without clearing
46+
warning: The directory `.venv` exists. In the future, uv will require `--clear` to remove the directory first or `--allow-existing` to write to the directory without clearing
47+
Activate with: source .venv/[BIN]/activate
5248
"
5349
);
5450

@@ -994,21 +990,17 @@ fn non_empty_dir_exists() -> Result<()> {
994990
uv_snapshot!(context.filters(), context.venv()
995991
.arg(context.venv.as_os_str())
996992
.arg("--python")
997-
.arg("3.12"), @r###"
998-
success: false
999-
exit_code: 1
993+
.arg("3.12"), @r"
994+
success: true
995+
exit_code: 0
1000996
----- stdout -----
1001997
1002998
----- stderr -----
1003999
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
10041000
Creating virtual environment at: .venv
1005-
uv::venv::creation
1006-
1007-
× Failed to create virtualenv
1008-
╰─▶ The directory `.venv` exists.
1009-
1010-
hint: Use `--clear` to remove the directory first or `--allow-existing` to write to the directory without clearing
1011-
"###
1001+
warning: The directory `.venv` exists. In the future, uv will require `--clear` to remove the directory first or `--allow-existing` to write to the directory without clearing
1002+
Activate with: source .venv/[BIN]/activate
1003+
"
10121004
);
10131005

10141006
Ok(())
@@ -1026,21 +1018,17 @@ fn non_empty_dir_exists_allow_existing() -> Result<()> {
10261018
uv_snapshot!(context.filters(), context.venv()
10271019
.arg(context.venv.as_os_str())
10281020
.arg("--python")
1029-
.arg("3.12"), @r###"
1030-
success: false
1031-
exit_code: 1
1021+
.arg("3.12"), @r"
1022+
success: true
1023+
exit_code: 0
10321024
----- stdout -----
10331025
10341026
----- stderr -----
10351027
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
10361028
Creating virtual environment at: .venv
1037-
uv::venv::creation
1038-
1039-
× Failed to create virtualenv
1040-
╰─▶ The directory `.venv` exists.
1041-
1042-
hint: Use `--clear` to remove the directory first or `--allow-existing` to write to the directory without clearing
1043-
"###
1029+
warning: The directory `.venv` exists. In the future, uv will require `--clear` to remove the directory first or `--allow-existing` to write to the directory without clearing
1030+
Activate with: source .venv/[BIN]/activate
1031+
"
10441032
);
10451033

10461034
uv_snapshot!(context.filters(), context.venv()

0 commit comments

Comments
 (0)