Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validate version before configuration #5599

Merged
merged 5 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ func (c *runCommand) persistentPostRunE(_ *cobra.Command, _ []string) error {
}

func (c *runCommand) preRunE(_ *cobra.Command, args []string) error {
if c.cfg.GetConfigDir() != "" && c.cfg.Version != "2" {
return fmt.Errorf("invalid version of the configuration: %q", c.cfg.Version)
}

dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), c.cfg,
lintersdb.NewLinterBuilder(), lintersdb.NewPluginModuleBuilder(c.log), lintersdb.NewPluginGoBuilder(c.log))
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func (l *Loader) Load(opts LoadOptions) error {
l.cfg.Linters.Exclusions.Generated = GeneratedModeStrict
}

err = l.checkConfigurationVersion()
if err != nil {
return err
}

if !l.cfg.InternalCmdTest {
for _, n := range slices.Concat(l.cfg.Linters.Enable, l.cfg.Linters.Disable) {
if n == "typecheck" {
Expand Down Expand Up @@ -127,6 +132,15 @@ func (l *Loader) appendStringSlice(name string, current *[]string) {
}
}

func (l *Loader) checkConfigurationVersion() error {
if l.cfg.GetConfigDir() != "" && l.cfg.Version != "2" {
return fmt.Errorf("unsupported version of the configuration: %q "+
"See https://golangci-lint.run/product/migration-guide for migration instructions", l.cfg.Version)
}

return nil
}

func (l *Loader) handleGoVersion() {
if l.cfg.Run.Go == "" {
l.cfg.Run.Go = detectGoVersion(context.Background())
Expand Down
31 changes: 18 additions & 13 deletions test/enabled_linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ func TestEnabledLinters(t *testing.T) {
{
name: "disable govet in config",
cfg: `
linters:
disable:
- govet
version: "2"
linters:
disable:
- govet
`,
enabledLinters: getEnabledByDefaultLintersExcept(t, "govet"),
},
{
name: "enable revive in config",
cfg: `
linters:
enable:
- revive
version: "2"
linters:
enable:
- revive
`,
enabledLinters: getEnabledByDefaultLintersWith(t, "revive"),
},
Expand All @@ -52,17 +54,19 @@ func TestEnabledLinters(t *testing.T) {
name: "enable revive in cmd and enable gofmt in config",
args: []string{"-Erevive"},
cfg: `
formatters:
enable:
- gofmt
version: "2"
formatters:
enable:
- gofmt
`,
enabledLinters: getEnabledByDefaultLintersWith(t, "revive", "gofmt"),
},
{
name: "fast option in config",
cfg: `
linters:
default: fast
version: "2"
linters:
default: fast
`,
enabledLinters: getAllLintersFromGroupFast(t),
},
Expand All @@ -74,8 +78,9 @@ func TestEnabledLinters(t *testing.T) {
{
name: "fast option in command-line has higher priority to enable",
cfg: `
linters:
default: none
version: "2"
linters:
default: none
`,
args: []string{"--default=fast"},
enabledLinters: getAllLintersFromGroupFast(t),
Expand Down
Loading