From f594570057cdecd262e4d35170563671f681f08f Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:52:31 +0800 Subject: [PATCH 1/4] Fix binary tool version check --- src/install/krate.rs | 5 +++++ src/install/mod.rs | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/install/krate.rs b/src/install/krate.rs index 62b44740..d5a16265 100644 --- a/src/install/krate.rs +++ b/src/install/krate.rs @@ -1,5 +1,6 @@ use crate::install::Tool; use anyhow::Result; +use log::debug; use serde::Deserialize; const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION"); @@ -25,6 +26,10 @@ impl Krate { .call()?; let kr: KrateResponse = res.into_json()?; + debug!( + "Latest `{name}` version on crates.io is {}", + kr.krate.max_version + ); Ok(kr.krate) } } diff --git a/src/install/mod.rs b/src/install/mod.rs index 747a1691..af2856fa 100644 --- a/src/install/mod.rs +++ b/src/install/mod.rs @@ -99,7 +99,7 @@ pub fn check_version(tool: &Tool, path: &Path, expected_version: &str) -> Result let v = get_cli_version(tool, path)?; info!( - "Checking installed `{}` version == expected version: {} == {}", + r#"Checking installed `{}` version == expected version: "{}" == "{}""#, tool, v, &expected_version ); Ok(v == expected_version) @@ -110,7 +110,7 @@ pub fn get_cli_version(tool: &Tool, path: &Path) -> Result { let mut cmd = Command::new(path); cmd.arg("--version"); let stdout = child::run_capture_stdout(cmd, tool)?; - let version = stdout.trim().split_whitespace().nth(1); + let version = stdout.trim().split_whitespace().last(); match version { Some(v) => Ok(v.to_string()), None => bail!("Something went wrong! We couldn't determine your version of the wasm-bindgen CLI. We were supposed to set that up for you, so it's likely not your fault! You should file an issue: https://github.com/rustwasm/wasm-pack/issues/new?template=bug_report.md.") From d5533bb6c96a7aa47749033405e5d85f41460472 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sat, 16 Sep 2023 19:07:17 +0800 Subject: [PATCH 2/4] More robust version extraction using regex --- Cargo.lock | 27 ++++++++++++++++++++------- Cargo.toml | 10 +++++++--- src/install/mod.rs | 17 +++++++++++++++-- tests/all/download.rs | 19 ++++++++++++++++++- 4 files changed, 60 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f1d5e16..b97cc5a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -207,7 +207,7 @@ checksum = "a246e68bb43f6cd9db24bea052a53e40405417c5fb372e3d1a8a7f770a564ef5" dependencies = [ "memchr", "once_cell", - "regex-automata", + "regex-automata 0.1.10", "serde", ] @@ -938,9 +938,9 @@ checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "memchr" -version = "2.5.0" +version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" [[package]] name = "miniz_oxide" @@ -1157,12 +1157,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.8.4" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f" +checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" dependencies = [ "aho-corasick", "memchr", + "regex-automata 0.3.8", "regex-syntax", ] @@ -1172,11 +1173,22 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +[[package]] +name = "regex-automata" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + [[package]] name = "regex-syntax" -version = "0.7.2" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" +checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" [[package]] name = "ring" @@ -1748,6 +1760,7 @@ dependencies = [ "log", "parking_lot", "predicates", + "regex", "semver", "serde", "serde_derive", diff --git a/Cargo.toml b/Cargo.toml index 60ddf2c6..2b02ffdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,10 @@ name = "wasm-pack" description = "📦✨ your favorite rust -> wasm workflow tool!" version = "0.12.1" -authors = ["Ashley Williams ", "Jesper Håkansson "] +authors = [ + "Ashley Williams ", + "Jesper Håkansson ", +] repository = "https://github.com/rustwasm/wasm-pack.git" license = "MIT OR Apache-2.0" edition = "2021" @@ -16,13 +19,16 @@ atty = "0.2.14" binary-install = "0.2.0" cargo_metadata = "0.15.2" chrono = "0.4.23" +clap = { version = "4.2.5", features = ["derive"] } console = "0.15.5" dialoguer = "0.10.3" env_logger = { version = "0.10.0", default-features = false } glob = "0.3.1" human-panic = "1.0.3" +lazy_static = "1.4.0" log = "0.4.17" parking_lot = "0.12.1" +regex = "1.9.5" semver = "1.0.16" serde = "1.0.152" serde_derive = "1.0.152" @@ -30,7 +36,6 @@ serde_ignored = "0.1.7" serde_json = "1.0.91" siphasher = "0.3.10" strsim = "0.10.0" -clap = { version = "4.2.5", features = ["derive"] } toml = "0.7.3" ureq = { version = "2.6.2", features = ["json"] } walkdir = "2.3.2" @@ -42,4 +47,3 @@ lazy_static = "1.4.0" predicates = "3.0.3" serial_test = "2.0.0" tempfile = "3.3.0" - diff --git a/src/install/mod.rs b/src/install/mod.rs index af2856fa..7be516f2 100644 --- a/src/install/mod.rs +++ b/src/install/mod.rs @@ -7,8 +7,10 @@ use crate::install; use crate::PBAR; use anyhow::{anyhow, bail, Context, Result}; use binary_install::{Cache, Download}; +use lazy_static::lazy_static; use log::debug; use log::{info, warn}; +use regex::Regex; use std::env; use std::fs; use std::path::Path; @@ -110,13 +112,24 @@ pub fn get_cli_version(tool: &Tool, path: &Path) -> Result { let mut cmd = Command::new(path); cmd.arg("--version"); let stdout = child::run_capture_stdout(cmd, tool)?; - let version = stdout.trim().split_whitespace().last(); - match version { + + match extract_version(&stdout) { Some(v) => Ok(v.to_string()), None => bail!("Something went wrong! We couldn't determine your version of the wasm-bindgen CLI. We were supposed to set that up for you, so it's likely not your fault! You should file an issue: https://github.com/rustwasm/wasm-pack/issues/new?template=bug_report.md.") } } +/// Extract the version number from the version string printed by a binary. +pub fn extract_version(raw: &str) -> Option<&str> { + lazy_static! { + static ref VERSION_EXTRACTOR: Regex = Regex::new(r"(^|\s)(\d+(\.\d+)*)($|\s)").unwrap(); + } + VERSION_EXTRACTOR + .captures(raw) + .and_then(|captures| captures.get(2)) + .map(|m| m.as_str()) +} + /// Downloads a precompiled copy of the tool, if available. pub fn download_prebuilt( tool: &Tool, diff --git a/tests/all/download.rs b/tests/all/download.rs index 9db79c76..46ebd326 100644 --- a/tests/all/download.rs +++ b/tests/all/download.rs @@ -1,4 +1,21 @@ -use wasm_pack::install::{self, Arch, Os, Tool}; +use wasm_pack::install::{self, extract_version, Arch, Os, Tool}; + +#[test] +fn can_extract_cli_version() { + let tests = [ + ("cargo-generate 0.18.4", "0.18.4"), + ("wasm-bindgen 0.2.87", "0.2.87"), + ("wasm-opt version 116", "116"), + ("cargo-generate 1", "1"), // missing minor & patch version + ("cargo-generate 0.18", "0.18"), // missing patch version + ("cargo generate 0.18.4", "0.18.4"), // space-separated subcommand + ("wasm-bindgen 0.2.87 (deadbeef)", "0.2.87"), // with commit hash + ]; + + for (i, o) in tests { + assert_eq!(extract_version(i), Some(o)); + } +} #[test] #[cfg(any( From f2a04cc93dc7b54fcc53049e2f1b53cbeb8a5a09 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sat, 16 Sep 2023 19:21:20 +0800 Subject: [PATCH 3/4] Add negative test for version extraction --- tests/all/download.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/all/download.rs b/tests/all/download.rs index 46ebd326..2d48653b 100644 --- a/tests/all/download.rs +++ b/tests/all/download.rs @@ -3,17 +3,18 @@ use wasm_pack::install::{self, extract_version, Arch, Os, Tool}; #[test] fn can_extract_cli_version() { let tests = [ - ("cargo-generate 0.18.4", "0.18.4"), - ("wasm-bindgen 0.2.87", "0.2.87"), - ("wasm-opt version 116", "116"), - ("cargo-generate 1", "1"), // missing minor & patch version - ("cargo-generate 0.18", "0.18"), // missing patch version - ("cargo generate 0.18.4", "0.18.4"), // space-separated subcommand - ("wasm-bindgen 0.2.87 (deadbeef)", "0.2.87"), // with commit hash + ("cargo-generate 0.18.4", Some("0.18.4")), + ("wasm-bindgen 0.2.87", Some("0.2.87")), + ("wasm-opt version 116", Some("116")), + ("cargo-generate 1", Some("1")), // missing minor & patch version + ("cargo-generate 0.18", Some("0.18")), // missing patch version + ("cargo generate 0.18.4", Some("0.18.4")), // space-separated subcommand + ("wasm-bindgen 0.2.87 (deadbeef)", Some("0.2.87")), // with commit hash + ("wasm-opt version", None), // version missing ]; for (i, o) in tests { - assert_eq!(extract_version(i), Some(o)); + assert_eq!(extract_version(i), o); } } From 4029762913bf9aa18a0e32704328ff8cdb5217de Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sat, 16 Sep 2023 19:24:19 +0800 Subject: [PATCH 4/4] Add raw version test for version extraction --- tests/all/download.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/all/download.rs b/tests/all/download.rs index 2d48653b..b38bf126 100644 --- a/tests/all/download.rs +++ b/tests/all/download.rs @@ -10,6 +10,7 @@ fn can_extract_cli_version() { ("cargo-generate 0.18", Some("0.18")), // missing patch version ("cargo generate 0.18.4", Some("0.18.4")), // space-separated subcommand ("wasm-bindgen 0.2.87 (deadbeef)", Some("0.2.87")), // with commit hash + ("69.420", Some("69.420")), // raw version ("wasm-opt version", None), // version missing ];