From 30beb6fee7b052e588ee72c238e315a557b5d6f2 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 29 Mar 2025 23:45:38 +0800 Subject: [PATCH] fix(cli): `tauri info` can't find the latest version for rust crates (#13096) * fix(cli): `tauri info` can't find the latest version for rust crates * Forget to remove dbg! * Use strip_suffix * Add change file --- .../fix-tauri-info-crate-latest-version.md | 5 +++ .changes/fix-tauri-info-request-log-level.md | 5 +++ Cargo.lock | 12 +++---- .../tauri-cli/src/helpers/cargo_manifest.rs | 31 ++++++++++++++----- crates/tauri-cli/src/info/packages_rust.rs | 22 +++++++------ 5 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 .changes/fix-tauri-info-crate-latest-version.md create mode 100644 .changes/fix-tauri-info-request-log-level.md diff --git a/.changes/fix-tauri-info-crate-latest-version.md b/.changes/fix-tauri-info-crate-latest-version.md new file mode 100644 index 000000000..03b6bd13c --- /dev/null +++ b/.changes/fix-tauri-info-crate-latest-version.md @@ -0,0 +1,5 @@ +--- +"tauri-cli": patch:bug +--- + +Fix `tauri info` can't find the latest version for rust crates diff --git a/.changes/fix-tauri-info-request-log-level.md b/.changes/fix-tauri-info-request-log-level.md new file mode 100644 index 000000000..854a099f7 --- /dev/null +++ b/.changes/fix-tauri-info-request-log-level.md @@ -0,0 +1,5 @@ +--- +"tauri-cli": patch:bug +--- + +Fix `tauri info` logging network operations in `info` level instead of `debug` diff --git a/Cargo.lock b/Cargo.lock index b9f60920b..201828a39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1355,7 +1355,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" dependencies = [ "lazy_static", - "windows-sys 0.59.0", + "windows-sys 0.48.0", ] [[package]] @@ -4305,7 +4305,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" dependencies = [ "cfg-if", - "windows-targets 0.52.6", + "windows-targets 0.48.5", ] [[package]] @@ -5792,7 +5792,7 @@ dependencies = [ "aes-gcm", "aes-kw", "argon2", - "base64 0.22.1", + "base64 0.21.7", "bitfield", "block-padding", "blowfish", @@ -9654,9 +9654,9 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "ureq" -version = "3.0.2" +version = "3.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa6a209bb4b8d5903579fc4f8cd12ce5594f1ed6b735bf290ab5e2bdb3e70013" +checksum = "217751151c53226090391713e533d9a5e904ba2570dabaaace29032687589c3e" dependencies = [ "base64 0.22.1", "cc", @@ -10177,7 +10177,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.48.0", ] [[package]] diff --git a/crates/tauri-cli/src/helpers/cargo_manifest.rs b/crates/tauri-cli/src/helpers/cargo_manifest.rs index 6e9ed5ba1..f72647327 100644 --- a/crates/tauri-cli/src/helpers/cargo_manifest.rs +++ b/crates/tauri-cli/src/helpers/cargo_manifest.rs @@ -97,15 +97,30 @@ impl std::fmt::Display for CrateVersion { } } +// Reference: https://github.com/rust-lang/crates.io/blob/98c83c8231cbcd15d6b8f06d80a00ad462f71585/src/views.rs#L274 +#[derive(serde::Deserialize)] +struct CrateMetadata { + /// The "default" version of this crate. + /// + /// This version will be displayed by default on the crate's page. + pub default_version: Option, +} + +// Reference: https://github.com/rust-lang/crates.io/blob/98c83c8231cbcd15d6b8f06d80a00ad462f71585/src/controllers/krate/metadata.rs#L44 +#[derive(serde::Deserialize)] +struct CrateIoGetResponse { + /// The crate metadata. + #[serde(rename = "crate")] + krate: CrateMetadata, +} + pub fn crate_latest_version(name: &str) -> Option { - let url = format!("https://docs.rs/crate/{name}/"); - let response = ureq::get(&url).call().ok()?; - if response.status().is_redirection() { - if let Some(location) = response.headers().get("location") { - return location.to_str().ok().map(|s| s.replace(&url, "")); - } - } - None + // Reference: https://github.com/rust-lang/crates.io/blob/98c83c8231cbcd15d6b8f06d80a00ad462f71585/src/controllers/krate/metadata.rs#L88 + let url = format!("https://crates.io/api/v1/crates/{name}?include"); + let mut response = ureq::get(&url).call().ok()?; + let metadata: CrateIoGetResponse = + serde_json::from_reader(response.body_mut().as_reader()).unwrap(); + metadata.krate.default_version } pub fn crate_version( diff --git a/crates/tauri-cli/src/info/packages_rust.rs b/crates/tauri-cli/src/info/packages_rust.rs index 0f3ca19e6..b2e4c873f 100644 --- a/crates/tauri-cli/src/info/packages_rust.rs +++ b/crates/tauri-cli/src/info/packages_rust.rs @@ -47,18 +47,20 @@ pub fn items(frontend_dir: Option<&PathBuf>, tauri_dir: Option<&Path>) -> Vec