Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List installed tools when no command is provided to uv tool run #5553

Merged
merged 2 commits into from
Jul 29, 2024
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
3 changes: 2 additions & 1 deletion crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2264,8 +2264,9 @@ pub struct ToolRunArgs {
///
/// If more complex version specification is desired or if the command is provided by a different
/// package, use `--from`.
/// If no command is given, should list installed tools and treat it as `uv tool list`
#[command(subcommand)]
pub command: ExternalCommand,
pub command: Option<ExternalCommand>,

/// Use the given package to provide the command.
///
Expand Down
9 changes: 7 additions & 2 deletions crates/uv/src/commands/tool/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::commands::reporters::PythonDownloadReporter;

use crate::commands::project::resolve_names;
use crate::commands::{
project, project::environment::CachedEnvironment, tool::common::matching_packages,
project, project::environment::CachedEnvironment, tool::common::matching_packages, tool_list,
};
use crate::commands::{ExitStatus, SharedState};
use crate::printer::Printer;
Expand All @@ -56,7 +56,7 @@ impl Display for ToolRunCommand {

/// Run a command.
pub(crate) async fn run(
command: ExternalCommand,
command: Option<ExternalCommand>,
from: Option<String>,
with: &[RequirementsSource],
python: Option<String>,
Expand All @@ -76,6 +76,11 @@ pub(crate) async fn run(
warn_user_once!("`{invocation_source}` is experimental and may change without warning");
}

// treat empty command as `uv tool list`
let Some(command) = command else {
return tool_list(false, PreviewMode::Enabled, cache, printer).await;
};

let (target, args) = command.split();
let Some(target) = target else {
return Err(anyhow::anyhow!("No tool command provided"));
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl RunSettings {
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone)]
pub(crate) struct ToolRunSettings {
pub(crate) command: ExternalCommand,
pub(crate) command: Option<ExternalCommand>,
pub(crate) from: Option<String>,
pub(crate) with: Vec<String>,
pub(crate) with_requirements: Vec<PathBuf>,
Expand Down
45 changes: 45 additions & 0 deletions crates/uv/tests/tool_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,48 @@ fn tool_run_requirements_txt_arguments() {
+ werkzeug==3.0.1
"###);
}

/// List installed tools when no command arg is given (e.g. `uv tool run`).
#[test]
fn tool_run_list_installed() {
let context = TestContext::new("3.12").with_filtered_exe_suffix();
let tool_dir = context.temp_dir.child("tools");
let bin_dir = context.temp_dir.child("bin");

// No tools installed.
uv_snapshot!(context.filters(), context.tool_run()
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
warning: `uv tool run` is experimental and may change without warning
No tools installed
"###);

// Install `black`.
context
.tool_install()
.arg("black==24.2.0")
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str())
.assert()
.success();

// List installed tools.
uv_snapshot!(context.filters(), context.tool_run()
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----
black v24.2.0
- black
- blackd

Comment on lines +805 to +809
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it also show a message about what the below list means:
Available tools and executables to run:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! However, since I'm simply reusing the tool_list, the output will exactly match uv run list. Additionally, if we want to display a message, we would probably expect the output of uv run list to change as well. I can make the change if desired, or perhaps it would be more suitable to make the change in a follow-up PR.

----- stderr -----
warning: `uv tool run` is experimental and may change without warning
"###);
}
Loading