Skip to content

Commit cc5025a

Browse files
committed
fix: improve internal Metalinter build
1 parent 45053ec commit cc5025a

File tree

2 files changed

+103
-20
lines changed

2 files changed

+103
-20
lines changed

pkg/lint/lintersdb/manager.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,30 @@ func (m *Manager) build(enabledByDefaultLinters []*linter.Config) map[string]*li
204204
}
205205

206206
func (m *Manager) combineGoAnalysisLinters(linters map[string]*linter.Config) {
207+
mlConfig := &linter.Config{}
208+
207209
var goanalysisLinters []*goanalysis.Linter
208210
goanalysisPresets := map[string]bool{}
211+
209212
for _, lc := range linters {
210213
lnt, ok := lc.Linter.(*goanalysis.Linter)
211214
if !ok {
212215
continue
213216
}
217+
214218
if lnt.LoadMode() == goanalysis.LoadModeWholeProgram {
215219
// It's ineffective by CPU and memory to run whole-program and incremental analyzers at once.
216220
continue
217221
}
222+
223+
mlConfig.LoadMode |= lc.LoadMode
224+
225+
if lc.IsSlowLinter() {
226+
mlConfig.ConsiderSlow()
227+
}
228+
218229
goanalysisLinters = append(goanalysisLinters, lnt)
230+
219231
for _, p := range lc.InPresets {
220232
goanalysisPresets[p] = true
221233
}
@@ -245,22 +257,14 @@ func (m *Manager) combineGoAnalysisLinters(linters map[string]*linter.Config) {
245257
return a.Name() <= b.Name()
246258
})
247259

248-
ml := goanalysis.NewMetaLinter(goanalysisLinters)
260+
mlConfig.Linter = goanalysis.NewMetaLinter(goanalysisLinters)
249261

250262
presets := maps.Keys(goanalysisPresets)
251263
sort.Strings(presets)
264+
mlConfig.InPresets = presets
252265

253-
mlConfig := &linter.Config{
254-
Linter: ml,
255-
EnabledByDefault: false,
256-
InPresets: presets,
257-
AlternativeNames: nil,
258-
OriginalURL: "",
259-
}
260-
261-
mlConfig = mlConfig.WithLoadForGoAnalysis()
266+
linters[mlConfig.Linter.Name()] = mlConfig
262267

263-
linters[ml.Name()] = mlConfig
264268
m.debugf("Combined %d go/analysis linters into one metalinter", len(goanalysisLinters))
265269
}
266270

pkg/lint/lintersdb/manager_test.go

+88-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/stretchr/testify/assert"
77
"github.com/stretchr/testify/require"
8+
"golang.org/x/tools/go/packages"
89

