Clear part of package fields before analysis
This commit is contained in:
parent
bb41e54b04
commit
796b4fffbe
@ -1,6 +1,8 @@
|
|||||||
package linter
|
package linter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"go/ast"
|
||||||
|
|
||||||
"golang.org/x/tools/go/packages"
|
"golang.org/x/tools/go/packages"
|
||||||
|
|
||||||
"github.com/golangci/golangci-lint/internal/pkgcache"
|
"github.com/golangci/golangci-lint/internal/pkgcache"
|
||||||
@ -30,3 +32,18 @@ type Context struct {
|
|||||||
func (c *Context) Settings() *config.LintersSettings {
|
func (c *Context) Settings() *config.LintersSettings {
|
||||||
return &c.Cfg.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{}
|
||||||
|
}
|
||||||
|
@ -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 := *lintCtx
|
||||||
specificLintCtx.Log = r.Log.Child(lc.Name())
|
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)
|
issues, err := lc.Linter.Run(ctx, &specificLintCtx)
|
||||||
|
|
||||||
lintCtx.Packages = oldPackages
|
|
||||||
lintCtx.OriginalPackages = oldOriginalPackages
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user