Skip to content

Commit 24d524a

Browse files
Fix plugin release sorting to use semantic versioning (#1492)
* Fix plugin release sorting to use semantic versioning Plugin releases were being sorted lexicographically, causing versions like 1.10.0 to appear before 1.9.0. Now uses proper semver comparison to ensure correct ordering. Co-Authored-By: Claude Sonnet 4.5 <[email protected]> Committed-By-Agent: claude * make fmt * go mod tidy --------- Co-authored-by: Claude Sonnet 4.5 <[email protected]>
1 parent 83084ac commit 24d524a

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ require (
4646
github.com/go-git/go-git/v5 v5.14.0
4747
github.com/hashicorp/go-hclog v1.6.3
4848
github.com/hashicorp/go-plugin v1.6.3
49+
github.com/hashicorp/go-version v1.8.0
4950
github.com/joho/godotenv v1.5.1
5051
)
5152

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB1
110110
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
111111
github.com/hashicorp/go-plugin v1.6.3 h1:xgHB+ZUSYeuJi96WtxEjzi23uh7YQpznjGh0U0UUrwg=
112112
github.com/hashicorp/go-plugin v1.6.3/go.mod h1:MRobyh+Wc/nYy1V4KAXUiYfzxoYhs7V1mlH1Z7iY2h0=
113+
github.com/hashicorp/go-version v1.8.0 h1:KAkNb1HAiZd1ukkxDFGmokVZe1Xy9HG6NUp+bPle2i4=
114+
github.com/hashicorp/go-version v1.8.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
113115
github.com/hashicorp/yamux v0.1.2 h1:XtB8kyFOyHXYVFnwT5C3+Bdo8gArse7j2AQ0DA0Uey8=
114116
github.com/hashicorp/yamux v0.1.2/go.mod h1:C+zze2n6e/7wshOZep2A70/aQU6QBRWJO/G6FT1wIns=
115117
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=

pkg/plugins/utilities.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/BurntSushi/toml"
2020

2121
hcplugin "github.com/hashicorp/go-plugin"
22+
"github.com/hashicorp/go-version"
2223
"github.com/spf13/afero"
2324
"github.com/spf13/cobra"
2425

@@ -274,7 +275,15 @@ func addPluginToList(pluginList *PluginList, pl Plugin) {
274275

275276
// Other code assumes the releases are sorted with latest version last.
276277
sort.Slice(pluginList.Plugins[idx].Releases, func(i, j int) bool {
277-
return pluginList.Plugins[idx].Releases[i].Version < pluginList.Plugins[idx].Releases[j].Version
278+
vi, errI := version.NewVersion(pluginList.Plugins[idx].Releases[i].Version)
279+
vj, errJ := version.NewVersion(pluginList.Plugins[idx].Releases[j].Version)
280+
281+
// If either version fails to parse, fall back to string comparison
282+
if errI != nil || errJ != nil {
283+
return pluginList.Plugins[idx].Releases[i].Version < pluginList.Plugins[idx].Releases[j].Version
284+
}
285+
286+
return vi.LessThan(vj)
278287
})
279288
}
280289
}

pkg/plugins/utilities_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,45 @@ func TestRefreshPluginSucceedsIfAdditionalManifestNotFound(t *testing.T) {
399399
err := RefreshPluginManifest(context.Background(), config, fs, stripeServer.URL)
400400
require.Nil(t, err)
401401
}
402+
403+
func TestAddPluginToListSortsBySemver(t *testing.T) {
404+
pluginList := &PluginList{
405+
Plugins: []Plugin{
406+
{
407+
Shortname: "test-plugin",
408+
MagicCookieValue: "TEST-COOKIE-123",
409+
Releases: []Release{
410+
{Version: "1.0.0", OS: "darwin", Arch: "amd64"},
411+
{Version: "1.2.0", OS: "darwin", Arch: "amd64"},
412+
},
413+
},
414+
},
415+
}
416+
417+
// Add a plugin with versions that would be sorted incorrectly by string comparison
418+
newPlugin := Plugin{
419+
Shortname: "test-plugin",
420+
MagicCookieValue: "TEST-COOKIE-123",
421+
Releases: []Release{
422+
{Version: "1.10.0", OS: "darwin", Arch: "amd64"}, // String comparison would put this before 1.2.0
423+
{Version: "1.9.0", OS: "darwin", Arch: "amd64"}, // Should come after 1.2.0 but before 1.10.0
424+
{Version: "2.0.0", OS: "darwin", Arch: "amd64"},
425+
{Version: "1.0.1", OS: "darwin", Arch: "amd64"}, // Should come after 1.0.0 but before 1.2.0
426+
},
427+
}
428+
429+
addPluginToList(pluginList, newPlugin)
430+
431+
// Verify the plugin was merged (not added as a new one)
432+
require.Equal(t, 1, len(pluginList.Plugins))
433+
434+
// Verify all releases are present
435+
require.Equal(t, 6, len(pluginList.Plugins[0].Releases))
436+
437+
// Verify they are sorted by semver (not by string comparison)
438+
expectedOrder := []string{"1.0.0", "1.0.1", "1.2.0", "1.9.0", "1.10.0", "2.0.0"}
439+
for i, release := range pluginList.Plugins[0].Releases {
440+
require.Equal(t, expectedOrder[i], release.Version,
441+
"Expected release %d to be version %s, but got %s", i, expectedOrder[i], release.Version)
442+
}
443+
}

0 commit comments

Comments
 (0)