Skip to content

Commit 2d2d071

Browse files
committed
feat: auto-detect go version
1 parent f055441 commit 2d2d071

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-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: 14 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

@@ -164,6 +165,19 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
164165
unusedCfg = &m.cfg.LintersSettings.Unused
165166
varnamelenCfg = &m.cfg.LintersSettings.Varnamelen
166167
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
168+
169+
if m.cfg.Run.Go == "" {
170+
file, err := gomoddirectives.GetModuleFile()
171+
if err != nil {
172+
m.log.Warnf("unable to find go.mod file: %w", err)
173+
}
174+
175+
if file != nil && file.Go != nil {
176+
m.cfg.Run.Go = file.Go.Version
177+
} else {
178+
m.cfg.Run.Go = "1.17"
179+
}
180+
}
167181
}
168182

169183
const megacheckName = "megacheck"

0 commit comments

Comments
 (0)