Skip to content

Commit fc3d0f7

Browse files
committed
Speed up linting: use deduplicated packages
Use deduplicated packages for all linters except megacheck. See #585 for details.
1 parent e87a1cf commit fc3d0f7

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

pkg/golinters/megacheck.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ func (m MegacheckMetalinter) isValidChild(name string) bool {
177177
}
178178

179179
func (m megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
180-
issues, err := m.runMegacheck(lintCtx.Packages, lintCtx.Settings().Unused.CheckExported)
180+
// Use OriginalPackages not Packages because `unused` doesn't work properly
181+
// when we deduplicate normal and test packages.
182+
issues, err := m.runMegacheck(lintCtx.OriginalPackages, lintCtx.Settings().Unused.CheckExported)
181183
if err != nil {
182184
return nil, errors.Wrap(err, "failed to run megacheck")
183185
}
@@ -231,7 +233,7 @@ func (m megacheck) runMegacheck(workingPkgs []*packages.Package, checkExportedUn
231233
opts := &lintutil.Options{
232234
// TODO: get current go version, but now it doesn't matter,
233235
// may be needed after next updates of megacheck
234-
GoVersion: 11,
236+
GoVersion: 12,
235237

236238
Config: cfg,
237239
// TODO: support Ignores option

pkg/lint/linter/context.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ import (
1313
)
1414

1515
type Context struct {
16-
Packages []*packages.Package
16+
// Packages are deduplicated (test and normal packages) packages
17+
Packages []*packages.Package
18+
19+
// OriginalPackages aren't deduplicated: they contain both normal and test
20+
// version for each of packages
21+
OriginalPackages []*packages.Package
22+
1723
NotCompilingPackages []*packages.Package
1824

1925
LoaderConfig *loader.Config // deprecated, don't use for new linters

pkg/lint/load.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,12 @@ func (cl ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*li
381381
}
382382

383383
ret := &linter.Context{
384-
Packages: pkgs,
384+
Packages: deduplicatedPkgs,
385+
386+
// At least `unused` linters works properly only on original (not deduplicated) packages,
387+
// see https://github.com/golangci/golangci-lint/pull/585.
388+
OriginalPackages: pkgs,
389+
385390
Program: prog,
386391
SSAProgram: ssaProg,
387392
LoaderConfig: &loader.Config{
@@ -402,6 +407,7 @@ func (cl ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*li
402407
// separateNotCompilingPackages moves not compiling packages into separate slice:
403408
// a lot of linters crash on such packages
404409
func separateNotCompilingPackages(lintCtx *linter.Context) {
410+
// Separate deduplicated packages
405411
goodPkgs := make([]*packages.Package, 0, len(lintCtx.Packages))
406412
for _, pkg := range lintCtx.Packages {
407413
if pkg.IllTyped {
@@ -415,4 +421,13 @@ func separateNotCompilingPackages(lintCtx *linter.Context) {
415421
if len(lintCtx.NotCompilingPackages) != 0 {
416422
lintCtx.Log.Infof("Packages that do not compile: %+v", lintCtx.NotCompilingPackages)
417423
}
424+
425+
// Separate original (not deduplicated) packages
426+
goodOriginalPkgs := make([]*packages.Package, 0, len(lintCtx.OriginalPackages))
427+
for _, pkg := range lintCtx.OriginalPackages {
428+
if !pkg.IllTyped {
429+
goodOriginalPkgs = append(goodOriginalPkgs, pkg)
430+
}
431+
}
432+
lintCtx.OriginalPackages = goodOriginalPkgs
418433
}

0 commit comments

Comments
 (0)