Skip to content

Speed up linting: use deduplicated packages #667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
6 changes: 4 additions & 2 deletions pkg/golinters/megacheck.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}