From 397a537eef163fc09eb88c4df5a9d5ffa41295dc Mon Sep 17 00:00:00 2001 From: Gideon <87426140+GideonBear@users.noreply.github.com> Date: Sun, 13 Apr 2025 10:07:57 +0200 Subject: [PATCH] Fix `uv` step (#1121) --- src/steps/generic.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/steps/generic.rs b/src/steps/generic.rs index 4da9aa73..cbfebb4a 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -1316,20 +1316,32 @@ pub fn run_uv(ctx: &ExecutionContext) -> Result<()> { .execute(&uv_exec) .arg("--version") .output_checked_utf8()?; - // example output: "uv 0.5.11 (c4d0caaee 2024-12-19)\n" + // Multiple possible output formats are possible according to uv source code + // + // https://github.com/astral-sh/uv/blob/6b7f60c1eaa840c2e933a0fb056ab46f99c991a5/crates/uv-cli/src/version.rs#L28-L42 + // + // For example: + // "uv 0.5.11 (c4d0caaee 2024-12-19)\n" + // "uv 0.5.11+1 (xxxd0cee 2024-12-20)\n" + // "uv 0.6.14\n" + let uv_version_output_stdout = uv_version_output.stdout; let version_str = { - // trim the starting "uv" and " " (whitespace) + // Trim the starting "uv" and " " (whitespace) let start_trimmed = uv_version_output_stdout .trim_start_matches("uv") .trim_start_matches(' '); - // remove the tailing part " (c4d0caaee 2024-12-19)\n" - let first_whitespace_index = start_trimmed - .find(' ') - .expect("the output of `uv --version` changed, please file an issue to Topgrade"); - // this should be our version str "0.5.11" - &start_trimmed[..first_whitespace_index] + // Remove the tailing part " (c4d0caaee 2024-12-19)\n", if it's there + match start_trimmed.find(' ') { + None => start_trimmed.trim_end_matches('\n'), // Otherwise, just strip the newline + Some(i) => &start_trimmed[..i], + } + + // After trimming, it should be a string in 2 possible formats, both can be handled by `Version::parse()` + // + // 1. "0.5.11" + // 2. "0.5.11+1" }; let version = Version::parse(version_str).expect("the output of `uv --version` changed, please file an issue to Topgrade");