2018-11-07 09:47:54 +03:00
|
|
|
package packages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-04 10:55:39 +02:00
|
|
|
"regexp"
|
2021-03-25 23:52:55 +01:00
|
|
|
"strings"
|
2018-11-07 09:47:54 +03:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages"
|
|
|
|
)
|
|
|
|
|
2021-04-04 10:55:39 +02:00
|
|
|
// reFile matches a line who starts with path and position.
|
|
|
|
// ex: `/example/main.go:11:17: foobar`
|
|
|
|
var reFile = regexp.MustCompile(`^.+\.go:\d+:\d+: .+`)
|
|
|
|
|
2019-10-01 14:52:00 +03:00
|
|
|
func ExtractErrors(pkg *packages.Package) []packages.Error {
|
2019-01-20 21:56:57 +03:00
|
|
|
errors := extractErrorsImpl(pkg, map[*packages.Package]bool{})
|
2018-11-07 09:47:54 +03:00
|
|
|
if len(errors) == 0 {
|
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
|
|
|
seenErrors := map[string]bool{}
|
|
|
|
var uniqErrors []packages.Error
|
|
|
|
for _, err := range errors {
|
2021-03-25 23:52:55 +01:00
|
|
|
msg := stackCrusher(err.Error())
|
|
|
|
if seenErrors[msg] {
|
2018-11-07 09:47:54 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-03-25 23:52:55 +01:00
|
|
|
|
|
|
|
if msg != err.Error() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
seenErrors[msg] = true
|
|
|
|
|
2018-11-07 09:47:54 +03:00
|
|
|
uniqErrors = append(uniqErrors, err)
|
|
|
|
}
|
|
|
|
|
2018-11-23 18:23:24 +03:00
|
|
|
if len(pkg.GoFiles) != 0 {
|
2021-03-25 23:52:55 +01:00
|
|
|
// errors were extracted from deps and have at least one file in package
|
2018-11-07 09:47:54 +03:00
|
|
|
for i := range uniqErrors {
|
2021-03-25 23:52:55 +01:00
|
|
|
if _, parseErr := ParseErrorPosition(uniqErrors[i].Pos); parseErr == nil {
|
|
|
|
continue
|
2018-11-23 18:23:24 +03:00
|
|
|
}
|
2021-03-25 23:52:55 +01:00
|
|
|
|
2021-11-11 06:56:36 +02:00
|
|
|
// change pos to local file to properly process it by processors (properly read line etc.)
|
2021-03-25 23:52:55 +01:00
|
|
|
uniqErrors[i].Msg = fmt.Sprintf("%s: %s", uniqErrors[i].Pos, uniqErrors[i].Msg)
|
|
|
|
uniqErrors[i].Pos = fmt.Sprintf("%s:1", pkg.GoFiles[0])
|
2018-11-07 09:47:54 +03:00
|
|
|
}
|
|
|
|
|
2018-11-23 18:23:24 +03:00
|
|
|
// some errors like "code in directory expects import" don't have Pos, set it here
|
2018-11-18 15:50:15 +03:00
|
|
|
for i := range uniqErrors {
|
|
|
|
err := &uniqErrors[i]
|
|
|
|
if err.Pos == "" {
|
|
|
|
err.Pos = fmt.Sprintf("%s:1", pkg.GoFiles[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 09:47:54 +03:00
|
|
|
return uniqErrors
|
|
|
|
}
|
|
|
|
|
2019-01-20 21:56:57 +03:00
|
|
|
func extractErrorsImpl(pkg *packages.Package, seenPackages map[*packages.Package]bool) []packages.Error {
|
|
|
|
if seenPackages[pkg] {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
seenPackages[pkg] = true
|
|
|
|
|
2021-11-11 06:56:36 +02:00
|
|
|
if !pkg.IllTyped { // otherwise, it may take hours to traverse all deps many times
|
2018-11-10 14:32:26 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:52:55 +01:00
|
|
|
if len(pkg.Errors) > 0 {
|
2018-11-07 09:47:54 +03:00
|
|
|
return pkg.Errors
|
|
|
|
}
|
|
|
|
|
|
|
|
var errors []packages.Error
|
|
|
|
for _, iPkg := range pkg.Imports {
|
2019-01-20 21:56:57 +03:00
|
|
|
iPkgErrors := extractErrorsImpl(iPkg, seenPackages)
|
2018-11-07 09:47:54 +03:00
|
|
|
if iPkgErrors != nil {
|
|
|
|
errors = append(errors, iPkgErrors...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors
|
|
|
|
}
|
2021-03-25 23:52:55 +01:00
|
|
|
|
|
|
|
func stackCrusher(msg string) string {
|
|
|
|
index := strings.Index(msg, "(")
|
|
|
|
lastIndex := strings.LastIndex(msg, ")")
|
|
|
|
|
|
|
|
if index == -1 || index == len(msg)-1 || lastIndex == -1 || lastIndex != len(msg)-1 {
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
frag := msg[index+1 : lastIndex]
|
|
|
|
|
2021-04-04 10:55:39 +02:00
|
|
|
if !reFile.MatchString(frag) {
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:52:55 +01:00
|
|
|
return stackCrusher(frag)
|
|
|
|
}
|