Denis Isaev a57bc83d70 On of cases for : 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
2018-11-07 10:06:55 +03:00

52 lines
1.1 KiB
Go

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
}