Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Avoid recursively deleting parent directories during uninstall
  • Loading branch information
charliermarsh committed Oct 12, 2026
commit 805248f2814491c08036a380b954d190cc08c908
8 changes: 6 additions & 2 deletions crates/uv-python-commands/src/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ pub async fn uninstall(
Err(err) => return Err(err.into()),
}

if uv_fs::directories(top_level)?.all(|path| uv_fs::is_temporary(&path)) {
fs_err::tokio::remove_dir_all(top_level).await?;
// The parent may contain user files, including `uv.toml` on Windows.
match fs_err::tokio::remove_dir(top_level).await {
Ok(()) => {}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) if err.kind() == io::ErrorKind::DirectoryNotEmpty => {}
Err(err) => return Err(err.into()),
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/uv-tool-commands/src/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ pub async fn uninstall(name: Vec<PackageName>, printer: Printer) -> Result<ExitS
.await
.ignore_currently_being_deleted()?;
if let Some(parent) = installed_tools.root().parent() {
if uv_fs::directories(parent)?.all(|path| uv_fs::is_temporary(&path)) {
fs_err::tokio::remove_dir_all(parent)
.await
.ignore_currently_being_deleted()?;
}
// The parent may contain user files, including `uv.toml` on Windows.
fs_err::tokio::remove_dir(parent)
.await
.ignore_currently_being_deleted()?;
}
}

Expand Down Expand Up @@ -86,6 +85,7 @@ impl IgnoreCurrentlyBeingDeleted for Result<(), std::io::Error> {
fn ignore_currently_being_deleted(self) -> Self {
match self {
Ok(()) => Ok(()),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(err) if err.kind() == std::io::ErrorKind::DirectoryNotEmpty => Ok(()),
Err(err) if err.is_in_process_of_being_deleted() => Ok(()),
Err(err) => Err(err),
Expand Down
66 changes: 65 additions & 1 deletion crates/uv/tests/tool/tool_uninstall.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::fixture::PathChild;
use assert_fs::fixture::{FileWriteStr, PathChild};

use uv_static::EnvVars;

Expand Down Expand Up @@ -50,6 +51,69 @@ fn tool_uninstall() {
");
}

/// Files beside the tools directory must survive removal of the last tool, even if no other
/// directories remain in the parent.
#[test]
fn tool_uninstall_preserves_parent_files() -> Result<()> {
let context = uv_test::test_context!("3.12")
.with_filtered_exe_suffix()
.with_tool_dirs();
let tool_dir = context.temp_dir.child("data/tools");
context
.temp_dir
.child("data/uv.toml")
.write_str("# User configuration\n")?;
context
.temp_dir
.child("data/keep.txt")
.write_str("Unrelated file\n")?;

context
.tool_install()
.arg("ruff==0.3.4")
.env(EnvVars::UV_TOOL_DIR, tool_dir.as_os_str())
.assert()
.success();

uv_snapshot!(context.filters(), context.tool_uninstall()
.arg("ruff")
.env(EnvVars::UV_TOOL_DIR, tool_dir.as_os_str()), @"
exit_code: 0 (success)
----- stderr -----
Uninstalled 1 executable: ruff
");

assert_eq!(context.read("data/uv.toml"), "# User configuration\n");
assert_eq!(context.read("data/keep.txt"), "Unrelated file\n");
assert!(!tool_dir.exists());

Ok(())
}

/// Uninstalling all tools also runs directory cleanup when no tools are installed.
#[test]
fn tool_uninstall_all_empty_preserves_parent_files() -> Result<()> {
let context = uv_test::test_context_with_versions!(&[]).with_tool_dirs();
let tool_dir = context.temp_dir.child("data/tools");
context
.temp_dir
.child("data/uv.toml")
.write_str("# User configuration\n")?;

uv_snapshot!(context.filters(), context.tool_uninstall()
.arg("--all")
.env(EnvVars::UV_TOOL_DIR, tool_dir.as_os_str()), @"
exit_code: 0 (success)
----- stderr -----
Nothing to uninstall
");

assert_eq!(context.read("data/uv.toml"), "# User configuration\n");
assert!(!tool_dir.exists());

Ok(())
}

#[test]
fn tool_uninstall_multiple_names() {
let context = uv_test::test_context!("3.12")
Expand Down
Loading
⌂ 换网址