Skip to content

Commit 1400552

Browse files
authored
fix: formatters shound't be enabled/disabled as linters (#5516)
1 parent fb7cc99 commit 1400552

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+101
-72
lines changed

.golangci.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,10 @@ formatters:
199199
goimports:
200200
local-prefixes:
201201
- github.com/golangci/golangci-lint
202-
202+
exclusions:
203+
paths:
204+
- test/testdata_etc # test files
205+
- internal/go # extracted from Go code
206+
- internal/x # extracted from x/tools code
207+
- pkg/goformatters/gci/internal # extracted from gci code
208+
- pkg/goanalysis/runner_checker.go # extracted from x/tools code

internal/go/cache/cache.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"strings"
2323
"time"
2424

25+
"github.com/rogpeppe/go-internal/lockedfile"
26+
2527
"github.com/golangci/golangci-lint/internal/go/mmap"
2628
"github.com/golangci/golangci-lint/internal/go/robustio"
27-
"github.com/rogpeppe/go-internal/lockedfile"
2829
)
2930

3031
// An ActionID is a cache action key, the hash of a complete description of a

pkg/config/linters.go

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package config
22

3+
import (
4+
"fmt"
5+
"slices"
6+
)
7+
38
const (
49
GroupStandard = "standard"
510
GroupAll = "all"
@@ -21,6 +26,8 @@ type Linters struct {
2126
func (l *Linters) Validate() error {
2227
validators := []func() error{
2328
l.Exclusions.Validate,
29+
l.validateNoFormattersEnabled,
30+
l.validateNoFormattersDisabled,
2431
}
2532

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

3239
return nil
3340
}
41+
42+
func (l *Linters) validateNoFormattersEnabled() error {
43+
for _, n := range l.Enable {
44+
if slices.Contains(getAllFormatterNames(), n) {
45+
return fmt.Errorf("%s is a formatter", n)
46+
}
47+
}
48+
49+
return nil
50+
}
51+
52+
func (l *Linters) validateNoFormattersDisabled() error {
53+
for _, n := range l.Disable {
54+
if slices.Contains(getAllFormatterNames(), n) {
55+
return fmt.Errorf("%s is a formatter", n)
56+
}
57+
}
58+
59+
return nil
60+
}
61+
62+
func getAllFormatterNames() []string {
63+
return []string{"gci", "gofmt", "gofumpt", "goimports"}
64+
}

pkg/config/loader.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,13 @@ func (l *Loader) handleEnableOnlyOption() error {
167167

168168
if len(only) > 0 {
169169
l.cfg.Linters = Linters{
170-
Enable: only,
171-
Default: GroupNone,
170+
Default: GroupNone,
171+
Enable: only,
172+
Settings: l.cfg.Linters.Settings,
173+
Exclusions: l.cfg.Linters.Exclusions,
172174
}
175+
176+
l.cfg.Formatters = Formatters{}
173177
}
174178

175179
return nil

pkg/goformatters/gofumpt/gofumpt.go

-5
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,5 @@ func (f *Formatter) Format(_ string, src []byte) ([]byte, error) {
3737
}
3838

3939
func getLangVersion(v string) string {
40-
if v == "" {
41-
// TODO: defaults to "1.15", in the future (v2) must be removed.
42-
return "go1.15"
43-
}
44-
4540
return "go" + strings.TrimPrefix(v, "go")
4641
}

pkg/golinters/gci/testdata/fix/in/gci.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egci
21
//golangcitest:config_path testdata/gci.yml
32
//golangcitest:expected_exitcode 0
43
package gci

pkg/golinters/gci/testdata/fix/out/gci.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egci
21
//golangcitest:config_path testdata/gci.yml
32
//golangcitest:expected_exitcode 0
43
package gci

pkg/golinters/gci/testdata/gci.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egci
21
//golangcitest:config_path testdata/gci.yml
32
package testdata
43

pkg/golinters/gci/testdata/gci_cgo.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egci
21
//golangcitest:config_path testdata/gci.yml
32
package testdata
43

pkg/golinters/gci/testdata/gci_go124.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//go:build go1.24
22

3-
//golangcitest:args -Egci
3+
//golangcitest:config_path testdata/gci_go124.yml
44
//golangcitest:expected_exitcode 0
55
package testdata
66

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "2"
2+
3+
formatters:
4+
enable:
5+
- gci

pkg/golinters/gofmt/testdata/fix/in/gofmt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egofmt
1+
//golangcitest:config_path testdata/gofmt.yml
22
//golangcitest:expected_exitcode 0
33
package p
44

pkg/golinters/gofmt/testdata/fix/in/gofmt_rewrite_rules.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egofmt
21
//golangcitest:config_path testdata/gofmt_rewrite_rules.yml
32
//golangcitest:expected_exitcode 0
43
package p

pkg/golinters/gofmt/testdata/fix/out/gofmt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egofmt
1+
//golangcitest:config_path testdata/gofmt.yml
22
//golangcitest:expected_exitcode 0
33
package p
44

pkg/golinters/gofmt/testdata/fix/out/gofmt_rewrite_rules.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egofmt
21
//golangcitest:config_path testdata/gofmt_rewrite_rules.yml
32
//golangcitest:expected_exitcode 0
43
package p

pkg/golinters/gofmt/testdata/gofmt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egofmt
1+
//golangcitest:config_path testdata/gofmt.yml
22
package testdata
33

44
import "fmt"
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "2"
2+
3+
formatters:
4+
enable:
5+
- gofmt

pkg/golinters/gofmt/testdata/gofmt_cgo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egofmt
1+
//golangcitest:config_path testdata/gofmt.yml
22
package testdata
33

44
/*

pkg/golinters/gofmt/testdata/gofmt_no_simplify.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egofmt
21
//golangcitest:config_path testdata/gofmt_no_simplify.yml
32
package testdata
43

pkg/golinters/gofmt/testdata/gofmt_rewrite_rules.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egofmt
21
//golangcitest:config_path testdata/gofmt_rewrite_rules.yml
32
package testdata
43

pkg/golinters/gofmt/testdata/gofmt_too_many_empty_lines.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egofmt
1+
//golangcitest:config_path testdata/gofmt.yml
22
package testdata
33

44
import "fmt"

pkg/golinters/gofumpt/testdata/fix/in/gofumpt.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egofumpt
21
//golangcitest:config_path testdata/gofumpt-fix.yml
32
//golangcitest:expected_exitcode 0
43
package p

pkg/golinters/gofumpt/testdata/fix/in/gofumpt_cgo.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egofumpt
21
//golangcitest:config_path testdata/gofumpt-fix.yml
32
//golangcitest:expected_exitcode 0
43
package p

pkg/golinters/gofumpt/testdata/fix/out/gofumpt.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egofumpt
21
//golangcitest:config_path testdata/gofumpt-fix.yml
32
//golangcitest:expected_exitcode 0
43
package p

pkg/golinters/gofumpt/testdata/fix/out/gofumpt_cgo.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egofumpt
21
//golangcitest:config_path testdata/gofumpt-fix.yml
32
//golangcitest:expected_exitcode 0
43
package p
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//golangcitest:args -Egofumpt
1+
//golangcitest:config_path testdata/gofumpt.yml
22
package testdata
33

44
import "fmt"
55

66
func GofumptNewLine() {
77
fmt.Println( "foo" ) // want "File is not properly formatted"
8-
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "2"
2+
3+
formatters:
4+
enable:
5+
- gofumpt

pkg/golinters/gofumpt/testdata/gofumpt_cgo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egofumpt
1+
//golangcitest:config_path testdata/gofumpt.yml
22
package testdata
33

44
/*

pkg/golinters/gofumpt/testdata/gofumpt_too_many_empty_lines.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egofumpt
1+
//golangcitest:config_path testdata/gofumpt.yml
22
package testdata
33

44
import "fmt"

pkg/golinters/gofumpt/testdata/gofumpt_with_extra.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egofumpt
21
//golangcitest:config_path testdata/gofumpt_with_extra.yml
32
package testdata
43

pkg/golinters/goimports/testdata/fix/in/goimports.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egoimports
1+
//golangcitest:config_path testdata/goimports.yml
22
//golangcitest:expected_exitcode 0
33
package p
44

pkg/golinters/goimports/testdata/fix/in/goimports_cgo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egoimports
1+
//golangcitest:config_path testdata/goimports.yml
22
//golangcitest:expected_exitcode 0
33
package p
44

pkg/golinters/goimports/testdata/fix/out/goimports.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egoimports
1+
//golangcitest:config_path testdata/goimports.yml
22
//golangcitest:expected_exitcode 0
33
package p
44

pkg/golinters/goimports/testdata/fix/out/goimports_cgo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egoimports
1+
//golangcitest:config_path testdata/goimports.yml
22
//golangcitest:expected_exitcode 0
33
package p
44

pkg/golinters/goimports/testdata/goimports.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egoimports
1+
//golangcitest:config_path testdata/goimports.yml
22
package testdata
33

44
import (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "2"
2+
3+
formatters:
4+
enable:
5+
- goimports

pkg/golinters/goimports/testdata/goimports_cgo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egoimports
1+
//golangcitest:config_path testdata/goimports.yml
22
package testdata
33

44
/*

pkg/golinters/goimports/testdata/goimports_local.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//golangcitest:args -Egoimports
21
//golangcitest:config_path testdata/goimports_local.yml
32
package testdata
43

pkg/golinters/golines/testdata/fix/in/golines.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egolines
1+
//golangcitest:config_path testdata/golines.yml
22
//golangcitest:expected_exitcode 0
33
package testdata
44

pkg/golinters/golines/testdata/fix/out/golines.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egolines
1+
//golangcitest:config_path testdata/golines.yml
22
//golangcitest:expected_exitcode 0
33
package testdata
44

pkg/golinters/golines/testdata/golines.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egolines
1+
//golangcitest:config_path testdata/golines.yml
22
package testdata
33

44
import "fmt"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "2"
2+
3+
formatters:
4+
enable:
5+
- golines

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/golangci/golangci-lint/pkg/config"
1313
"github.com/golangci/golangci-lint/pkg/goanalysis"
14+
"github.com/golangci/golangci-lint/pkg/goformatters"
1415
"github.com/golangci/golangci-lint/pkg/lint/linter"
1516
"github.com/golangci/golangci-lint/pkg/logutils"
1617
)
@@ -279,6 +280,10 @@ func linterConfigsToMap(lcs []*linter.Config) map[string]*linter.Config {
279280
continue
280281
}
281282

283+
if goformatters.IsFormatter(lc.Name()) {
284+
continue
285+
}
286+
282287
ret[lc.Name()] = lc
283288
}
284289

test/enabled_linters_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ func TestEnabledLinters(t *testing.T) {
4949
enabledLinters: getEnabledByDefaultLintersExcept(t, "govet"),
5050
},
5151
{
52-
name: "enable gofmt in cmd and enable revive in config",
53-
args: []string{"-Egofmt"},
52+
name: "enable revive in cmd and enable gofmt in config",
53+
args: []string{"-Erevive"},
5454
cfg: `
55-
linters:
55+
formatters:
5656
enable:
57-
- revive
57+
- gofmt
5858
`,
5959
enabledLinters: getEnabledByDefaultLintersWith(t, "revive", "gofmt"),
6060
},

test/run_test.go

-24
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,6 @@ func TestCgoWithIssues(t *testing.T) {
156156
dir: "cgo_with_issues",
157157
expected: "SA5009: Printf format %t has arg #1 of wrong type",
158158
},
159-
{
160-
desc: "gofmt",
161-
args: []string{"--no-config", "--default=none", "-Egofmt"},
162-
dir: "cgo_with_issues",
163-
expected: "File is not properly formatted (gofmt)",
164-
},
165159
{
166160
desc: "revive",
167161
args: []string{"--no-config", "--default=none", "-Erevive"},
@@ -206,24 +200,6 @@ func TestLineDirective(t *testing.T) {
206200
targetPath: "linedirective",
207201
expected: "21-23 lines are duplicate of `testdata/linedirective/hello.go:25-27` (dupl)",
208202
},
209-
{
210-
desc: "gofmt",
211-
args: []string{
212-
"-Egofmt",
213-
"--default=none",
214-
},
215-
targetPath: "linedirective",
216-
expected: "File is not properly formatted (gofmt)",
217-
},
218-
{
219-
desc: "goimports",
220-
args: []string{
221-
"-Egoimports",
222-
"--default=none",
223-
},
224-
targetPath: "linedirective",
225-
expected: "File is not properly formatted (goimports)",
226-
},
227203
{
228204
desc: "gomodguard",
229205
args: []string{

test/testdata/fix/in/multiple-issues-fix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//golangcitest:args -Egofumpt,misspell
1+
//golangcitest:args -Emisspell
22
//golangcitest:config_path testdata/configs/multiple-issues-fix.yml
33
//golangcitest:expected_exitcode 0
44
package p

0 commit comments

Comments
 (0)