Skip to content

Commit 7ec2df8

Browse files
authored
Merge pull request #2947 from itowlson/versions-do-not-alpha-sort
Fix plugins versions being alpha compared
2 parents 666faa9 + 171cdb0 commit 7ec2df8

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

crates/plugins/src/manager.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use reqwest::{header::HeaderMap, Client};
1212
use serde::Serialize;
1313
use spin_common::sha256;
1414
use std::{
15+
cmp::Ordering,
1516
fs::{self, File},
1617
io::{copy, Cursor},
1718
path::{Path, PathBuf},
@@ -173,7 +174,9 @@ impl PluginManager {
173174
name: plugin_manifest.name(),
174175
version: installed.version,
175176
});
176-
} else if installed.version > plugin_manifest.version && !allow_downgrades {
177+
} else if installed.compare_versions(plugin_manifest) == Some(Ordering::Greater)
178+
&& !allow_downgrades
179+
{
177180
bail!(
178181
"Newer version {} of plugin '{}' is already installed. To downgrade to version {}, run `spin plugins upgrade` with the `--downgrade` flag.",
179182
installed.version,

crates/plugins/src/manifest.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ impl PluginManifest {
7070
pub fn try_version(&self) -> Result<semver::Version, semver::Error> {
7171
semver::Version::parse(&self.version)
7272
}
73+
74+
// Compares the versions. Returns None if either's version string is invalid semver.
75+
pub fn compare_versions(&self, other: &Self) -> Option<std::cmp::Ordering> {
76+
if let Ok(this_version) = self.try_version() {
77+
if let Ok(other_version) = other.try_version() {
78+
return Some(this_version.cmp_precedence(&other_version));
79+
}
80+
}
81+
None
82+
}
7383
}
7484

7585
/// Describes compatibility and location of a plugin source.

0 commit comments

Comments
 (0)