fix: prevent warning when using version range constraints

When using version ranges like ^1 or ~1.10, Helm incorrectly showed
warnings about falling back to closest version. Only show the warning
when an exact version is requested but not found.

Fixes: https://github.com/helm/helm/issues/31757

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
pull/31758/head
Benoit Tigeot 1 week ago
parent e1bdab214f
commit 9919f1ab8f
No known key found for this signature in database
GPG Key ID: 8E6D4FC8AEBDA62C

@ -175,6 +175,12 @@ func (i IndexFile) SortEntries() {
}
}
// isVersionRange checks if the version string is a range constraint (e.g., "^1", "~1.10")
// rather than an exact version (e.g., "1.10.0").
func isVersionRange(version string) bool {
return strings.ContainsAny(version, "^~<>=!*xX") || strings.Contains(version, " || ") || strings.Contains(version, " - ")
}
// Get returns the ChartVersion for the given name.
//
// If version is empty, this will return the chart with the latest stable version,
@ -215,7 +221,7 @@ func (i IndexFile) Get(name, version string) (*ChartVersion, error) {
}
if constraint.Check(test) {
if len(version) != 0 {
if len(version) != 0 && !isVersionRange(version) {
slog.Warn("unable to find exact version requested; falling back to closest available version", "chart", name, "requested", version, "selected", ver.Version)
}
return ver, nil

@ -718,3 +718,37 @@ func TestLoadIndex_DuplicateChartDeps(t *testing.T) {
})
}
}
func TestIsVersionRange(t *testing.T) {
tests := []struct {
version string
expected bool
}{
{"1.0.0", false},
{"1.0.0+metadata", false},
{"^1", true},
{"^1.2.3", true},
{"~1.10", true},
{"~1.10.0", true},
{">= 1.0.0", true},
{"> 1.0.0", true},
{"< 2.0.0", true},
{"<= 2.0.0", true},
{"!= 1.0.0", true},
{"1.*", true},
{"1.x", true},
{"1.X", true},
{"1.0.0 - 2.0.0", true},
{"^1.0.0 || ^2.0.0", true},
{">=1.0.0 <2.0.0", true},
}
for _, tt := range tests {
t.Run(tt.version, func(t *testing.T) {
got := isVersionRange(tt.version)
if got != tt.expected {
t.Errorf("isVersionRange(%q) = %v, want %v", tt.version, got, tt.expected)
}
})
}
}

Loading…
Cancel
Save