Skip to content

Commit 9e289cd

Browse files
committed
feat: auto-detect go version
1 parent f055441 commit 9e289cd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pkg/commands/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
9595
"Modules download mode. If not empty, passed as -mod=<mode> to go tools")
9696
fs.IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code",
9797
exitcodes.IssuesFound, wh("Exit code when issues were found"))
98-
fs.StringVar(&rc.Go, "go", "1.17", wh("Targeted Go version"))
98+
fs.StringVar(&rc.Go, "go", "", wh("Targeted Go version"))
9999
fs.StringSliceVar(&rc.BuildTags, "build-tags", nil, wh("Build tags"))
100100

101101
fs.DurationVar(&rc.Timeout, "deadline", defaultTimeout, wh("Deadline for total work"))

pkg/lint/lintersdb/manager.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66
"plugin"
77

8+
"github.com/ldez/gomoddirectives"
89
"github.com/spf13/viper"
910
"golang.org/x/tools/go/analysis"
1011

@@ -166,6 +167,17 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
166167
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
167168
}
168169

170+
if m.cfg != nil && m.cfg.Run.Go == "" {
171+
file, err := gomoddirectives.GetModuleFile()
172+
if err != nil {
173+
m.log.Warnf("unable to find go.mod file: %w", err)
174+
}
175+
176+
if file != nil && file.Go != nil {
177+
m.cfg.Run.Go = file.Go.Version
178+
}
179+
}
180+
169181
const megacheckName = "megacheck"
170182

171183
// The linters are sorted in the alphabetical order (case-insensitive).

0 commit comments

Comments
 (0)