Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not modify the original version #4

Merged
merged 2 commits into from
May 30, 2024

Conversation

nikpivkin
Copy link
Contributor

Methods that bump a version modify the original object, which leads to unexpected results when comparing a constraint in a loop:

package main

import (
	"fmt"
	"sort"

	"github.com/aquasecurity/go-version/pkg/version"
)

func main() {
	versionsRaw := []string{"3.0.0", "3.1.0", "3.1.1", "3.1.2", "3.1.3", "3.2.0", "4.0.0"}
	versions := make([]version.Version, len(versionsRaw))
	for i, raw := range versionsRaw {
		v, _ := version.Parse(raw)
		versions[i] = v
	}

	sort.Sort(sort.Reverse(version.Collection(versions)))

	fmt.Println(versions)
	constraints, _ := version.NewConstraints("~> 3.0")
	for _, ver := range versions {
		if constraints.Check(ver) {
			fmt.Println(ver)
			return
		}
	}

	fmt.Println("Not found")
}
go run .
[4.0.0 3.2.0 3.1.3 3.1.2 3.1.1 3.1.0 3.0.0]
Not found

Also from the Trivy logs:

Requesting module versions from registry using 'https://registry.terraform.io/v1/modules/terraform-google-modules/kubernetes-engine/google/versions'...
Found version '29.0.0' for constraint '~> 24.0'

@knqyf263
Copy link
Collaborator

We probably need to bump Go and golangci-lint first.

@nikpivkin
Copy link
Contributor Author

@knqyf263 Should we open a separate PR for this?

@knqyf263
Copy link
Collaborator

yes, please

@nikpivkin
Copy link
Contributor Author

@knqyf263 I opened PR #5

@knqyf263
Copy link
Collaborator

Thanks. Merged #5

@nikpivkin nikpivkin requested a review from knqyf263 May 30, 2024 10:54
@knqyf263 knqyf263 merged commit 9f59392 into aquasecurity:main May 30, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants