Skip to content

Commit

Permalink
Merge pull request #2947 from itowlson/versions-do-not-alpha-sort
Browse files Browse the repository at this point in the history
Fix plugins versions being alpha compared
  • Loading branch information
itowlson authored Dec 4, 2024
2 parents 666faa9 + 171cdb0 commit 7ec2df8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crates/plugins/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use reqwest::{header::HeaderMap, Client};
use serde::Serialize;
use spin_common::sha256;
use std::{
cmp::Ordering,
fs::{self, File},
io::{copy, Cursor},
path::{Path, PathBuf},
Expand Down Expand Up @@ -173,7 +174,9 @@ impl PluginManager {
name: plugin_manifest.name(),
version: installed.version,
});
} else if installed.version > plugin_manifest.version && !allow_downgrades {
} else if installed.compare_versions(plugin_manifest) == Some(Ordering::Greater)
&& !allow_downgrades
{
bail!(
"Newer version {} of plugin '{}' is already installed. To downgrade to version {}, run `spin plugins upgrade` with the `--downgrade` flag.",
installed.version,
Expand Down
10 changes: 10 additions & 0 deletions crates/plugins/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ impl PluginManifest {
pub fn try_version(&self) -> Result<semver::Version, semver::Error> {
semver::Version::parse(&self.version)
}

// Compares the versions. Returns None if either's version string is invalid semver.
pub fn compare_versions(&self, other: &Self) -> Option<std::cmp::Ordering> {
if let Ok(this_version) = self.try_version() {
if let Ok(other_version) = other.try_version() {
return Some(this_version.cmp_precedence(&other_version));
}
}
None
}
}

/// Describes compatibility and location of a plugin source.
Expand Down

0 comments on commit 7ec2df8

Please sign in to comment.