Skip to content

Commit 81c22e1

Browse files
committed
feat: Automatic Go version detection
1 parent da0a6b3 commit 81c22e1

File tree

9 files changed

+34
-3
lines changed

9 files changed

+34
-3
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ issues:
130130

131131
run:
132132
timeout: 5m
133+
go: '1.17' # TODO(ldez): we force to use an old version of Go for the CI and the tests.
133134
skip-dirs:
134135
- test/testdata_etc
135136
- internal/cache

pkg/commands/executor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func NewExecutor(version, commit, date string) *Executor {
110110
e.log.Fatalf("Can't read config: %s", err)
111111
}
112112

113+
if commandLineCfg.Run.Go == "" && e.cfg.Run.Go == "" {
114+
e.cfg.Run.Go = config.DetectGoVersion()
115+
}
116+
113117
// recreate after getting config
114118
e.DBManager = lintersdb.NewManager(e.cfg, e.log).WithCustomLinters()
115119

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/config/config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package config
22

3+
import (
4+
"github.com/ldez/gomoddirectives"
5+
)
6+
37
// Config encapsulates the config data specified in the golangci yaml config file.
48
type Config struct {
59
cfgDir string // The directory containing the golangci config file.
@@ -31,3 +35,18 @@ func NewDefault() *Config {
3135
type Version struct {
3236
Format string `mapstructure:"format"`
3337
}
38+
39+
func DetectGoVersion() string {
40+
const defaultGo = "1.17"
41+
42+
file, err := gomoddirectives.GetModuleFile()
43+
if err != nil {
44+
return defaultGo
45+
}
46+
47+
if file != nil && file.Go != nil {
48+
return file.Go.Version
49+
}
50+
51+
return defaultGo
52+
}

pkg/lint/linter/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func (lc *Config) WithNoopFallback(cfg *config.Config) *Config {
134134
return nil, nil
135135
},
136136
}
137+
138+
lc.LoadMode = 0
139+
return lc.WithLoadFiles()
137140
}
138141

139142
return lc

pkg/lint/linter/linter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ type Noop struct {
2222

2323
func (n Noop) Run(_ context.Context, lintCtx *Context) ([]result.Issue, error) {
2424
lintCtx.Log.Warnf("%s is disabled because of go1.18."+
25-
" If you are not using go1.18, you can set `go: go1.17` in the `run` section."+
2625
" You can track the evolution of the go1.18 support by following the https://github.com/golangci/golangci-lint/issues/2649.", n.name)
2726
return nil, nil
2827
}

test/fix_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestFix(t *testing.T) {
4343
t.Parallel()
4444

4545
args := []string{
46+
"--go=1.17", // TODO(ldez): we force to use an old version of Go for the CI and the tests.
4647
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number",
4748
"--allow-parallel-runners", "--fix",
4849
input,

test/linters_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finis
179179
func testOneSource(t *testing.T, sourcePath string) {
180180
args := []string{
181181
"run",
182+
"--go=1.17", // TODO(ldez): we force to use an old version of Go for the CI and the tests.
182183
"--allow-parallel-runners",
183184
"--disable-all",
184185
"--print-issued-lines=false",

test/testshared/testshared.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ func (r *LintRunner) Run(args ...string) *RunResult {
9898
func (r *LintRunner) RunCommand(command string, args ...string) *RunResult {
9999
r.Install()
100100

101-
runArgs := append([]string{command}, "--internal-cmd-test")
101+
runArgs := append([]string{command},
102+
"--go=1.17", // TODO(ldez): we force to use an old version of Go for the CI and the tests.
103+
"--internal-cmd-test",
104+
)
102105
runArgs = append(runArgs, args...)
103106

104107
defer func(startedAt time.Time) {

0 commit comments

Comments
 (0)