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: formatters shound't be enabled/disabled as linters #5516

Merged
merged 2 commits into from
Mar 11, 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
8 changes: 7 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,10 @@ formatters:
goimports:
local-prefixes:
- github.com/golangci/golangci-lint

exclusions:
paths:
- test/testdata_etc # test files
- internal/go # extracted from Go code
- internal/x # extracted from x/tools code
- pkg/goformatters/gci/internal # extracted from gci code
- pkg/goanalysis/runner_checker.go # extracted from x/tools code
3 changes: 2 additions & 1 deletion internal/go/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"strings"
"time"

"github.com/rogpeppe/go-internal/lockedfile"

"github.com/golangci/golangci-lint/internal/go/mmap"
"github.com/golangci/golangci-lint/internal/go/robustio"
"github.com/rogpeppe/go-internal/lockedfile"
)

// An ActionID is a cache action key, the hash of a complete description of a
Expand Down
31 changes: 31 additions & 0 deletions pkg/config/linters.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package config

import (
"fmt"
"slices"
)

const (
GroupStandard = "standard"
GroupAll = "all"
Expand All @@ -21,6 +26,8 @@ type Linters struct {
func (l *Linters) Validate() error {
validators := []func() error{
l.Exclusions.Validate,
l.validateNoFormattersEnabled,
l.validateNoFormattersDisabled,
}

for _, v := range validators {
Expand All @@ -31,3 +38,27 @@ func (l *Linters) Validate() error {

return nil
}

func (l *Linters) validateNoFormattersEnabled() error {
for _, n := range l.Enable {
if slices.Contains(getAllFormatterNames(), n) {
return fmt.Errorf("%s is a formatter", n)
}
}

return nil
}

func (l *Linters) validateNoFormattersDisabled() error {
for _, n := range l.Disable {
if slices.Contains(getAllFormatterNames(), n) {
return fmt.Errorf("%s is a formatter", n)
}
}

return nil
}

func getAllFormatterNames() []string {
return []string{"gci", "gofmt", "gofumpt", "goimports"}
}
8 changes: 6 additions & 2 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,13 @@ func (l *Loader) handleEnableOnlyOption() error {

if len(only) > 0 {
l.cfg.Linters = Linters{
Enable: only,
Default: GroupNone,
Default: GroupNone,
Enable: only,
Settings: l.cfg.Linters.Settings,
Exclusions: l.cfg.Linters.Exclusions,
}

l.cfg.Formatters = Formatters{}
}

return nil
Expand Down
5 changes: 0 additions & 5 deletions pkg/goformatters/gofumpt/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,5 @@ func (f *Formatter) Format(_ string, src []byte) ([]byte, error) {
}

func getLangVersion(v string) string {
if v == "" {
// TODO: defaults to "1.15", in the future (v2) must be removed.
return "go1.15"
}

return "go" + strings.TrimPrefix(v, "go")
}
1 change: 0 additions & 1 deletion pkg/golinters/gci/testdata/fix/in/gci.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egci
//golangcitest:config_path testdata/gci.yml
//golangcitest:expected_exitcode 0
package gci
Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gci/testdata/fix/out/gci.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egci
//golangcitest:config_path testdata/gci.yml
//golangcitest:expected_exitcode 0
package gci
Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gci/testdata/gci.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egci
//golangcitest:config_path testdata/gci.yml
package testdata

Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gci/testdata/gci_cgo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egci
//golangcitest:config_path testdata/gci.yml
package testdata

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gci/testdata/gci_go124.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build go1.24

//golangcitest:args -Egci
//golangcitest:config_path testdata/gci_go124.yml
//golangcitest:expected_exitcode 0
package testdata

Expand Down
5 changes: 5 additions & 0 deletions pkg/golinters/gci/testdata/gci_go124.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "2"

formatters:
enable:
- gci
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt/testdata/fix/in/gofmt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/gofmt.yml
//golangcitest:expected_exitcode 0
package p

Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gofmt/testdata/fix/in/gofmt_rewrite_rules.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/gofmt_rewrite_rules.yml
//golangcitest:expected_exitcode 0
package p
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt/testdata/fix/out/gofmt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/gofmt.yml
//golangcitest:expected_exitcode 0
package p

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/gofmt_rewrite_rules.yml
//golangcitest:expected_exitcode 0
package p
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt/testdata/gofmt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/gofmt.yml
package testdata

import "fmt"
Expand Down
5 changes: 5 additions & 0 deletions pkg/golinters/gofmt/testdata/gofmt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "2"

formatters:
enable:
- gofmt
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt/testdata/gofmt_cgo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/gofmt.yml
package testdata

/*
Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gofmt/testdata/gofmt_no_simplify.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/gofmt_no_simplify.yml
package testdata

Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gofmt/testdata/gofmt_rewrite_rules.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/gofmt_rewrite_rules.yml
package testdata

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt/testdata/gofmt_too_many_empty_lines.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/gofmt.yml
package testdata

import "fmt"
Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gofumpt/testdata/fix/in/gofumpt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt-fix.yml
//golangcitest:expected_exitcode 0
package p
Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gofumpt/testdata/fix/in/gofumpt_cgo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt-fix.yml
//golangcitest:expected_exitcode 0
package p
Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gofumpt/testdata/fix/out/gofumpt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt-fix.yml
//golangcitest:expected_exitcode 0
package p
Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gofumpt/testdata/fix/out/gofumpt_cgo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt-fix.yml
//golangcitest:expected_exitcode 0
package p
Expand Down
4 changes: 2 additions & 2 deletions pkg/golinters/gofumpt/testdata/gofumpt.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt.yml
package testdata

import "fmt"

func GofumptNewLine() {
fmt.Println( "foo" ) // want "File is not properly formatted"
}
}
5 changes: 5 additions & 0 deletions pkg/golinters/gofumpt/testdata/gofumpt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "2"

formatters:
enable:
- gofumpt
2 changes: 1 addition & 1 deletion pkg/golinters/gofumpt/testdata/gofumpt_cgo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt.yml
package testdata

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt.yml
package testdata

import "fmt"
Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gofumpt/testdata/gofumpt_with_extra.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt_with_extra.yml
package testdata

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goimports/testdata/fix/in/goimports.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egoimports
//golangcitest:config_path testdata/goimports.yml
//golangcitest:expected_exitcode 0
package p

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goimports/testdata/fix/in/goimports_cgo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egoimports
//golangcitest:config_path testdata/goimports.yml
//golangcitest:expected_exitcode 0
package p

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goimports/testdata/fix/out/goimports.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egoimports
//golangcitest:config_path testdata/goimports.yml
//golangcitest:expected_exitcode 0
package p

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goimports/testdata/fix/out/goimports_cgo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egoimports
//golangcitest:config_path testdata/goimports.yml
//golangcitest:expected_exitcode 0
package p

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goimports/testdata/goimports.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egoimports
//golangcitest:config_path testdata/goimports.yml
package testdata

import (
Expand Down
5 changes: 5 additions & 0 deletions pkg/golinters/goimports/testdata/goimports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "2"

formatters:
enable:
- goimports
2 changes: 1 addition & 1 deletion pkg/golinters/goimports/testdata/goimports_cgo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egoimports
//golangcitest:config_path testdata/goimports.yml
package testdata

/*
Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/goimports/testdata/goimports_local.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//golangcitest:args -Egoimports
//golangcitest:config_path testdata/goimports_local.yml
package testdata

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/golines/testdata/fix/in/golines.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egolines
//golangcitest:config_path testdata/golines.yml
//golangcitest:expected_exitcode 0
package testdata

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/golines/testdata/fix/out/golines.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egolines
//golangcitest:config_path testdata/golines.yml
//golangcitest:expected_exitcode 0
package testdata

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/golines/testdata/golines.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egolines
//golangcitest:config_path testdata/golines.yml
package testdata

import "fmt"
Expand Down
5 changes: 5 additions & 0 deletions pkg/golinters/golines/testdata/golines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "2"

formatters:
enable:
- golines
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goanalysis"
"github.com/golangci/golangci-lint/pkg/goformatters"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils"
)
Expand Down Expand Up @@ -279,6 +280,10 @@ func linterConfigsToMap(lcs []*linter.Config) map[string]*linter.Config {
continue
}

if goformatters.IsFormatter(lc.Name()) {
continue
}

ret[lc.Name()] = lc
}

Expand Down
8 changes: 4 additions & 4 deletions test/enabled_linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func TestEnabledLinters(t *testing.T) {
enabledLinters: getEnabledByDefaultLintersExcept(t, "govet"),
},
{
name: "enable gofmt in cmd and enable revive in config",
args: []string{"-Egofmt"},
name: "enable revive in cmd and enable gofmt in config",
args: []string{"-Erevive"},
cfg: `
linters:
formatters:
enable:
- revive
- gofmt
`,
enabledLinters: getEnabledByDefaultLintersWith(t, "revive", "gofmt"),
},
Expand Down
24 changes: 0 additions & 24 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@ func TestCgoWithIssues(t *testing.T) {
dir: "cgo_with_issues",
expected: "SA5009: Printf format %t has arg #1 of wrong type",
},
{
desc: "gofmt",
args: []string{"--no-config", "--default=none", "-Egofmt"},
dir: "cgo_with_issues",
expected: "File is not properly formatted (gofmt)",
},
{
desc: "revive",
args: []string{"--no-config", "--default=none", "-Erevive"},
Expand Down Expand Up @@ -206,24 +200,6 @@ func TestLineDirective(t *testing.T) {
targetPath: "linedirective",
expected: "21-23 lines are duplicate of `testdata/linedirective/hello.go:25-27` (dupl)",
},
{
desc: "gofmt",
args: []string{
"-Egofmt",
"--default=none",
},
targetPath: "linedirective",
expected: "File is not properly formatted (gofmt)",
},
{
desc: "goimports",
args: []string{
"-Egoimports",
"--default=none",
},
targetPath: "linedirective",
expected: "File is not properly formatted (goimports)",
},
{
desc: "gomodguard",
args: []string{
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/fix/in/multiple-issues-fix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//golangcitest:args -Egofumpt,misspell
//golangcitest:args -Emisspell
//golangcitest:config_path testdata/configs/multiple-issues-fix.yml
//golangcitest:expected_exitcode 0
package p
Expand Down
Loading
Loading