From 6963a2ec43cd30d05ad81bdadd512885307ffd0d Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Tue, 22 Mar 2022 12:39:29 +0100 Subject: [PATCH] feat: Automatic Go version detection --- .golangci.yml | 1 + pkg/commands/executor.go | 4 ++++ pkg/commands/run.go | 2 +- pkg/config/config.go | 22 ++++++++++++++++++++++ pkg/lint/linter/config.go | 3 +++ pkg/lint/linter/linter.go | 1 - test/fix_test.go | 1 + test/linters_test.go | 1 + test/testshared/testshared.go | 5 ++++- 9 files changed, 37 insertions(+), 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a17d2b119bad..4427980c5835 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -130,6 +130,7 @@ issues: run: timeout: 5m + go: '1.17' # TODO(ldez): we force to use an old version of Go for the CI and the tests. skip-dirs: - test/testdata_etc - internal/cache diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index 0ad37dde6ffc..4dee789cd945 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -110,6 +110,10 @@ func NewExecutor(version, commit, date string) *Executor { e.log.Fatalf("Can't read config: %s", err) } + if commandLineCfg.Run.Go == "" && e.cfg.Run.Go == "" { + e.cfg.Run.Go = config.DetectGo() + } + // recreate after getting config e.DBManager = lintersdb.NewManager(e.cfg, e.log).WithCustomLinters() diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 61f86ee52368..f4716d184819 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -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= 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")) diff --git a/pkg/config/config.go b/pkg/config/config.go index 49df4e495228..224807390ac6 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,5 +1,12 @@ package config +import ( + "strings" + + hcversion "github.com/hashicorp/go-version" + "github.com/ldez/gomoddirectives" +) + // Config encapsulates the config data specified in the golangci yaml config file. type Config struct { cfgDir string // The directory containing the golangci config file. @@ -31,3 +38,18 @@ func NewDefault() *Config { type Version struct { Format string `mapstructure:"format"` } + +func DetectGo() string { + const defaultGo = "1.17" + + file, err := gomoddirectives.GetModuleFile() + if err != nil { + return defaultGo + } + + if file != nil && file.Go != nil { + return file.Go.Version + } + + return defaultGo +} diff --git a/pkg/lint/linter/config.go b/pkg/lint/linter/config.go index e86a5dc44b9b..184b00cc7269 100644 --- a/pkg/lint/linter/config.go +++ b/pkg/lint/linter/config.go @@ -134,6 +134,9 @@ func (lc *Config) WithNoopFallback(cfg *config.Config) *Config { return nil, nil }, } + + lc.LoadMode = 0 + return lc.WithLoadFiles() } return lc diff --git a/pkg/lint/linter/linter.go b/pkg/lint/linter/linter.go index 04f0f6aeb81b..01e2196911e0 100644 --- a/pkg/lint/linter/linter.go +++ b/pkg/lint/linter/linter.go @@ -22,7 +22,6 @@ type Noop struct { func (n Noop) Run(_ context.Context, lintCtx *Context) ([]result.Issue, error) { lintCtx.Log.Warnf("%s is disabled because of go1.18."+ - " If you are not using go1.18, you can set `go: go1.17` in the `run` section."+ " You can track the evolution of the go1.18 support by following the https://github.com/golangci/golangci-lint/issues/2649.", n.name) return nil, nil } diff --git a/test/fix_test.go b/test/fix_test.go index 3d1144d5e6d7..ce61d85f2d59 100644 --- a/test/fix_test.go +++ b/test/fix_test.go @@ -43,6 +43,7 @@ func TestFix(t *testing.T) { t.Parallel() args := []string{ + "--go=1.17", // TODO(ldez): we force to use an old version of Go for the CI and the tests. "--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", "--allow-parallel-runners", "--fix", input, diff --git a/test/linters_test.go b/test/linters_test.go index a80baa6a511c..19c315ffef57 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -179,6 +179,7 @@ func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finis func testOneSource(t *testing.T, sourcePath string) { args := []string{ "run", + "--go=1.17", // TODO(ldez): we force to use an old version of Go for the CI and the tests. "--allow-parallel-runners", "--disable-all", "--print-issued-lines=false", diff --git a/test/testshared/testshared.go b/test/testshared/testshared.go index fa70c23131ae..9291532be429 100644 --- a/test/testshared/testshared.go +++ b/test/testshared/testshared.go @@ -98,7 +98,10 @@ func (r *LintRunner) Run(args ...string) *RunResult { func (r *LintRunner) RunCommand(command string, args ...string) *RunResult { r.Install() - runArgs := append([]string{command}, "--internal-cmd-test") + runArgs := append([]string{command}, + "--go=1.17", // TODO(ldez): we force to use an old version of Go for the CI and the tests. + "--internal-cmd-test", + ) runArgs = append(runArgs, args...) defer func(startedAt time.Time) {