Skip to content

Commit a57bc83

Browse files
committed
On of cases for #260: fix crash in staticcheck
1. Fix crash if deps of analyzed packages weren't compiled. 2. Print deps typechecking errors 3. Fix all issues filtering because of empty go env GOCACHE for go < 1.10
1 parent 0935ce1 commit a57bc83

File tree

6 files changed

+63
-9
lines changed

6 files changed

+63
-9
lines changed

pkg/golinters/megacheck.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/golangci/golangci-lint/pkg/fsutils"
1919
"github.com/golangci/golangci-lint/pkg/lint/linter"
20+
libpackages "github.com/golangci/golangci-lint/pkg/packages"
2021
"github.com/golangci/golangci-lint/pkg/result"
2122
)
2223

@@ -87,7 +88,7 @@ func (m Megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.I
8788
var errors []packages.Error
8889
for _, p := range lintCtx.NotCompilingPackages {
8990
errPkgs = append(errPkgs, p.String())
90-
errors = append(errors, p.Errors...)
91+
errors = append(errors, libpackages.ExtractErrors(p)...)
9192
}
9293

9394
warnText := fmt.Sprintf("Can't run megacheck because of compilation errors in packages %s",

pkg/golinters/typecheck.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"golang.org/x/tools/go/packages"
1212

1313
"github.com/golangci/golangci-lint/pkg/lint/linter"
14+
libpackages "github.com/golangci/golangci-lint/pkg/packages"
1415
"github.com/golangci/golangci-lint/pkg/result"
1516
)
1617

@@ -59,7 +60,8 @@ func (lint TypeCheck) parseError(srcErr packages.Error) (*result.Issue, error) {
5960
func (lint TypeCheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
6061
var res []result.Issue
6162
for _, pkg := range lintCtx.NotCompilingPackages {
62-
for _, err := range pkg.Errors {
63+
errors := libpackages.ExtractErrors(pkg)
64+
for _, err := range errors {
6365
i, perr := lint.parseError(err)
6466
if perr != nil {
6567
res = append(res, result.Issue{

pkg/lint/load.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/golangci/golangci-lint/pkg/lint/astcache"
2424
"github.com/golangci/golangci-lint/pkg/lint/linter"
2525
"github.com/golangci/golangci-lint/pkg/logutils"
26+
libpackages "github.com/golangci/golangci-lint/pkg/packages"
2627
)
2728

2829
type ContextLoader struct {
@@ -88,7 +89,7 @@ func shouldSkipPkg(pkg *packages.Package) bool {
8889
func (cl ContextLoader) makeFakeLoaderProgram(pkgs []*packages.Package) *loader.Program {
8990
var createdPkgs []*loader.PackageInfo
9091
for _, pkg := range pkgs {
91-
if len(pkg.Errors) != 0 {
92+
if pkg.IllTyped {
9293
// some linters crash on packages with errors,
9394
// skip them and warn about them in another place
9495
continue
@@ -104,7 +105,7 @@ func (cl ContextLoader) makeFakeLoaderProgram(pkgs []*packages.Package) *loader.
104105
allPkgs[pkg.Pkg] = pkg
105106
}
106107
for _, pkg := range pkgs {
107-
if len(pkg.Errors) != 0 {
108+
if pkg.IllTyped {
108109
// some linters crash on packages with errors,
109110
// skip them and warn about them in another place
110111
continue
@@ -346,8 +347,8 @@ func (cl ContextLoader) Load(ctx context.Context, linters []linter.Config) (*lin
346347
saveNotCompilingPackages(ret)
347348
} else {
348349
for _, pkg := range pkgs {
349-
if len(pkg.Errors) != 0 {
350-
cl.log.Infof("Pkg %s errors: %v", pkg.ID, pkg.Errors)
350+
if pkg.IllTyped {
351+
cl.log.Infof("Pkg %s errors: %v", pkg.ID, libpackages.ExtractErrors(pkg))
351352
}
352353
}
353354
}
@@ -360,7 +361,7 @@ func (cl ContextLoader) Load(ctx context.Context, linters []linter.Config) (*lin
360361
// which can work with them.
361362
func saveNotCompilingPackages(lintCtx *linter.Context) {
362363
for _, pkg := range lintCtx.Packages {
363-
if len(pkg.Errors) != 0 {
364+
if pkg.IllTyped {
364365
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, pkg)
365366
}
366367
}

pkg/packages/util.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package packages
2+
3+
import (
4+
"fmt"
5+
6+
"golang.org/x/tools/go/packages"
7+
)
8+
9+
func ExtractErrors(pkg *packages.Package) []packages.Error {
10+
errors := extractErrorsImpl(pkg)
11+
if len(errors) == 0 {
12+
return errors
13+
}
14+
15+
seenErrors := map[string]bool{}
16+
var uniqErrors []packages.Error
17+
for _, err := range errors {
18+
if seenErrors[err.Msg] {
19+
continue
20+
}
21+
seenErrors[err.Msg] = true
22+
uniqErrors = append(uniqErrors, err)
23+
}
24+
25+
if len(pkg.Errors) == 0 && len(pkg.GoFiles) != 0 {
26+
// erorrs were extracted from deps and have at leat one file in package
27+
for i := range uniqErrors {
28+
// change pos to local file to properly process it by processors (properly read line etc)
29+
uniqErrors[i].Msg = fmt.Sprintf("%s: %s", uniqErrors[i].Pos, uniqErrors[i].Msg)
30+
uniqErrors[i].Pos = fmt.Sprintf("%s:1", pkg.GoFiles[0])
31+
}
32+
}
33+
34+
return uniqErrors
35+
}
36+
37+
func extractErrorsImpl(pkg *packages.Package) []packages.Error {
38+
if len(pkg.Errors) != 0 {
39+
return pkg.Errors
40+
}
41+
42+
var errors []packages.Error
43+
for _, iPkg := range pkg.Imports {
44+
iPkgErrors := extractErrorsImpl(iPkg)
45+
if iPkgErrors != nil {
46+
errors = append(errors, iPkgErrors...)
47+
}
48+
}
49+
50+
return errors
51+
}

pkg/result/processors/cgo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (p Cgo) Process(issues []result.Issue) ([]result.Issue, error) {
4242
issueFilePath = absPath
4343
}
4444

45-
if strings.HasPrefix(issueFilePath, p.goCacheDir) {
45+
if p.goCacheDir != "" && strings.HasPrefix(issueFilePath, p.goCacheDir) {
4646
return false, nil
4747
}
4848

pkg/result/processors/skip_dirs.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func NewSkipDirs(patterns []string, log logutils.Log, runArgs []string) (*SkipDi
5252
sortedAbsArgs = append(sortedAbsArgs, absArg)
5353
}
5454
sort.Sort(sortedByLenStrings(sortedAbsArgs))
55-
log.Infof("sorted abs args: %s", sortedAbsArgs)
5655

5756
return &SkipDirs{
5857
patterns: patternsRe,

0 commit comments

Comments
 (0)