Skip to content

Commit 796b4ff

Browse files
committed
Clear part of package fields before analysis
1 parent bb41e54 commit 796b4ff

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

pkg/lint/linter/context.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package linter
22

33
import (
4+
"go/ast"
5+
46
"golang.org/x/tools/go/packages"
57

68
"github.com/golangci/golangci-lint/internal/pkgcache"
@@ -30,3 +32,18 @@ type Context struct {
3032
func (c *Context) Settings() *config.LintersSettings {
3133
return &c.Cfg.LintersSettings
3234
}
35+
36+
func (c *Context) ClearTypesInPackages() {
37+
for _, p := range c.Packages {
38+
clearTypes(p)
39+
}
40+
for _, p := range c.OriginalPackages {
41+
clearTypes(p)
42+
}
43+
}
44+
45+
func clearTypes(p *packages.Package) {
46+
p.Types = nil
47+
p.TypesInfo = nil
48+
p.Syntax = []*ast.File{}
49+
}

pkg/lint/runner.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,14 @@ func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context,
111111
}
112112
}()
113113

114-
// pkgs will get dirty while analyzing, which affects to other linters' result.
115-
// To avoid this issue, we clone the loaded packages rather than directly using them.
116-
oldPackages := lintCtx.Packages
117-
oldOriginalPackages := lintCtx.OriginalPackages
118-
clone := func(pkgs []*gopackages.Package) []*gopackages.Package {
119-
clonedPkgs := make([]*gopackages.Package, len(pkgs))
120-
for i, pkg := range pkgs {
121-
p := *pkg
122-
clonedPkgs[i] = &p
123-
}
124-
return clonedPkgs
125-
}
126-
lintCtx.Packages = clone(lintCtx.Packages)
127-
lintCtx.OriginalPackages = clone(lintCtx.OriginalPackages)
128-
129114
specificLintCtx := *lintCtx
130115
specificLintCtx.Log = r.Log.Child(lc.Name())
131116

117+
// Packages in lintCtx might be dirty due to the last analysis,
118+
// which affects to the next analysis.
119+
// To avoid this issue, we clear type information from the packages.
120+
specificLintCtx.ClearTypesInPackages()
132121
issues, err := lc.Linter.Run(ctx, &specificLintCtx)
133-
134-
lintCtx.Packages = oldPackages
135-
lintCtx.OriginalPackages = oldOriginalPackages
136-
137122
if err != nil {
138123
return nil, err
139124
}

0 commit comments

Comments
 (0)