Skip to content

Commit f0c1904

Browse files
authored
feat: check that Go version use to build is greater or equals to the Go version of the project (#4938)
1 parent 2f53f2c commit f0c1904

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

pkg/config/config.go

+37
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/config_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
78
)
89

910
func TestIsGoGreaterThanOrEqual(t *testing.T) {
@@ -131,3 +132,56 @@ func Test_trimGoVersion(t *testing.T) {
131132
})
132133
}
133134
}
135+
136+
func Test_checkGoVersion(t *testing.T) {
137+
testCases := []struct {
138+
desc string
139+
version string
140+
require require.ErrorAssertionFunc
141+
}{
142+
{
143+
desc: "version greater than runtime version (patch)",
144+
version: "1.30.1",
145+
require: require.Error,
146+
},
147+
{
148+
desc: "version greater than runtime version (family)",
149+
version: "1.30",
150+
require: require.Error,
151+
},
152+
{
153+
desc: "version greater than runtime version (RC)",
154+
version: "1.30.0-rc1",
155+
require: require.Error,
156+
},
157+
{
158+
desc: "version equals to runtime version",
159+
version: getRuntimeGoVersion(),
160+
require: require.NoError,
161+
},
162+
{
163+
desc: "version lower than runtime version (patch)",
164+
version: "1.19.1",
165+
require: require.NoError,
166+
},
167+
{
168+
desc: "version lower than runtime version (family)",
169+
version: "1.19",
170+
require: require.NoError,
171+
},
172+
{
173+
desc: "version lower than runtime version (RC)",
174+
version: "1.19.0-rc1",
175+
require: require.NoError,
176+
},
177+
}
178+
179+
for _, test := range testCases {
180+
t.Run(test.desc, func(t *testing.T) {
181+
t.Parallel()
182+
183+
err := checkGoVersion(test.version)
184+
test.require(t, err)
185+
})
186+
}
187+
}

pkg/config/loader.go

+5
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)