Skip to content

Commit 301efb3

Browse files
authored
fix: validate version before configuration (#5599)
1 parent 7868040 commit 301efb3

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

pkg/commands/run.go

-4
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,6 @@ func (c *runCommand) persistentPostRunE(_ *cobra.Command, _ []string) error {
185185
}
186186

187187
func (c *runCommand) preRunE(_ *cobra.Command, args []string) error {
188-
if c.cfg.GetConfigDir() != "" && c.cfg.Version != "2" {
189-
return fmt.Errorf("invalid version of the configuration: %q", c.cfg.Version)
190-
}
191-
192188
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), c.cfg,
193189
lintersdb.NewLinterBuilder(), lintersdb.NewPluginModuleBuilder(c.log), lintersdb.NewPluginGoBuilder(c.log))
194190
if err != nil {

pkg/config/loader.go

+14
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func (l *Loader) Load(opts LoadOptions) error {
6060
l.cfg.Linters.Exclusions.Generated = GeneratedModeStrict
6161
}
6262

63+
err = l.checkConfigurationVersion()
64+
if err != nil {
65+
return err
66+
}
67+
6368
if !l.cfg.InternalCmdTest {
6469
for _, n := range slices.Concat(l.cfg.Linters.Enable, l.cfg.Linters.Disable) {
6570
if n == "typecheck" {
@@ -127,6 +132,15 @@ func (l *Loader) appendStringSlice(name string, current *[]string) {
127132
}
128133
}
129134

135+
func (l *Loader) checkConfigurationVersion() error {
136+
if l.cfg.GetConfigDir() != "" && l.cfg.Version != "2" {
137+
return fmt.Errorf("unsupported version of the configuration: %q "+
138+
"See https://golangci-lint.run/product/migration-guide for migration instructions", l.cfg.Version)
139+
}
140+
141+
return nil
142+
}
143+
130144
func (l *Loader) handleGoVersion() {
131145
if l.cfg.Run.Go == "" {
132146
l.cfg.Run.Go = detectGoVersion(context.Background())

test/enabled_linters_test.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ func TestEnabledLinters(t *testing.T) {
2828
{
2929
name: "disable govet in config",
3030
cfg: `
31-
linters:
32-
disable:
33-
- govet
31+
version: "2"
32+
linters:
33+
disable:
34+
- govet
3435
`,
3536
enabledLinters: getEnabledByDefaultLintersExcept(t, "govet"),
3637
},
3738
{
3839
name: "enable revive in config",
3940
cfg: `
40-
linters:
41-
enable:
42-
- revive
41+
version: "2"
42+
linters:
43+
enable:
44+
- revive
4345
`,
4446
enabledLinters: getEnabledByDefaultLintersWith(t, "revive"),
4547
},
@@ -52,17 +54,19 @@ func TestEnabledLinters(t *testing.T) {
5254
name: "enable revive in cmd and enable gofmt in config",
5355
args: []string{"-Erevive"},
5456
cfg: `
55-
formatters:
56-
enable:
57-
- gofmt
57+
version: "2"
58+
formatters:
59+
enable:
60+
- gofmt
5861
`,
5962
enabledLinters: getEnabledByDefaultLintersWith(t, "revive", "gofmt"),
6063
},
6164
{
6265
name: "fast option in config",
6366
cfg: `
64-
linters:
65-
default: fast
67+
version: "2"
68+
linters:
69+
default: fast
6670
`,
6771
enabledLinters: getAllLintersFromGroupFast(t),
6872
},
@@ -74,8 +78,9 @@ func TestEnabledLinters(t *testing.T) {
7478
{
7579
name: "fast option in command-line has higher priority to enable",
7680
cfg: `
77-
linters:
78-
default: none
81+
version: "2"
82+
linters:
83+
default: none
7984
`,
8085
args: []string{"--default=fast"},
8186
enabledLinters: getAllLintersFromGroupFast(t),

0 commit comments

Comments
 (0)