Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uv-tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ uv-settings = { workspace = true }
uv-state = { workspace = true }
uv-static = { workspace = true }
uv-virtualenv = { workspace = true }
uv-warnings = { workspace = true }

fs-err = { workspace = true }
owo-colors = { workspace = true }
Expand Down
16 changes: 11 additions & 5 deletions crates/uv-tool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use uv_dirs::user_executable_directory;
use uv_fs::{LockedFile, LockedFileError, LockedFileMode, Simplified};
use uv_install_wheel::read_record;
use uv_installer::SitePackages;
use uv_normalize::{InvalidNameError, PackageName};
use uv_normalize::PackageName;
use uv_pep440::Version;
use uv_python::{BrokenLink, Interpreter, PythonEnvironment};
use uv_state::{StateBucket, StateStore};
use uv_static::EnvVars;
use uv_warnings::warn_user;

pub(crate) use receipt::ToolReceipt;
pub use tool::{Tool, ToolEntrypoint};
Expand Down Expand Up @@ -77,8 +78,6 @@ pub enum Error {
#[error("Failed to find a directory to install executables into")]
NoExecutableDirectory,
#[error(transparent)]
ToolName(#[from] InvalidNameError),
#[error(transparent)]
EnvironmentError(#[from] uv_python::Error),
#[error("Failed to find a receipt for tool `{0}` at {1}")]
MissingToolReceipt(String, PathBuf),
Expand All @@ -101,7 +100,6 @@ impl Error {
| Self::VirtualEnvError(_)
| Self::EntrypointRead(_)
| Self::NoExecutableDirectory
| Self::ToolName(_)
| Self::EnvironmentError(_)
| Self::MissingToolReceipt(_, _)
| Self::EnvironmentRead(_, _)
Expand Down Expand Up @@ -148,6 +146,8 @@ impl InstalledTools {

/// Return the metadata for all installed tools.
///
/// Directories with invalid package names are skipped with a warning.
///
/// If a tool is present, but is missing a receipt or the receipt is invalid, the tool will be
/// included with an error.
///
Expand All @@ -162,7 +162,13 @@ impl InstalledTools {
else {
continue;
};
let name = PackageName::from_str(name)?;
let Ok(name) = PackageName::from_str(name) else {
warn_user!(
"Ignoring tool directory `{}` with an invalid package name; move it outside the tool directory, or remove it if no longer needed",
directory.user_display()
);
continue;
};
let path = directory.join("uv-receipt.toml");
let contents = match fs_err::read_to_string(&path) {
Ok(contents) => contents,
Expand Down
11 changes: 8 additions & 3 deletions crates/uv/src/commands/tool/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Context, Result};
use itertools::Itertools;
use owo_colors::OwoColorize;
use std::collections::BTreeMap;
Expand All @@ -13,7 +13,7 @@ use uv_configuration::{Concurrency, Constraints, DryRun, HashCheckingMode, Targe
use uv_distribution::LoweredExtraBuildDependencies;
use uv_distribution_types::{ExtraBuildRequires, Index, Name, Requirement, RequirementSource};
use uv_errors::{ErrorOptions, Hints, write_error_chain_with_options};
use uv_fs::CWD;
use uv_fs::{CWD, Simplified};
use uv_installer::{InstallationStrategy, Planner, SitePackages};
use uv_normalize::PackageName;
use uv_pep440::{Operator, Version};
Expand Down Expand Up @@ -68,7 +68,12 @@ pub(crate) async fn upgrade(
if names.is_empty() {
installed_tools
.tools()
.unwrap_or_default()
.with_context(|| {
format!(
"Failed to inspect installed tools in `{}`",
installed_tools.root().user_display()
)
})?
.into_iter()
.map(|(name, _)| (name, Vec::new()))
.collect()
Expand Down
32 changes: 29 additions & 3 deletions crates/uv/tests/tool/tool_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,41 @@ fn tool_upgrade_all_ignores_invalid_tool_name() -> Result<()> {

tool_dir.child("tool backup").create_dir_all()?;

// Silently treating an enumeration error as an empty tool directory hides a broken
// installation and exits successfully; see astral-sh/uv#21058.
uv_snapshot!(context.filters(), context.tool_upgrade()
.arg("--all")
.env(EnvVars::UV_TOOL_DIR, tool_dir.as_os_str())
.env(EnvVars::XDG_BIN_HOME, bin_dir.as_os_str())
.env(EnvVars::PATH, bin_dir.as_os_str()), @"
exit_code: 0 (success)
----- stderr -----
warning: Ignoring tool directory `tools/tool backup` with an invalid package name; move it outside the tool directory, or remove it if no longer needed
Nothing to upgrade
");

Ok(())
}

#[test]
fn tool_upgrade_all_unreadable_receipt() -> Result<()> {
let context = uv_test::test_context!("3.12").with_tool_dirs();
let tool_dir = context.temp_dir.child("tools");

tool_dir.child("babel").create_dir_all()?;
tool_dir
.child("babel")
.child("uv-receipt.toml")
.write_binary(&[0xff])?;

uv_snapshot!(context.filters(), context.tool_upgrade().arg("--all"), @"
exit_code: 2 (failure)
----- stderr -----
error: Failed to inspect installed tools in `tools`
Caused by: failed to read from file `[TEMP_DIR]/tools/babel/uv-receipt.toml`: stream did not contain valid UTF-8
");

Ok(())
}

#[test]
fn tool_upgrade_preserves_workspace_member_editability() -> Result<()> {
let context = uv_test::test_context!("3.12").with_filtered_exe_suffix();
Expand Down Expand Up @@ -754,7 +774,7 @@ fn tool_upgrade_pinned_hint_with_mixed_constraint() {
}

#[test]
fn tool_upgrade_all() {
fn tool_upgrade_all() -> Result<()> {
let context = uv_test::test_context!("3.12")
.with_filtered_counts()
.with_filtered_exe_suffix();
Expand Down Expand Up @@ -796,6 +816,9 @@ fn tool_upgrade_all() {
Installed 1 executable: pybabel
");

// An invalid directory must not prevent valid tools from being upgraded.
tool_dir.child("tool backup").create_dir_all()?;

// Upgrade all from PyPI.
uv_snapshot!(context.filters(), context.tool_upgrade()
.arg("--all")
Expand All @@ -806,6 +829,7 @@ fn tool_upgrade_all() {
.env(EnvVars::PATH, bin_dir.as_os_str()), @"
exit_code: 0 (success)
----- stderr -----
warning: Ignoring tool directory `tools/tool backup` with an invalid package name; move it outside the tool directory, or remove it if no longer needed
Updated babel v2.6.0 -> v2.14.0
- babel==2.6.0
+ babel==2.14.0
Expand All @@ -816,6 +840,8 @@ fn tool_upgrade_all() {
+ python-dotenv==1.0.1
Installed 1 executable: dotenv
");

Ok(())
}

#[test]
Expand Down
Loading