diff --git a/pkg/lint/linter/context.go b/pkg/lint/linter/context.go index 1c4902e1889b..a9f9d7d7f2b3 100644 --- a/pkg/lint/linter/context.go +++ b/pkg/lint/linter/context.go @@ -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{} +} diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index 56b02ccef7ab..4dc40f3fe04d 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -114,6 +114,10 @@ func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context, 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) if err != nil { return nil, err