From 668acc3e6431fdd6734f8a0f726d5d8a0d4ce687 Mon Sep 17 00:00:00 2001 From: Roland Schaer Date: Fri, 15 Mar 2024 01:22:58 -0300 Subject: [PATCH] fix(go): go backend supports versions prefixed with 'v' (#1753) * fix(go): go backend supports versions prefixed with 'v' * fix(go): use regex macro * fix(go): prevent clashes with Git hashes --- src/forge/go.rs | 18 ++++++++++++++++-- src/toolset/tool_version.rs | 5 ++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/forge/go.rs b/src/forge/go.rs index f40519612..a12d24ad9 100644 --- a/src/forge/go.rs +++ b/src/forge/go.rs @@ -32,7 +32,13 @@ impl Forge for GoForge { let res = cmd!("go", "list", "-m", "-versions", "-json", cur_mod_path).read(); if let Ok(raw) = res { let res = serde_json::from_str::(&raw); - if let Ok(mod_info) = res { + if let Ok(mut mod_info) = res { + // remove the leading v from the versions + mod_info.versions = mod_info + .versions + .into_iter() + .map(|v| v.trim_start_matches('v').to_string()) + .collect(); return Ok(mod_info.versions); } }; @@ -50,9 +56,17 @@ impl Forge for GoForge { let settings = Settings::get(); settings.ensure_experimental("go backend")?; + // if the (semantic) version has no v prefix, add it + // we allow max. 6 digits for the major version to prevent clashes with Git commit hashes + let version = if regex!(r"^\d{1,6}(\.\d+)*([+-.].+)?$").is_match(&ctx.tv.version) { + format!("v{}", ctx.tv.version) + } else { + ctx.tv.version.clone() + }; + CmdLineRunner::new("go") .arg("install") - .arg(&format!("{}@{}", self.name(), ctx.tv.version)) + .arg(&format!("{}@{}", self.name(), version)) .with_pr(ctx.pr.as_ref()) .envs(config.env()?) .env("GOBIN", ctx.tv.install_path().join("bin")) diff --git a/src/toolset/tool_version.rs b/src/toolset/tool_version.rs index b2c63ddca..4544321fd 100644 --- a/src/toolset/tool_version.rs +++ b/src/toolset/tool_version.rs @@ -6,6 +6,7 @@ use std::path::PathBuf; use console::style; use eyre::Result; +use regex::Captures; use versions::{Chunk, Version}; use crate::cli::args::ForgeArg; @@ -33,7 +34,9 @@ impl ToolVersion { ) -> Self { ToolVersion { forge: tool.fa().clone(), - version, + version: regex!(r"^v(\d+(\.\d+)*([+-.].+)?)$") + .replace(&version, |caps: &Captures| format!("{}", &caps[1])) + .to_string(), request, opts, }