From e17bba746790bd000101463c10bd741f92935032 Mon Sep 17 00:00:00 2001 From: Xianpeng Shen <3353385+shenxianpeng@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:09:05 +0000 Subject: [PATCH] feat: accept `latest` for `--version` `latest` requests the newest major version in the static binaries range known at build time, so cpp-linter-action v3 can default to it instead of a hard-coded number. --- clang-tools-manager/src/main.rs | 3 +++ clang-tools-manager/src/version.rs | 33 +++++++++++++++++++++++++++++- cpp-linter/src/cli/mod.rs | 4 ++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/clang-tools-manager/src/main.rs b/clang-tools-manager/src/main.rs index 484274e3..741e033f 100644 --- a/clang-tools-manager/src/main.rs +++ b/clang-tools-manager/src/main.rs @@ -11,6 +11,9 @@ use std::{collections::HashMap, path::PathBuf, str::FromStr}; #[command(styles(CLI_HELP_STYLE))] pub struct CliOptions { /// The desired version of clang to install. + /// + /// Takes a version requirement (eg. `18` or `=18.1.8`), `latest` for + /// the newest major version of the static binaries, or a path. #[arg( short, long, diff --git a/clang-tools-manager/src/version.rs b/clang-tools-manager/src/version.rs index 652e769e..0fe0f3a0 100644 --- a/clang-tools-manager/src/version.rs +++ b/clang-tools-manager/src/version.rs @@ -269,6 +269,21 @@ pub enum RequestedVersionParsingError { NonCanonicalPath(String, std::io::Error), } +impl RequestedVersion { + /// The value that requests the newest major version + /// in the range of the static binaries (see [`RequestedVersion::latest()`]). + pub const LATEST: &str = "latest"; + + /// Request the newest major version in the range of the static binaries + /// that this build knows about (any minor or patch release of it). + /// + /// The range is read when this crate is built, so it does not change at run time. + pub fn latest() -> Self { + let major = *StaticDistDownloader::get_major_version_range().end(); + Self::Requirement(VersionReq::parse(&major.to_string()).unwrap_or(VersionReq::STAR)) + } +} + impl FromStr for RequestedVersion { type Err = RequestedVersionParsingError; @@ -277,6 +292,8 @@ impl FromStr for RequestedVersion { Ok(Self::SystemDefault) } else if input == "CPP-LINTER-VERSION" { Ok(Self::NoValue) + } else if input.eq_ignore_ascii_case(Self::LATEST) { + Ok(Self::latest()) } else if let Ok(req) = VersionReq::parse(input) { Ok(Self::Requirement(req)) } else { @@ -312,7 +329,10 @@ mod tests { use semver::VersionReq; use tempfile::TempDir; - use super::{RequestedVersion, RequestedVersionParsingError, requires_exact_version}; + use super::{ + RequestedVersion, RequestedVersionParsingError, StaticDistDownloader, + requires_exact_version, + }; use crate::{ClangTool, utils::normalize_path}; // See also crate::tool::tests module for other `RequestedVersion::from_str()` tests. @@ -409,6 +429,17 @@ mod tests { ); } + #[test] + fn validate_version_latest() { + let major = *StaticDistDownloader::get_major_version_range().end(); + let expected = + RequestedVersion::Requirement(VersionReq::parse(&format!("^{major}")).unwrap()); + for input in ["latest", "LATEST", "Latest"] { + assert_eq!(RequestedVersion::from_str(input).unwrap(), expected); + } + assert_eq!(RequestedVersion::latest(), expected); + } + #[test] fn validate_version_exact() { let req_ver = RequestedVersion::from_str("12").unwrap(); diff --git a/cpp-linter/src/cli/mod.rs b/cpp-linter/src/cli/mod.rs index 409b8caa..f7d10db2 100644 --- a/cpp-linter/src/cli/mod.rs +++ b/cpp-linter/src/cli/mod.rs @@ -103,6 +103,10 @@ pub struct GeneralOptions { /// `18.1.8` accepts 18.1.8 or a newer 18.x release, and `16` accepts any 16.x release. /// Use `=18.1.8` to require exactly that version. System package managers /// are not used for such a version because they only install by major version. + /// - `latest` for the newest major version that cpp-linter can install + /// from its static binaries (any minor or patch release of it). + /// This version is fixed when cpp-linter is built, so it only changes + /// with a cpp-linter update. /// - A blank string (`''`) to use the platform's default /// installed version. /// - A path to where the clang tools are