Skip to content

Commit

Permalink
fix: trim Go version (#3995)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Aug 9, 2023
1 parent e0a8bd8 commit c1d8c56
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lintersdb

import (
"regexp"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters"
"github.com/golangci/golangci-lint/pkg/lint/linter"
Expand Down Expand Up @@ -227,24 +229,24 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
}

if gocriticCfg != nil {
gocriticCfg.Go = m.cfg.Run.Go
gocriticCfg.Go = trimGoVersion(m.cfg.Run.Go)
}

if gofumptCfg != nil && gofumptCfg.LangVersion == "" {
gofumptCfg.LangVersion = m.cfg.Run.Go
}

if staticcheckCfg != nil && staticcheckCfg.GoVersion == "" {
staticcheckCfg.GoVersion = m.cfg.Run.Go
staticcheckCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
}
if gosimpleCfg != nil && gosimpleCfg.GoVersion == "" {
gosimpleCfg.GoVersion = m.cfg.Run.Go
gosimpleCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
}
if stylecheckCfg != nil && stylecheckCfg.GoVersion != "" {
stylecheckCfg.GoVersion = m.cfg.Run.Go
stylecheckCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
}
if unusedCfg != nil && unusedCfg.GoVersion == "" {
unusedCfg.GoVersion = m.cfg.Run.Go
unusedCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
}
}

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

return ret
}

// Trims the Go version to keep only M.m.
// Since Go 1.21 the version inside the go.mod can be a patched version (ex: 1.21.0).
// https://go.dev/doc/toolchain#versions
// This a problem with staticcheck and gocritic.
func trimGoVersion(v string) string {
if v == "" {
return ""
}

exp := regexp.MustCompile(`(\d\.\d+)\.\d+`)

if exp.MatchString(v) {
return exp.FindStringSubmatch(v)[1]
}

return v
}

0 comments on commit c1d8c56

Please sign in to comment.