Skip to content

Commit 541cd1d

Browse files
author
golangci
authored
Merge pull request #38 from golangci/feature/dont-pass-not-compiling-packages-to-program-linters
#33: don't pass not compiling packages to linters accepting loader.Pr…
2 parents a041498 + b12c559 commit 541cd1d

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

pkg/commands/run.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ func discoverGoRoot() (string, error) {
190190
return strings.TrimSpace(string(output)), nil
191191
}
192192

193+
// separateNotCompilingPackages moves not compiling packages into separate slices:
194+
// a lot of linters crash on such packages. Leave them only for those linters
195+
// which can work with them.
196+
func separateNotCompilingPackages(lintCtx *golinters.Context) {
197+
prog := lintCtx.Program
198+
199+
compilingCreated := make([]*loader.PackageInfo, 0, len(prog.Created))
200+
for _, info := range prog.Created {
201+
if len(info.Errors) != 0 {
202+
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info)
203+
} else {
204+
compilingCreated = append(compilingCreated, info)
205+
}
206+
}
207+
prog.Created = compilingCreated
208+
209+
for k, info := range prog.Imported {
210+
if len(info.Errors) != 0 {
211+
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info)
212+
delete(prog.Imported, k)
213+
}
214+
}
215+
}
216+
193217
func buildLintCtx(ctx context.Context, linters []pkg.Linter, cfg *config.Config) (*golinters.Context, error) {
194218
// Set GOROOT to have working cross-compilation: cross-compiled binaries
195219
// have invalid GOROOT. XXX: can't use runtime.GOROOT().
@@ -228,14 +252,18 @@ func buildLintCtx(ctx context.Context, linters []pkg.Linter, cfg *config.Config)
228252
astCache = astcache.LoadFromFiles(paths.Files)
229253
}
230254

231-
return &golinters.Context{
255+
ret := &golinters.Context{
232256
Paths: paths,
233257
Cfg: cfg,
234258
Program: prog,
235259
SSAProgram: ssaProg,
236260
LoaderConfig: loaderConfig,
237261
ASTCache: astCache,
238-
}, nil
262+
}
263+
264+
separateNotCompilingPackages(ret)
265+
266+
return ret, nil
239267
}
240268

241269
func (e *Executor) runAnalysis(ctx context.Context, args []string) (<-chan result.Issue, error) {

pkg/golinters/context.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
)
1010

1111
type Context struct {
12-
Paths *fsutils.ProjectPaths
13-
Cfg *config.Config
14-
Program *loader.Program
15-
SSAProgram *ssa.Program
16-
LoaderConfig *loader.Config
17-
ASTCache *astcache.Cache
12+
Paths *fsutils.ProjectPaths
13+
Cfg *config.Config
14+
Program *loader.Program
15+
SSAProgram *ssa.Program
16+
LoaderConfig *loader.Config
17+
ASTCache *astcache.Cache
18+
NotCompilingPackages []*loader.PackageInfo
1819
}
1920

2021
func (c *Context) Settings() *config.LintersSettings {

pkg/golinters/typecheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (lint TypeCheck) parseError(err error) *result.Issue {
6363

6464
func (lint TypeCheck) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
6565
var res []result.Issue
66-
for _, pkg := range lintCtx.Program.InitialPackages() {
66+
for _, pkg := range lintCtx.NotCompilingPackages {
6767
for _, err := range pkg.Errors {
6868
i := lint.parseError(err)
6969
if i != nil {

0 commit comments

Comments
 (0)