Skip to content

Commit

Permalink
feat: Automatic Go version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 22, 2022
1 parent f055441 commit 6963a2e
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/executor.go
Expand Up @@ -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()

Expand Down
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
22 changes: 22 additions & 0 deletions 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.
Expand Down Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions pkg/lint/linter/config.go
Expand Up @@ -134,6 +134,9 @@ func (lc *Config) WithNoopFallback(cfg *config.Config) *Config {
return nil, nil
},
}

lc.LoadMode = 0
return lc.WithLoadFiles()
}

return lc
Expand Down
1 change: 0 additions & 1 deletion pkg/lint/linter/linter.go
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions test/fix_test.go
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/linters_test.go
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion test/testshared/testshared.go
Expand Up @@ -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) {
Expand Down

0 comments on commit 6963a2e

Please sign in to comment.