From 5e75f975ffde10846903549b54fe9f37231c457f Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Wed, 17 Jul 2024 13:46:00 +0200 Subject: [PATCH 1/5] fix(cli/package/tag): Better messages when tagging an existing package --- lib/cli/src/commands/package/tag.rs | 88 +++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/lib/cli/src/commands/package/tag.rs b/lib/cli/src/commands/package/tag.rs index c3099db0186..b63c514a1ea 100644 --- a/lib/cli/src/commands/package/tag.rs +++ b/lib/cli/src/commands/package/tag.rs @@ -12,14 +12,18 @@ use is_terminal::IsTerminal; use std::{ path::{Path, PathBuf}, str::FromStr, + sync::OnceLock, }; -use wasmer_api::WasmerClient; +use wasmer_api::{types::PackageVersionWithPackage, WasmerClient}; use wasmer_config::package::{ Manifest, NamedPackageId, NamedPackageIdent, PackageBuilder, PackageHash, PackageIdent, }; use super::PublishWait; +/// The package related to the NamedPackageId the user chose to tag. +static MAYBE_USER_PACKAGE: OnceLock> = OnceLock::new(); + /// Tag an existing package. #[derive(Debug, clap::Parser)] pub struct PackageTag { @@ -425,6 +429,28 @@ impl PackageTag { registry_version.clone() }; + // Must necessarily bump if there's a package with the chosen version with a different hash. + let must_bump = { + let maybe_pkg = wasmer_api::query::get_package_version( + client, + full_pkg_name.to_string(), + user_version.to_string(), + ) + .await?; + let maybe_hash = maybe_pkg + .as_ref() + .and_then(|p| p.distribution_v3.pirita_sha256_hash.clone()); + + MAYBE_USER_PACKAGE.set(maybe_pkg).unwrap(); + + if let Some(hash) = maybe_hash { + let registry_package_hash = PackageHash::from_str(&format!("sha256:{hash}"))?; + registry_package_hash != self.package_hash + } else { + false + } + }; + if user_version <= registry_version { if self.bump { user_version = registry_version.clone(); @@ -433,12 +459,31 @@ impl PackageTag { let theme = ColorfulTheme::default(); let mut new_version = registry_version.clone(); new_version.patch += 1; - if Confirm::with_theme(&theme) + if must_bump { + eprintln!("{}: Registry already has version {user_version} of {full_pkg_name}, but with different contents.", "Warn".bold().yellow()); + eprintln!("{}: Not bumping the version will make this action fail.", "Warn".bold().yellow()); + let res = Confirm::with_theme(&theme) + .with_prompt(format!("Continue ({user_version} -> {new_version})?")) + .interact()?; + if res { + user_version = new_version.clone(); + self.update_manifest_version(manifest_path, manifest, &user_version) + .await?; + } else { + anyhow::bail!( + "Refusing to map two different releases of {full_pkg_name} to the same version." + ) + } + } else { + let res = Confirm::with_theme(&theme) .with_prompt(format!("Do you want to bump the package's version ({user_version} -> {new_version})?")) - .interact()? { + .interact()?; + if res { user_version = new_version.clone(); - self.update_manifest_version(manifest_path, manifest, &user_version).await?; - } + self.update_manifest_version(manifest_path, manifest, &user_version) + .await?; + } + } } } @@ -450,7 +495,9 @@ impl PackageTag { Some(v) => Ok(v), None => { if self.non_interactive { - anyhow::bail!("No package name specified: use --version ") + anyhow::bail!( + "No package version specified: use --version " + ) } else { let version = crate::utils::prompts::prompt_for_package_version( "Enter the package version", @@ -547,20 +594,25 @@ impl PackageTag { return Ok(false); } - if let Some(pkg) = wasmer_api::query::get_package_version( - client, - id.full_name.clone(), - id.version.to_string(), - ) - .await? - { - if let Some(hash) = pkg.distribution_v3.pirita_sha256_hash { - let registry_package_hash = PackageHash::from_str(&format!("sha256:{hash}"))?; - if registry_package_hash == self.package_hash { - return Ok(false); - } + let hash = if let Some(pkg) = MAYBE_USER_PACKAGE.get().and_then(|p| p.clone()) { + pkg.distribution_v3.pirita_sha256_hash + } else { + wasmer_api::query::get_package_version( + client, + id.full_name.clone(), + id.version.to_string(), + ) + .await? + .and_then(|p| p.distribution_v3.pirita_sha256_hash) + }; + + if let Some(hash) = hash { + let registry_package_hash = PackageHash::from_str(&format!("sha256:{hash}"))?; + if registry_package_hash == self.package_hash { + return Ok(false); } } + Ok(true) } } From 0bc5a6fd347d7225fceea557a87bb5b26236390b Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Wed, 17 Jul 2024 16:45:27 +0200 Subject: [PATCH 2/5] chore: Make linter happy --- lib/cli/src/commands/package/tag.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cli/src/commands/package/tag.rs b/lib/cli/src/commands/package/tag.rs index b63c514a1ea..a79e3d8d876 100644 --- a/lib/cli/src/commands/package/tag.rs +++ b/lib/cli/src/commands/package/tag.rs @@ -461,7 +461,10 @@ impl PackageTag { new_version.patch += 1; if must_bump { eprintln!("{}: Registry already has version {user_version} of {full_pkg_name}, but with different contents.", "Warn".bold().yellow()); - eprintln!("{}: Not bumping the version will make this action fail.", "Warn".bold().yellow()); + eprintln!( + "{}: Not bumping the version will make this action fail.", + "Warn".bold().yellow() + ); let res = Confirm::with_theme(&theme) .with_prompt(format!("Continue ({user_version} -> {new_version})?")) .interact()?; From f3012559bc1cfc6e2ca761b6ca8aa423cc50e6ae Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Tue, 23 Jul 2024 16:22:58 +0200 Subject: [PATCH 3/5] feat: Minor changes to special-case for the `tag` subcommand --- lib/cli/src/commands/package/tag.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cli/src/commands/package/tag.rs b/lib/cli/src/commands/package/tag.rs index a79e3d8d876..63cdc78fda2 100644 --- a/lib/cli/src/commands/package/tag.rs +++ b/lib/cli/src/commands/package/tag.rs @@ -466,16 +466,16 @@ impl PackageTag { "Warn".bold().yellow() ); let res = Confirm::with_theme(&theme) - .with_prompt(format!("Continue ({user_version} -> {new_version})?")) + .with_prompt(format!( + "Update the version from {user_version} to {new_version}?" + )) .interact()?; if res { user_version = new_version.clone(); self.update_manifest_version(manifest_path, manifest, &user_version) .await?; } else { - anyhow::bail!( - "Refusing to map two different releases of {full_pkg_name} to the same version." - ) + anyhow::bail!("Aborted by the user") } } else { let res = Confirm::with_theme(&theme) From c9f854cac7de79080d6a4c4768f94ef23e259240 Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Thu, 25 Jul 2024 17:56:06 +0200 Subject: [PATCH 4/5] fix(cli/package/tag): Don't use a global static --- lib/cli/src/commands/package/tag.rs | 30 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/cli/src/commands/package/tag.rs b/lib/cli/src/commands/package/tag.rs index 63cdc78fda2..d31535cb096 100644 --- a/lib/cli/src/commands/package/tag.rs +++ b/lib/cli/src/commands/package/tag.rs @@ -12,7 +12,6 @@ use is_terminal::IsTerminal; use std::{ path::{Path, PathBuf}, str::FromStr, - sync::OnceLock, }; use wasmer_api::{types::PackageVersionWithPackage, WasmerClient}; use wasmer_config::package::{ @@ -21,9 +20,6 @@ use wasmer_config::package::{ use super::PublishWait; -/// The package related to the NamedPackageId the user chose to tag. -static MAYBE_USER_PACKAGE: OnceLock> = OnceLock::new(); - /// Tag an existing package. #[derive(Debug, clap::Parser)] pub struct PackageTag { @@ -92,6 +88,10 @@ pub struct PackageTag { value_enum )] pub wait: PublishWait, + + #[clap(skip)] + /// The package related to the NamedPackageId the user chose to tag. + pub(crate) maybe_user_package: Option, } impl PackageTag { @@ -377,7 +377,7 @@ impl PackageTag { } async fn get_version( - &self, + &mut self, client: &WasmerClient, manifest: Option<&Manifest>, manifest_path: Option<&Path>, @@ -441,7 +441,7 @@ impl PackageTag { .as_ref() .and_then(|p| p.distribution_v3.pirita_sha256_hash.clone()); - MAYBE_USER_PACKAGE.set(maybe_pkg).unwrap(); + self.maybe_user_package = maybe_pkg; if let Some(hash) = maybe_hash { let registry_package_hash = PackageHash::from_str(&format!("sha256:{hash}"))?; @@ -466,16 +466,16 @@ impl PackageTag { "Warn".bold().yellow() ); let res = Confirm::with_theme(&theme) - .with_prompt(format!( - "Update the version from {user_version} to {new_version}?" - )) + .with_prompt(format!("Continue ({user_version} -> {new_version})?")) .interact()?; if res { user_version = new_version.clone(); self.update_manifest_version(manifest_path, manifest, &user_version) .await?; } else { - anyhow::bail!("Aborted by the user") + anyhow::bail!( + "Refusing to map two different releases of {full_pkg_name} to the same version." + ) } } else { let res = Confirm::with_theme(&theme) @@ -518,7 +518,7 @@ impl PackageTag { } async fn synthesize_id( - &self, + &mut self, client: &WasmerClient, manifest: Option<&Manifest>, manifest_path: Option<&Path>, @@ -554,7 +554,7 @@ impl PackageTag { } pub async fn tag( - &self, + &mut self, client: &WasmerClient, manifest: Option<&Manifest>, manifest_path: Option<&Path>, @@ -597,8 +597,8 @@ impl PackageTag { return Ok(false); } - let hash = if let Some(pkg) = MAYBE_USER_PACKAGE.get().and_then(|p| p.clone()) { - pkg.distribution_v3.pirita_sha256_hash + let hash = if let Some(pkg) = self.maybe_user_package.as_ref() { + pkg.distribution_v3.pirita_sha256_hash.clone() } else { wasmer_api::query::get_package_version( client, @@ -624,7 +624,7 @@ impl PackageTag { impl AsyncCliCommand for PackageTag { type Output = (); - async fn run_async(self) -> Result { + async fn run_async(mut self) -> Result { tracing::info!("Checking if user is logged in"); let client = login_user(&self.api, &self.env, !self.non_interactive, "tag a package").await?; From 08f932e919a90e7667167f06b72be7ceaa986801 Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Thu, 25 Jul 2024 18:52:23 +0200 Subject: [PATCH 5/5] fix: Add missing field and missing annotation --- lib/cli/src/commands/package/mod.rs | 1 + lib/cli/src/commands/package/publish.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/cli/src/commands/package/mod.rs b/lib/cli/src/commands/package/mod.rs index 700ab1be823..1126e34c918 100644 --- a/lib/cli/src/commands/package/mod.rs +++ b/lib/cli/src/commands/package/mod.rs @@ -13,6 +13,7 @@ pub use common::wait::PublishWait; // Allowing missing_docs because the comment would override the doc comment on // the command struct. #[allow(missing_docs)] +#[allow(clippy::large_enum_variant)] pub enum Package { Download(download::PackageDownload), Build(build::PackageBuild), diff --git a/lib/cli/src/commands/package/publish.rs b/lib/cli/src/commands/package/publish.rs index e7e1a2dc3dd..0543bdbb765 100644 --- a/lib/cli/src/commands/package/publish.rs +++ b/lib/cli/src/commands/package/publish.rs @@ -120,6 +120,7 @@ impl PackagePublish { package_path: self.package_path.clone(), package_hash, package_id: None, + maybe_user_package: None, } .tag( client,