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
This commit is contained in:
Denis Isaev 2018-11-07 09:47:54 +03:00 committed by Isaev Denis
parent 0935ce1b8d
commit a57bc83d70
6 changed files with 63 additions and 9 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/golangci/golangci-lint/pkg/fsutils" "github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/lint/linter"
libpackages "github.com/golangci/golangci-lint/pkg/packages"
"github.com/golangci/golangci-lint/pkg/result" "github.com/golangci/golangci-lint/pkg/result"
) )
@ -87,7 +88,7 @@ func (m Megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.I
var errors []packages.Error var errors []packages.Error
for _, p := range lintCtx.NotCompilingPackages { for _, p := range lintCtx.NotCompilingPackages {
errPkgs = append(errPkgs, p.String()) errPkgs = append(errPkgs, p.String())
errors = append(errors, p.Errors...) errors = append(errors, libpackages.ExtractErrors(p)...)
} }
warnText := fmt.Sprintf("Can't run megacheck because of compilation errors in packages %s", warnText := fmt.Sprintf("Can't run megacheck because of compilation errors in packages %s",

View File

@ -11,6 +11,7 @@ import (
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
"github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/lint/linter"
libpackages "github.com/golangci/golangci-lint/pkg/packages"
"github.com/golangci/golangci-lint/pkg/result" "github.com/golangci/golangci-lint/pkg/result"
) )
@ -59,7 +60,8 @@ func (lint TypeCheck) parseError(srcErr packages.Error) (*result.Issue, error) {
func (lint TypeCheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) { func (lint TypeCheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
var res []result.Issue var res []result.Issue
for _, pkg := range lintCtx.NotCompilingPackages { for _, pkg := range lintCtx.NotCompilingPackages {
for _, err := range pkg.Errors { errors := libpackages.ExtractErrors(pkg)
for _, err := range errors {
i, perr := lint.parseError(err) i, perr := lint.parseError(err)
if perr != nil { if perr != nil {
res = append(res, result.Issue{ res = append(res, result.Issue{

View File

@ -23,6 +23,7 @@ import (
"github.com/golangci/golangci-lint/pkg/lint/astcache" "github.com/golangci/golangci-lint/pkg/lint/astcache"
"github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils" "github.com/golangci/golangci-lint/pkg/logutils"
libpackages "github.com/golangci/golangci-lint/pkg/packages"
) )
type ContextLoader struct { type ContextLoader struct {
@ -88,7 +89,7 @@ func shouldSkipPkg(pkg *packages.Package) bool {
func (cl ContextLoader) makeFakeLoaderProgram(pkgs []*packages.Package) *loader.Program { func (cl ContextLoader) makeFakeLoaderProgram(pkgs []*packages.Package) *loader.Program {
var createdPkgs []*loader.PackageInfo var createdPkgs []*loader.PackageInfo
for _, pkg := range pkgs { for _, pkg := range pkgs {
if len(pkg.Errors) != 0 { if pkg.IllTyped {
// some linters crash on packages with errors, // some linters crash on packages with errors,
// skip them and warn about them in another place // skip them and warn about them in another place
continue continue
@ -104,7 +105,7 @@ func (cl ContextLoader) makeFakeLoaderProgram(pkgs []*packages.Package) *loader.
allPkgs[pkg.Pkg] = pkg allPkgs[pkg.Pkg] = pkg
} }
for _, pkg := range pkgs { for _, pkg := range pkgs {
if len(pkg.Errors) != 0 { if pkg.IllTyped {
// some linters crash on packages with errors, // some linters crash on packages with errors,
// skip them and warn about them in another place // skip them and warn about them in another place
continue continue
@ -346,8 +347,8 @@ func (cl ContextLoader) Load(ctx context.Context, linters []linter.Config) (*lin
saveNotCompilingPackages(ret) saveNotCompilingPackages(ret)
} else { } else {
for _, pkg := range pkgs { for _, pkg := range pkgs {
if len(pkg.Errors) != 0 { if pkg.IllTyped {
cl.log.Infof("Pkg %s errors: %v", pkg.ID, pkg.Errors) cl.log.Infof("Pkg %s errors: %v", pkg.ID, libpackages.ExtractErrors(pkg))
} }
} }
} }
@ -360,7 +361,7 @@ func (cl ContextLoader) Load(ctx context.Context, linters []linter.Config) (*lin
// which can work with them. // which can work with them.
func saveNotCompilingPackages(lintCtx *linter.Context) { func saveNotCompilingPackages(lintCtx *linter.Context) {
for _, pkg := range lintCtx.Packages { for _, pkg := range lintCtx.Packages {
if len(pkg.Errors) != 0 { if pkg.IllTyped {
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, pkg) lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, pkg)
} }
} }

51
pkg/packages/util.go Normal file
View File

@ -0,0 +1,51 @@
package packages
import (
"fmt"
"golang.org/x/tools/go/packages"
)
func ExtractErrors(pkg *packages.Package) []packages.Error {
errors := extractErrorsImpl(pkg)
if len(errors) == 0 {
return errors
}
seenErrors := map[string]bool{}
var uniqErrors []packages.Error
for _, err := range errors {
if seenErrors[err.Msg] {
continue
}
seenErrors[err.Msg] = true
uniqErrors = append(uniqErrors, err)
}
if len(pkg.Errors) == 0 && len(pkg.GoFiles) != 0 {
// erorrs were extracted from deps and have at leat one file in package
for i := range uniqErrors {
// change pos to local file to properly process it by processors (properly read line etc)
uniqErrors[i].Msg = fmt.Sprintf("%s: %s", uniqErrors[i].Pos, uniqErrors[i].Msg)
uniqErrors[i].Pos = fmt.Sprintf("%s:1", pkg.GoFiles[0])
}
}
return uniqErrors
}
func extractErrorsImpl(pkg *packages.Package) []packages.Error {
if len(pkg.Errors) != 0 {
return pkg.Errors
}
var errors []packages.Error
for _, iPkg := range pkg.Imports {
iPkgErrors := extractErrorsImpl(iPkg)
if iPkgErrors != nil {
errors = append(errors, iPkgErrors...)
}
}
return errors
}

View File

@ -42,7 +42,7 @@ func (p Cgo) Process(issues []result.Issue) ([]result.Issue, error) {
issueFilePath = absPath issueFilePath = absPath
} }
if strings.HasPrefix(issueFilePath, p.goCacheDir) { if p.goCacheDir != "" && strings.HasPrefix(issueFilePath, p.goCacheDir) {
return false, nil return false, nil
} }

View File

@ -52,7 +52,6 @@ func NewSkipDirs(patterns []string, log logutils.Log, runArgs []string) (*SkipDi
sortedAbsArgs = append(sortedAbsArgs, absArg) sortedAbsArgs = append(sortedAbsArgs, absArg)
} }
sort.Sort(sortedByLenStrings(sortedAbsArgs)) sort.Sort(sortedByLenStrings(sortedAbsArgs))
log.Infof("sorted abs args: %s", sortedAbsArgs)
return &SkipDirs{ return &SkipDirs{
patterns: patternsRe, patterns: patternsRe,