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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 0 additions & 5 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)