Skip to content

Commit

Permalink
feat: check that Go version use to build is greater or equals to the …
Browse files Browse the repository at this point in the history
…Go version of the project
  • Loading branch information
ldez committed Aug 21, 2024
1 parent 2f53f2c commit 8df1ff6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package config

import (
"fmt"
"go/version"
"os"
"regexp"
"runtime"
"strings"

hcversion "github.com/hashicorp/go-version"
Expand Down Expand Up @@ -108,3 +111,37 @@ func trimGoVersion(v string) string {

return v
}

func getRuntimeGoVersion() string {
goVersion := runtime.Version()

parts := strings.Fields(goVersion)

if len(parts) == 0 {
return goVersion
}

// When using GOEXPERIMENT, the version returned might look something like "go1.23.0 X:boringcrypto".
return parts[0]
}

func checkGoVersion(goVersion string) error {
langVersion := version.Lang(getRuntimeGoVersion())

runtimeVersion, err := hcversion.NewVersion(strings.TrimPrefix(langVersion, "go"))
if err != nil {
return err
}

targetedVersion, err := hcversion.NewVersion(trimGoVersion(goVersion))
if err != nil {
return err
}

if runtimeVersion.LessThan(targetedVersion) {
return fmt.Errorf("the Go language version (%s) used to build golangci-lint is lower than the targeted Go version (%s)",
langVersion, goVersion)
}

return nil
}
5 changes: 5 additions & 0 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (l *Loader) Load(opts LoadOptions) error {

l.handleGoVersion()

err = checkGoVersion(l.cfg.Run.Go)
if err != nil {
return err
}

err = l.handleEnableOnlyOption()
if err != nil {
return err
Expand Down

0 comments on commit 8df1ff6

Please sign in to comment.