Skip to content
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
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/enabled_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,14 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)
if !ok {
continue
}

if lnt.LoadMode() == goanalysis.LoadModeWholeProgram {
// It's ineffective by CPU and memory to run whole-program and incremental analyzers at once.
continue
}

goanalysisLinters = append(goanalysisLinters, lnt)

for _, p := range lc.InPresets {
goanalysisPresets[p] = true
}
Expand Down Expand Up @@ -185,6 +188,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)
ml := goanalysis.NewMetaLinter(goanalysisLinters)

presets := maps.Keys(goanalysisPresets)
sort.Strings(presets)

mlConfig := &linter.Config{
Linter: ml,
Expand All @@ -197,6 +201,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)
mlConfig = mlConfig.WithLoadForGoAnalysis()

linters[ml.Name()] = mlConfig

es.debugf("Combined %d go/analysis linters into one metalinter", len(goanalysisLinters))
}

Expand Down
148 changes: 146 additions & 2 deletions pkg/lint/lintersdb/enabled_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,91 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils"
)

func TestGetEnabledLintersSet(t *testing.T) {
type dummyLogger struct{}

func (d dummyLogger) Fatalf(_ string, _ ...any) {}

func (d dummyLogger) Panicf(_ string, _ ...any) {}

func (d dummyLogger) Errorf(_ string, _ ...any) {}

func (d dummyLogger) Warnf(_ string, _ ...any) {}

func (d dummyLogger) Infof(_ string, _ ...any) {}

func (d dummyLogger) Child(_ string) logutils.Log {
return nil
}

func (d dummyLogger) SetLevel(_ logutils.LogLevel) {}

func TestEnabledSet_GetEnabledLintersMap(t *testing.T) {
m := NewManager(nil, nil)

cfg := config.NewDefault()

cfg.Linters.DisableAll = true
cfg.Linters.Enable = []string{"gofmt"}

es := NewEnabledSet(m, NewValidator(m), dummyLogger{}, cfg)

lintersMap, err := es.GetEnabledLintersMap()
require.NoError(t, err)

gofmtConfigs := m.GetLinterConfigs("gofmt")
typecheckConfigs := m.GetLinterConfigs("typecheck")

expected := map[string]*linter.Config{
"gofmt": gofmtConfigs[0],
"typecheck": typecheckConfigs[0],
}

assert.Equal(t, expected, lintersMap)
}

func TestEnabledSet_GetOptimizedLinters(t *testing.T) {
m := NewManager(nil, nil)

cfg := config.NewDefault()

cfg.Linters.DisableAll = true
cfg.Linters.Enable = []string{"gofmt"}

es := NewEnabledSet(m, NewValidator(m), dummyLogger{}, cfg)

optimizedLinters, err := es.GetOptimizedLinters()
require.NoError(t, err)

gofmtConfigs := m.GetLinterConfigs("gofmt")
typecheckConfigs := m.GetLinterConfigs("typecheck")

var gaLinters []*goanalysis.Linter
for _, l := range gofmtConfigs {
gaLinters = append(gaLinters, l.Linter.(*goanalysis.Linter))
}
for _, l := range typecheckConfigs {
gaLinters = append(gaLinters, l.Linter.(*goanalysis.Linter))
}

mlConfig := &linter.Config{
Linter: goanalysis.NewMetaLinter(gaLinters),
InPresets: []string{"bugs", "format"},
}

expected := []*linter.Config{mlConfig.WithLoadForGoAnalysis()}

assert.Equal(t, expected, optimizedLinters)
}

func TestEnabledSet_build(t *testing.T) {
type cs struct {
cfg config.Linters
name string // test case name
Expand Down Expand Up @@ -94,7 +173,7 @@ func TestGetEnabledLintersSet(t *testing.T) {
}

m := NewManager(nil, nil)
es := NewEnabledSet(m, NewValidator(m), nil, nil)
es := NewEnabledSet(m, NewValidator(m), dummyLogger{}, nil)

for _, c := range cases {
c := c
Expand All @@ -117,3 +196,68 @@ func TestGetEnabledLintersSet(t *testing.T) {
})
}
}

func TestEnabledSet_combineGoAnalysisLinters(t *testing.T) {
m := NewManager(nil, nil)

es := NewEnabledSet(m, NewValidator(m), dummyLogger{}, config.NewDefault())

foo := goanalysis.NewLinter("foo", "example foo", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)
bar := goanalysis.NewLinter("bar", "example bar", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)

testCases := []struct {
desc string
linters map[string]*linter.Config
expected map[string]*linter.Config
}{
{
desc: "no combined, one linter",
linters: map[string]*linter.Config{
"foo": {
Linter: foo,
InPresets: []string{"A"},
},
},
expected: map[string]*linter.Config{
"foo": {
Linter: foo,
InPresets: []string{"A"},
},
},
},
{
desc: "combined, several linters",
linters: map[string]*linter.Config{
"foo": {
Linter: foo,
InPresets: []string{"A"},
},
"bar": {
Linter: bar,
InPresets: []string{"B"},
},
},
expected: func() map[string]*linter.Config {
mlConfig := &linter.Config{
Linter: goanalysis.NewMetaLinter([]*goanalysis.Linter{bar, foo}),
InPresets: []string{"A", "B"},
}

return map[string]*linter.Config{
"goanalysis_metalinter": mlConfig.WithLoadForGoAnalysis(),
}
}(),
},
}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

es.combineGoAnalysisLinters(test.linters)

assert.Equal(t, test.expected, test.linters)
})
}
}
Loading