Speed up linting: use deduplicated packages (#667)

Use deduplicated packages for all linters except megacheck.
See https://github.com/golangci/golangci-lint/pull/585 for details.
This commit is contained in:
Isaev Denis 2019-09-09 16:48:15 +03:00 committed by GitHub
parent e1a7422dd5
commit 375a5a8cae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 4 deletions

View File

@ -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")
}
@ -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

View File

@ -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

View File

@ -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{
@ -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 {
@ -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
}