diff --git a/pkg/golinters/megacheck.go b/pkg/golinters/megacheck.go index af5eaf0e5730..d4bf8604181a 100644 --- a/pkg/golinters/megacheck.go +++ b/pkg/golinters/megacheck.go @@ -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 diff --git a/pkg/lint/linter/context.go b/pkg/lint/linter/context.go index 9c3d50401b49..aaf805821d70 100644 --- a/pkg/lint/linter/context.go +++ b/pkg/lint/linter/context.go @@ -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 diff --git a/pkg/lint/load.go b/pkg/lint/load.go index 01c125e7a58d..9a8d0ccb391f 100644 --- a/pkg/lint/load.go +++ b/pkg/lint/load.go @@ -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 }