Clear part of package fields before analysis

This commit is contained in:
Soichiro Kashima 2020-01-28 16:04:43 +09:00
parent bb41e54b04
commit 796b4fffbe
2 changed files with 21 additions and 19 deletions

View File

@ -1,6 +1,8 @@
package linter
import (
"go/ast"
"golang.org/x/tools/go/packages"
"github.com/golangci/golangci-lint/internal/pkgcache"
@ -30,3 +32,18 @@ type Context struct {
func (c *Context) Settings() *config.LintersSettings {
return &c.Cfg.LintersSettings
}
func (c *Context) ClearTypesInPackages() {
for _, p := range c.Packages {
clearTypes(p)
}
for _, p := range c.OriginalPackages {
clearTypes(p)
}
}
func clearTypes(p *packages.Package) {
p.Types = nil
p.TypesInfo = nil
p.Syntax = []*ast.File{}
}

View File

@ -111,29 +111,14 @@ func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context,
}
}()
// pkgs will get dirty while analyzing, which affects to other linters' result.
// To avoid this issue, we clone the loaded packages rather than directly using them.
oldPackages := lintCtx.Packages
oldOriginalPackages := lintCtx.OriginalPackages
clone := func(pkgs []*gopackages.Package) []*gopackages.Package {
clonedPkgs := make([]*gopackages.Package, len(pkgs))
for i, pkg := range pkgs {
p := *pkg
clonedPkgs[i] = &p
}
return clonedPkgs
}
lintCtx.Packages = clone(lintCtx.Packages)
lintCtx.OriginalPackages = clone(lintCtx.OriginalPackages)
specificLintCtx := *lintCtx
specificLintCtx.Log = r.Log.Child(lc.Name())
// Packages in lintCtx might be dirty due to the last analysis,
// which affects to the next analysis.
// To avoid this issue, we clear type information from the packages.
specificLintCtx.ClearTypesInPackages()
issues, err := lc.Linter.Run(ctx, &specificLintCtx)
lintCtx.Packages = oldPackages
lintCtx.OriginalPackages = oldOriginalPackages
if err != nil {
return nil, err
}