Skip to content

Commit

Permalink
Suppress resolver output by default in uv run
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 30, 2024
1 parent f7494f2 commit 2e93ce7
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 33 deletions.
12 changes: 12 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1946,6 +1946,12 @@ pub struct RunArgs {
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
#[arg(long, short, env = "UV_PYTHON", verbatim_doc_comment)]
pub python: Option<String>,

/// Whether to show resolver and installer output from any environment modifications.
///
/// By default, environment modifications are omitted, but enabled under `--verbose`.
#[arg(long, env = "UV_SHOW_RESOLUTION", value_parser = clap::builder::BoolishValueParser::new(), hide = true)]
pub show_resolution: bool,
}

#[derive(Args)]
Expand Down Expand Up @@ -2307,6 +2313,12 @@ pub struct ToolRunArgs {
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
#[arg(long, short, env = "UV_PYTHON", verbatim_doc_comment)]
pub python: Option<String>,

/// Whether to show resolver and installer output from any environment modifications.
///
/// By default, environment modifications are omitted, but enabled under `--verbose`.
#[arg(long, env = "UV_SHOW_RESOLUTION", value_parser = clap::builder::BoolishValueParser::new(), hide = true)]
pub show_resolution: bool,
}

#[derive(Args)]
Expand Down
13 changes: 7 additions & 6 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::settings::ResolverInstallerSettings;
pub(crate) async fn run(
command: ExternalCommand,
requirements: Vec<RequirementsSource>,
show_resolution: bool,
locked: bool,
frozen: bool,
package: Option<PackageName>,
Expand Down Expand Up @@ -87,7 +88,7 @@ pub(crate) async fn run(
// Initialize any shared state.
let state = SharedState::default();

let reporter = PythonDownloadReporter::single(printer);
let reporter = PythonDownloadReporter::single(printer.filter(show_resolution));

// Determine whether the command to execute is a PEP 723 script.
let script_interpreter = if let RunCommand::Python(target, _) = &command {
Expand Down Expand Up @@ -144,7 +145,7 @@ pub(crate) async fn run(
concurrency,
native_tls,
cache,
printer,
printer.filter(show_resolution),
)
.await?;

Expand Down Expand Up @@ -202,7 +203,7 @@ pub(crate) async fn run(
connectivity,
native_tls,
cache,
printer,
printer.filter(show_resolution),
)
.await?;

Expand All @@ -218,7 +219,7 @@ pub(crate) async fn run(
concurrency,
native_tls,
cache,
printer,
printer.filter(show_resolution),
)
.await
{
Expand Down Expand Up @@ -247,7 +248,7 @@ pub(crate) async fn run(
concurrency,
native_tls,
cache,
printer,
printer.filter(show_resolution),
)
.await?;

Expand Down Expand Up @@ -403,7 +404,7 @@ pub(crate) async fn run(
concurrency,
native_tls,
cache,
printer,
printer.filter(show_resolution),
)
.await?,
)
Expand Down
7 changes: 4 additions & 3 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, tool_list,
project::environment::CachedEnvironment, tool::common::matching_packages, tool_list,
};
use crate::commands::{ExitStatus, SharedState};
use crate::printer::Printer;
Expand Down Expand Up @@ -59,6 +59,7 @@ pub(crate) async fn run(
command: Option<ExternalCommand>,
from: Option<String>,
with: &[RequirementsSource],
show_resolution: bool,
python: Option<String>,
settings: ResolverInstallerSettings,
invocation_source: ToolRunCommand,
Expand Down Expand Up @@ -106,7 +107,7 @@ pub(crate) async fn run(
concurrency,
native_tls,
cache,
printer,
printer.filter(show_resolution),
)
.await?;

Expand Down Expand Up @@ -315,7 +316,7 @@ async fn get_or_create_environment(

// Resolve the `from` requirement.
let from = {
project::resolve_names(
resolve_names(
vec![RequirementsSpecification::parse_package(from)?],
&interpreter,
settings,
Expand Down
2 changes: 2 additions & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
args.command,
args.from,
&requirements,
args.show_resolution || globals.verbose > 0,
args.python,
args.settings,
invocation_source,
Expand Down Expand Up @@ -910,6 +911,7 @@ async fn run_project(
commands::run(
args.command,
requirements,
args.show_resolution || globals.verbose > 0,
args.locked,
args.frozen,
args.package,
Expand Down
10 changes: 10 additions & 0 deletions crates/uv/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ impl Printer {
Self::NoProgress => Stderr::Enabled,
}
}

/// Filter the [`Printer`], casting to [`Printer::Quiet`] if the condition is false.
#[must_use]
pub(crate) fn filter(self, condition: bool) -> Self {
if condition {
self
} else {
Self::Quiet
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
10 changes: 8 additions & 2 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub(crate) struct RunSettings {
pub(crate) command: ExternalCommand,
pub(crate) with: Vec<String>,
pub(crate) with_requirements: Vec<PathBuf>,
pub(crate) show_resolution: bool,
pub(crate) package: Option<PackageName>,
pub(crate) no_project: bool,
pub(crate) python: Option<String>,
Expand All @@ -206,8 +207,6 @@ impl RunSettings {
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn resolve(args: RunArgs, filesystem: Option<FilesystemOptions>) -> Self {
let RunArgs {
locked,
frozen,
extra,
all_extras,
no_all_extras,
Expand All @@ -216,6 +215,9 @@ impl RunSettings {
command,
with,
with_requirements,
show_resolution,
locked,
frozen,
installer,
build,
refresh,
Expand All @@ -238,6 +240,7 @@ impl RunSettings {
.into_iter()
.filter_map(Maybe::into_option)
.collect(),
show_resolution,
package,
no_project,
python,
Expand All @@ -258,6 +261,7 @@ pub(crate) struct ToolRunSettings {
pub(crate) from: Option<String>,
pub(crate) with: Vec<String>,
pub(crate) with_requirements: Vec<PathBuf>,
pub(crate) show_resolution: bool,
pub(crate) python: Option<String>,
pub(crate) refresh: Refresh,
pub(crate) settings: ResolverInstallerSettings,
Expand All @@ -272,6 +276,7 @@ impl ToolRunSettings {
from,
with,
with_requirements,
show_resolution,
installer,
build,
refresh,
Expand All @@ -286,6 +291,7 @@ impl ToolRunSettings {
.into_iter()
.filter_map(Maybe::into_option)
.collect(),
show_resolution,
python,
refresh: Refresh::from(refresh),
settings: ResolverInstallerSettings::combine(
Expand Down
21 changes: 12 additions & 9 deletions crates/uv/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,21 +481,24 @@ impl TestContext {
/// Create a `uv run` command with options shared across scenarios.
pub fn run(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("run");
command.arg("run").env("UV_SHOW_RESOLUTION", "1");
self.add_shared_args(&mut command);
command
}

/// Create a `uv tool run` command with options shared across scenarios.
pub fn tool_run(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("run");
command
.arg("tool")
.arg("run")
.env("UV_SHOW_RESOLUTION", "1");
self.add_shared_args(&mut command);
command
}

/// Create a `uv tool install` command with options shared across scenarios.
pub fn tool_install(&self) -> std::process::Command {
pub fn tool_install(&self) -> Command {
let mut command = self.tool_install_without_exclude_newer();
command.arg("--exclude-newer").arg(EXCLUDE_NEWER);
command
Expand All @@ -507,16 +510,16 @@ impl TestContext {
/// it can result in tests failing when the index state changes. Therefore,
/// if you use this, there should be some other kind of mitigation in place.
/// For example, pinning package versions.
pub fn tool_install_without_exclude_newer(&self) -> std::process::Command {
let mut command = std::process::Command::new(get_bin());
pub fn tool_install_without_exclude_newer(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("install");
self.add_shared_args(&mut command);
command
}

/// Create a `uv tool list` command with options shared across scenarios.
pub fn tool_list(&self) -> std::process::Command {
let mut command = std::process::Command::new(get_bin());
pub fn tool_list(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("list");
self.add_shared_args(&mut command);
command
Expand All @@ -531,8 +534,8 @@ impl TestContext {
}

/// Create a `uv tool uninstall` command with options shared across scenarios.
pub fn tool_uninstall(&self) -> std::process::Command {
let mut command = std::process::Command::new(get_bin());
pub fn tool_uninstall(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("uninstall");
self.add_shared_args(&mut command);
command
Expand Down
33 changes: 33 additions & 0 deletions crates/uv/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,36 @@ fn run_from_directory() -> Result<()> {

Ok(())
}

/// By default, omit resolver and installer output.
#[test]
fn run_without_output() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! { r#"
[project]
name = "foo"
version = "1.0.0"
requires-python = ">=3.8"
dependencies = ["anyio", "sniffio==1.3.1"]
"#
})?;

let test_script = context.temp_dir.child("main.py");
test_script.write_str(indoc! { r"
import sniffio
"
})?;

uv_snapshot!(context.filters(), context.run().env_remove("UV_SHOW_RESOLUTION").arg("--with").arg("iniconfig").arg("main.py"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv run` is experimental and may change without warning
"###);

Ok(())
}
50 changes: 37 additions & 13 deletions crates/uv/tests/tool_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ fn tool_run_at_version() {

// When `--from` is used, `@` is not treated as a version request
uv_snapshot!(filters, context.tool_run()
.arg("--from")
.arg("pytest")
.arg("[email protected]")
.arg("--version")
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
.arg("--from")
.arg("pytest")
.arg("[email protected]")
.arg("--version")
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
success: false
exit_code: 1
----- stdout -----
Expand Down Expand Up @@ -262,13 +262,13 @@ fn tool_run_warn_executable_not_in_from() {
filters.push(("(?s)fastapi` instead.*", "fastapi` instead."));

uv_snapshot!(filters, context.tool_run()
.arg("--from")
.arg("fastapi")
.arg("fastapi")
.env("UV_EXCLUDE_NEWER", "2024-05-04T00:00:00Z") // TODO: Remove this once EXCLUDE_NEWER is bumped past 2024-05-04
// (FastAPI 0.111 is only available from this date onwards)
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
.arg("--from")
.arg("fastapi")
.arg("fastapi")
.env("UV_EXCLUDE_NEWER", "2024-05-04T00:00:00Z") // TODO: Remove this once EXCLUDE_NEWER is bumped past 2024-05-04
// (FastAPI 0.111 is only available from this date onwards)
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
success: false
exit_code: 1
----- stdout -----
Expand Down Expand Up @@ -811,3 +811,27 @@ fn tool_run_list_installed() {
warning: `uv tool run` is experimental and may change without warning
"###);
}

/// By default, omit resolver and installer output.
#[test]
fn tool_run_without_output() {
let context = TestContext::new("3.12").with_filtered_counts();
let tool_dir = context.temp_dir.child("tools");
let bin_dir = context.temp_dir.child("bin");

uv_snapshot!(context.filters(), context.tool_run()
.env_remove("UV_SHOW_RESOLUTION")
.arg("--")
.arg("pytest")
.arg("--version")
.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 -----
pytest 8.1.1
----- stderr -----
warning: `uv tool run` is experimental and may change without warning
"###);
}

0 comments on commit 2e93ce7

Please sign in to comment.