Skip to content

Commit

Permalink
uv tool run error messages references uvx when appropriate (#5014)
Browse files Browse the repository at this point in the history
## Summary

Resolves #5013. 

## Test Plan

```console
❯ ./target/debug/uv tool run fastapi-cli
warning: `uv tool run` is experimental and may change without warning.
Resolved 9 packages in 28ms
The executable fastapi-cli was not found.
However, the following executables are available via uv tool run --from fastapi-cli <EXECUTABLE>:
- fastapi
```

```console
❯ ./target/debug/uvx fastapi-cli
warning: `uvx` is experimental and may change without warning.
Resolved 9 packages in 23ms
The executable fastapi-cli was not found.
However, the following executables are available via uvx --from fastapi-cli <EXECUTABLE>:
- fastapi
```

---------

Co-authored-by: Zanie Blue <[email protected]>
  • Loading branch information
blueraft and zanieb committed Jul 12, 2024
1 parent 2ccb7ed commit 26d8879
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/uv/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub(crate) use tool::dir::dir as tool_dir;
pub(crate) use tool::install::install as tool_install;
pub(crate) use tool::list::list as tool_list;
pub(crate) use tool::run::run as tool_run;
pub(crate) use tool::run::ToolRunCommand;
pub(crate) use tool::uninstall::uninstall as tool_uninstall;
use uv_cache::Cache;
use uv_fs::Simplified;
Expand Down
24 changes: 21 additions & 3 deletions crates/uv/src/commands/tool/run.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::borrow::Cow;
use std::ffi::OsString;
use std::fmt::Write;
use std::path::PathBuf;
use std::str::FromStr;
use std::{borrow::Cow, fmt::Display};

use anyhow::{bail, Context, Result};
use itertools::Itertools;
Expand Down Expand Up @@ -32,13 +32,31 @@ use crate::commands::{ExitStatus, SharedState};
use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings;

/// The user-facing command used to invoke a tool run.
pub(crate) enum ToolRunCommand {
/// via the `uvx` alias
Uvx,
/// via `uv tool run`
ToolRun,
}

impl Display for ToolRunCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ToolRunCommand::Uvx => write!(f, "uvx"),
ToolRunCommand::ToolRun => write!(f, "uv tool run"),
}
}
}

/// Run a command.
pub(crate) async fn run(
command: ExternalCommand,
from: Option<String>,
with: Vec<String>,
python: Option<String>,
settings: ResolverInstallerSettings,
invocation_source: ToolRunCommand,
isolated: bool,
preview: PreviewMode,
python_preference: PythonPreference,
Expand All @@ -50,7 +68,7 @@ pub(crate) async fn run(
printer: Printer,
) -> Result<ExitStatus> {
if preview.is_disabled() {
warn_user_once!("`uv tool run` is experimental and may change without warning.");
warn_user_once!("`{invocation_source}` is experimental and may change without warning.");
}

let has_from = from.is_some();
Expand Down Expand Up @@ -144,7 +162,7 @@ pub(crate) async fn run(
"However, the following executables are available:",
)?;
} else {
let command = format!("uv tool run --from {from} <EXECUTABLE>");
let command = format!("{invocation_source} --from {from} <EXECUTABLE>");
writeln!(
printer.stdout(),
"However, the following executables are available via {}:",
Expand Down
13 changes: 10 additions & 3 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use uv_distribution::Workspace;
use uv_requirements::RequirementsSource;
use uv_settings::{Combine, FilesystemOptions};

use crate::commands::ExitStatus;
use crate::commands::{ExitStatus, ToolRunCommand};
use crate::printer::Printer;
use crate::settings::{
CacheSettings, GlobalSettings, PipCheckSettings, PipCompileSettings, PipFreezeSettings,
Expand Down Expand Up @@ -598,21 +598,28 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
Ok(ExitStatus::Success)
}
Commands::Tool(ToolNamespace {
command: ToolCommand::Run(args) | ToolCommand::Uvx(args),
command: run_variant @ (ToolCommand::Uvx(_) | ToolCommand::Run(_)),
}) => {
let (args, invocation_source) = match run_variant {
ToolCommand::Uvx(args) => (args, ToolRunCommand::Uvx),
ToolCommand::Run(args) => (args, ToolRunCommand::ToolRun),
// OK guarded by the outer match statement
_ => unreachable!(),
};

// Resolve the settings from the command-line arguments and workspace configuration.
let args = settings::ToolRunSettings::resolve(args, filesystem);
show_settings!(args);

// Initialize the cache.
let cache = cache.init()?.with_refresh(args.refresh);

commands::tool_run(
args.command,
args.from,
args.with,
args.python,
args.settings,
invocation_source,
globals.isolated,
globals.preview,
globals.python_preference,
Expand Down

0 comments on commit 26d8879

Please sign in to comment.