Skip to content

Commit

Permalink
feat: auto-detect go version
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 21, 2022
1 parent f055441 commit 9e289cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/commands/run.go
Expand Up @@ -95,7 +95,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
"Modules download mode. If not empty, passed as -mod=<mode> to go tools")
fs.IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code",
exitcodes.IssuesFound, wh("Exit code when issues were found"))
fs.StringVar(&rc.Go, "go", "1.17", wh("Targeted Go version"))
fs.StringVar(&rc.Go, "go", "", wh("Targeted Go version"))
fs.StringSliceVar(&rc.BuildTags, "build-tags", nil, wh("Build tags"))

fs.DurationVar(&rc.Timeout, "deadline", defaultTimeout, wh("Deadline for total work"))
Expand Down
12 changes: 12 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"plugin"

"github.com/ldez/gomoddirectives"
"github.com/spf13/viper"
"golang.org/x/tools/go/analysis"

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

if m.cfg != nil && m.cfg.Run.Go == "" {
file, err := gomoddirectives.GetModuleFile()
if err != nil {
m.log.Warnf("unable to find go.mod file: %w", err)
}

if file != nil && file.Go != nil {
m.cfg.Run.Go = file.Go.Version
}
}

const megacheckName = "megacheck"

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

0 comments on commit 9e289cd

Please sign in to comment.