910
"github.com/golangci/golangci-lint/pkg/config"
1011
"github.com/golangci/golangci-lint/pkg/goanalysis"
@@ -176,8 +177,11 @@ func TestManager_combineGoAnalysisLinters(t *testing.T) {
176177
m, err := NewManager(nil, nil)
177178
require.NoError(t, err)
178179

179-
foo := goanalysis.NewLinter("foo", "example foo", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)
180-
bar := goanalysis.NewLinter("bar", "example bar", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)
180+
fooTyped := goanalysis.NewLinter("foo", "example foo", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)
181+
barTyped := goanalysis.NewLinter("bar", "example bar", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)
182+
183+
fooSyntax := goanalysis.NewLinter("foo", "example foo", nil, nil).WithLoadMode(goanalysis.LoadModeSyntax)
184+
barSyntax := goanalysis.NewLinter("bar", "example bar", nil, nil).WithLoadMode(goanalysis.LoadModeSyntax)
181185

182186
testCases := []struct {
183187
desc string
@@ -188,37 +192,112 @@ func TestManager_combineGoAnalysisLinters(t *testing.T) {
188192
desc: "no combined, one linter",
189193
linters: map[string]*linter.Config{
190194
"foo": {
191-
Linter: foo,
195+
Linter: fooTyped,
192196
InPresets: []string{"A"},
193197
},
194198
},
195199
expected: map[string]*linter.Config{
196200
"foo": {
197-
Linter: foo,
201+
Linter: fooTyped,
202+
InPresets: []string{"A"},
203+
},
204+
},
205+
},
206+
{
207+
desc: "combined, several linters (typed)",
208+
linters: map[string]*linter.Config{
209+
"foo": {
210+
Linter: fooTyped,
198211
InPresets: []string{"A"},
199212
},
213+
"bar": {
214+
Linter: barTyped,
215+
InPresets: []string{"B"},
216+
},
200217
},
218+
expected: func() map[string]*linter.Config {
219+
mlConfig := &linter.Config{
220+
Linter: goanalysis.NewMetaLinter([]*goanalysis.Linter{barTyped, fooTyped}),
221+
InPresets: []string{"A", "B"},
222+
}
223+
224+
return map[string]*linter.Config{
225+
"goanalysis_metalinter": mlConfig,
226+
}
227+
}(),
228+
},
229+
{
230+
desc: "combined, several linters (different LoadMode)",
231+
linters: map[string]*linter.Config{
232+
"foo": {
233+
Linter: fooTyped,
234+
InPresets: []string{"A"},
235+
LoadMode: packages.NeedName,
236+
},
237+
"bar": {
238+
Linter: barTyped,
239+
InPresets: []string{"B"},
240+
LoadMode: packages.NeedTypesSizes,
241+
},
242+
},
243+
expected: func() map[string]*linter.Config {
244+
mlConfig := &linter.Config{
245+
Linter: goanalysis.NewMetaLinter([]*goanalysis.Linter{barTyped, fooTyped}),
246+
InPresets: []string{"A", "B"},
247+
LoadMode: packages.NeedName | packages.NeedTypesSizes,
248+
}
249+
250+
return map[string]*linter.Config{
251+
"goanalysis_metalinter": mlConfig,
252+
}
253+
}(),
254+
},
255+
{
256+
desc: "combined, several linters (same LoadMode)",
257+
linters: map[string]*linter.Config{
258+
"foo": {
259+
Linter: fooTyped,
260+
InPresets: []string{"A"},
261+
LoadMode: packages.NeedName,
262+
},
263+
"bar": {
264+
Linter: barTyped,
265+
InPresets: []string{"B"},
266+
LoadMode: packages.NeedName,
267+
},
268+
},
269+
expected: func() map[string]*linter.Config {
270+
mlConfig := &linter.Config{
271+
Linter: goanalysis.NewMetaLinter([]*goanalysis.Linter{barTyped, fooTyped}),
272+
InPresets: []string{"A", "B"},
273+
LoadMode: packages.NeedName,
274+
}
275+
276+
return map[string]*linter.Config{
277+
"goanalysis_metalinter": mlConfig,
278+
}
279+
}(),
201280
},
202281
{
203-
desc: "combined, several linters",
282+
desc: "combined, several linters (syntax)",
204283
linters: map[string]*linter.Config{
205284
"foo": {
206-
Linter: foo,
285+
Linter: fooSyntax,
207286
InPresets: []string{"A"},
208287
},
209288
"bar": {
210-
Linter: bar,
289+
Linter: barSyntax,
211290
InPresets: []string{"B"},
212291
},
213292
},
214293
expected: func() map[string]*linter.Config {
215294
mlConfig := &linter.Config{
216-
Linter: goanalysis.NewMetaLinter([]*goanalysis.Linter{bar, foo}),
295+
Linter: goanalysis.NewMetaLinter([]*goanalysis.Linter{barSyntax, fooSyntax}),
217296
InPresets: []string{"A", "B"},
218297
}
219298

220299
return map[string]*linter.Config{
221-
"goanalysis_metalinter": mlConfig.WithLoadForGoAnalysis(),
300+
"goanalysis_metalinter": mlConfig,
222301
}
223302
}(),
224303
},

0 commit comments

Comments
 (0)