Skip to content

Commit 43c4f6e

Browse files
committed
fix: trim Go version
1 parent e0a8bd8 commit 43c4f6e

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

pkg/lint/lintersdb/manager.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package lintersdb
22

33
import (
4+
"regexp"
5+
46
"github.com/golangci/golangci-lint/pkg/config"
57
"github.com/golangci/golangci-lint/pkg/golinters"
68
"github.com/golangci/golangci-lint/pkg/lint/linter"
@@ -227,24 +229,24 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
227229
}
228230

229231
if gocriticCfg != nil {
230-
gocriticCfg.Go = m.cfg.Run.Go
232+
gocriticCfg.Go = trimGoVersion(m.cfg.Run.Go)
231233
}
232234

233235
if gofumptCfg != nil && gofumptCfg.LangVersion == "" {
234236
gofumptCfg.LangVersion = m.cfg.Run.Go
235237
}
236238

237239
if staticcheckCfg != nil && staticcheckCfg.GoVersion == "" {
238-
staticcheckCfg.GoVersion = m.cfg.Run.Go
240+
staticcheckCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
239241
}
240242
if gosimpleCfg != nil && gosimpleCfg.GoVersion == "" {
241-
gosimpleCfg.GoVersion = m.cfg.Run.Go
243+
gosimpleCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
242244
}
243245
if stylecheckCfg != nil && stylecheckCfg.GoVersion != "" {
244-
stylecheckCfg.GoVersion = m.cfg.Run.Go
246+
stylecheckCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
245247
}
246248
if unusedCfg != nil && unusedCfg.GoVersion == "" {
247-
unusedCfg.GoVersion = m.cfg.Run.Go
249+
unusedCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
248250
}
249251
}
250252

@@ -928,3 +930,21 @@ func (m Manager) GetAllLinterConfigsForPreset(p string) []*linter.Config {
928930

929931
return ret
930932
}
933+
934+
// Trims the Go version to keep only M.m.
935+
// Since Go 1.12 the version inside the go.mod can be a patched version (i.e. v1.12.0).
936+
// https://go.dev/doc/toolchain#versions
937+
// This a problem with staticcheck and gocritic.
938+
func trimGoVersion(v string) string {
939+
if v == "" {
940+
return ""
941+
}
942+
943+
exp := regexp.MustCompile(`(\d\.\d+)\.\d+`)
944+
945+
if exp.MatchString(v) {
946+
return exp.FindStringSubmatch(v)[1]
947+
}
948+
949+
return v
950+
}

0 commit comments

Comments
 (0)