Skip to content

Commit

Permalink
Speed up linting: use deduplicated packages (#667)
Browse files Browse the repository at this point in the history
Use deduplicated packages for all linters except megacheck.
See #585 for details.
  • Loading branch information
jirfag committed Sep 9, 2019
1 parent e1a7422 commit 375a5a8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
6 changes: 4 additions & 2 deletions pkg/golinters/megacheck.go
Expand Up @@ -177,7 +177,9 @@ func (m MegacheckMetalinter) isValidChild(name string) bool {
}

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

Config: cfg,
// TODO: support Ignores option
Expand Down
8 changes: 7 additions & 1 deletion pkg/lint/linter/context.go
Expand Up @@ -13,7 +13,13 @@ import (
)

type Context struct {
Packages []*packages.Package
// Packages are deduplicated (test and normal packages) packages
Packages []*packages.Package

// OriginalPackages aren't deduplicated: they contain both normal and test
// version for each of packages
OriginalPackages []*packages.Package

NotCompilingPackages []*packages.Package

LoaderConfig *loader.Config // deprecated, don't use for new linters
Expand Down
17 changes: 16 additions & 1 deletion pkg/lint/load.go
Expand Up @@ -381,7 +381,12 @@ func (cl ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*li
}

ret := &linter.Context{
Packages: pkgs,
Packages: deduplicatedPkgs,

// At least `unused` linters works properly only on original (not deduplicated) packages,
// see https://github.com/golangci/golangci-lint/pull/585.
OriginalPackages: pkgs,

Program: prog,
SSAProgram: ssaProg,
LoaderConfig: &loader.Config{
Expand All @@ -402,6 +407,7 @@ func (cl ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*li
// separateNotCompilingPackages moves not compiling packages into separate slice:
// a lot of linters crash on such packages
func separateNotCompilingPackages(lintCtx *linter.Context) {
// Separate deduplicated packages
goodPkgs := make([]*packages.Package, 0, len(lintCtx.Packages))
for _, pkg := range lintCtx.Packages {
if pkg.IllTyped {
Expand All @@ -415,4 +421,13 @@ func separateNotCompilingPackages(lintCtx *linter.Context) {
if len(lintCtx.NotCompilingPackages) != 0 {
lintCtx.Log.Infof("Packages that do not compile: %+v", lintCtx.NotCompilingPackages)
}

// Separate original (not deduplicated) packages
goodOriginalPkgs := make([]*packages.Package, 0, len(lintCtx.OriginalPackages))
for _, pkg := range lintCtx.OriginalPackages {
if !pkg.IllTyped {
goodOriginalPkgs = append(goodOriginalPkgs, pkg)
}
}
lintCtx.OriginalPackages = goodOriginalPkgs
}

0 comments on commit 375a5a8

Please sign in to comment.