Skip to content

Commit 8df1ff6

Browse files
committed
feat: check that Go version use to build is greater or equals to the Go version of the project
1 parent 2f53f2c commit 8df1ff6

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/config/config.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package config
22

33
import (
4+
"fmt"
5+
"go/version"
46
"os"
57
"regexp"
8+
"runtime"
69
"strings"
710

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

109112
return v
110113
}
114+
115+
func getRuntimeGoVersion() string {
116+
goVersion := runtime.Version()
117+
118+
parts := strings.Fields(goVersion)
119+
120+
if len(parts) == 0 {
121+
return goVersion
122+
}
123+
124+
// When using GOEXPERIMENT, the version returned might look something like "go1.23.0 X:boringcrypto".
125+
return parts[0]
126+
}
127+
128+
func checkGoVersion(goVersion string) error {
129+
langVersion := version.Lang(getRuntimeGoVersion())
130+
131+
runtimeVersion, err := hcversion.NewVersion(strings.TrimPrefix(langVersion, "go"))
132+
if err != nil {
133+
return err
134+
}
135+
136+
targetedVersion, err := hcversion.NewVersion(trimGoVersion(goVersion))
137+
if err != nil {
138+
return err
139+
}
140+
141+
if runtimeVersion.LessThan(targetedVersion) {
142+
return fmt.Errorf("the Go language version (%s) used to build golangci-lint is lower than the targeted Go version (%s)",
143+
langVersion, goVersion)
144+
}
145+
146+
return nil
147+
}

pkg/config/loader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ func (l *Loader) Load(opts LoadOptions) error {
7474

7575
l.handleGoVersion()
7676

77+
err = checkGoVersion(l.cfg.Run.Go)
78+
if err != nil {
79+
return err
80+
}
81+
7782
err = l.handleEnableOnlyOption()
7883
if err != nil {
7984
return err

0 commit comments

Comments
 (0)