Remove dangling environments in tool uninstall (#4740)

## Summary

It seems useful that `tool uninstall` guarantees the tool is gone (e.g.,
if the receipt and environment get out-of-sync somehow).
This commit is contained in:
Charlie Marsh 2024-07-02 17:03:39 -04:00 committed by GitHub
parent 32dc9bef59
commit 676d550410
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View file

@ -25,7 +25,22 @@ pub(crate) async fn uninstall(
let installed_tools = InstalledTools::from_settings()?;
let Some(receipt) = installed_tools.get_tool_receipt(&name)? else {
bail!("Tool `{}` is not installed", name);
// If the tool is not installed, attempt to remove the environment anyway.
match installed_tools.remove_environment(&name) {
Ok(()) => {
writeln!(
printer.stderr(),
"Removed dangling environment for tool: `{name}` (missing receipt)"
)?;
return Ok(ExitStatus::Success);
}
Err(uv_tool::Error::IO(err)) if err.kind() == std::io::ErrorKind::NotFound => {
bail!("Tool `{name}` is not installed");
}
Err(err) => {
return Err(err.into());
}
}
};
// Remove the tool itself.

View file

@ -108,12 +108,12 @@ fn tool_uninstall_missing_receipt() {
uv_snapshot!(context.filters(), context.tool_uninstall().arg("black")
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
success: false
exit_code: 2
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv tool uninstall` is experimental and may change without warning.
error: Tool `black` is not installed
Removed dangling environment for tool: `black` (missing receipt)
"###);
}