From ea4284c041a1996bf7ff6a2cce298e8e73a820b7 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Sun, 4 May 2025 16:54:36 -0500 Subject: [PATCH] Add `--dry-run` support to `uv self update` (#9829) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See commentary at https://github.com/astral-sh/uv/issues/9828#issuecomment-2537542100 regarding the limitations and future upstream changes needed. ``` ❯ cargo build --features self-update Compiling uv v0.5.8 (/Users/zb/workspace/uv/crates/uv) Finished `dev` profile [unoptimized + debuginfo] target(s) in 7.28s ❯ cp ./target/debug/uv ~/.cargo/bin ❯ uv self update --dry-run info: Checking for updates... Nothing to do. You're on the latest version of uv (v0.5.8) ❯ uv self update --dry-run 0.5.7 info: Checking for updates... Would update uv from v0.5.8 to v0.5.7 ❯ vi ~/.config/uv/uv-receipt.json # Edit the receipt to think its on an older version ❯ uv self update --dry-run info: Checking for updates... Would update uv from v0.5.8 to the latest version ``` --------- Co-authored-by: Charlie Marsh --- crates/uv-cli/src/lib.rs | 4 ++++ crates/uv/src/commands/self_update.rs | 34 ++++++++++++++++++++++++++- crates/uv/src/lib.rs | 3 ++- docs/reference/cli.md | 2 ++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/crates/uv-cli/src/lib.rs b/crates/uv-cli/src/lib.rs index 8cbb05779..5ff82a219 100644 --- a/crates/uv-cli/src/lib.rs +++ b/crates/uv-cli/src/lib.rs @@ -589,6 +589,10 @@ pub struct SelfUpdateArgs { /// A token is not required but can be used to reduce the chance of encountering rate limits. #[arg(long, env = EnvVars::UV_GITHUB_TOKEN)] pub token: Option, + + /// Run without performing the update. + #[arg(long)] + pub dry_run: bool, } #[derive(Args)] diff --git a/crates/uv/src/commands/self_update.rs b/crates/uv/src/commands/self_update.rs index d8674c7a8..128f2ebbc 100644 --- a/crates/uv/src/commands/self_update.rs +++ b/crates/uv/src/commands/self_update.rs @@ -15,6 +15,7 @@ use crate::printer::Printer; pub(crate) async fn self_update( version: Option, token: Option, + dry_run: bool, printer: Printer, ) -> Result { let mut updater = AxoUpdater::new_for("uv"); @@ -87,7 +88,38 @@ pub(crate) async fn self_update( UpdateRequest::Latest }; - updater.configure_version_specifier(update_request); + updater.configure_version_specifier(update_request.clone()); + + if dry_run { + // TODO(charlie): `updater.fetch_release` isn't public, so we can't say what the latest + // version is. + if updater.is_update_needed().await? { + let version = match update_request { + UpdateRequest::Latest | UpdateRequest::LatestMaybePrerelease => { + "the latest version".to_string() + } + UpdateRequest::SpecificTag(version) | UpdateRequest::SpecificVersion(version) => { + format!("v{version}") + } + }; + writeln!( + printer.stderr(), + "Would update uv from {} to {}", + format!("v{}", env!("CARGO_PKG_VERSION")).bold().white(), + version.bold().white(), + )?; + } else { + writeln!( + printer.stderr(), + "{}", + format_args!( + "You're on the latest version of uv ({})", + format!("v{}", env!("CARGO_PKG_VERSION")).bold().white() + ) + )?; + } + return Ok(ExitStatus::Success); + } // Run the updater. This involves a network request, since we need to determine the latest // available version of uv. diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index e6dd8d7bb..be406ce82 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -1018,8 +1018,9 @@ async fn run(mut cli: Cli) -> Result { SelfCommand::Update(SelfUpdateArgs { target_version, token, + dry_run, }), - }) => commands::self_update(target_version, token, printer).await, + }) => commands::self_update(target_version, token, dry_run, printer).await, Commands::Self_(SelfNamespace { command: SelfCommand::Version { diff --git a/docs/reference/cli.md b/docs/reference/cli.md index 0d121791e..1c5bbeed7 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -9371,6 +9371,8 @@ uv self update [OPTIONS] [TARGET_VERSION]

See --project to only change the project root directory.

+
--dry-run

Run without performing the update

+
--help, -h

Display the concise help for this command

--managed-python

Require use of uv-managed Python versions.