Skip to content

Commit

Permalink
fix: improve error stack parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 3, 2021
1 parent 3fee285 commit c2c33dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/packages/util.go
Expand Up @@ -2,11 +2,14 @@ package packages

import (
"fmt"
"regexp"
"strings"

"golang.org/x/tools/go/packages"
)

var reFile = regexp.MustCompile(`^.+\.go:\d+:\d+: .+`)

func ExtractErrors(pkg *packages.Package) []packages.Error {
errors := extractErrorsImpl(pkg, map[*packages.Package]bool{})
if len(errors) == 0 {
Expand Down Expand Up @@ -89,5 +92,9 @@ func stackCrusher(msg string) string {

frag := msg[index+1 : lastIndex]

if !reFile.MatchString(frag) {
return msg
}

return stackCrusher(frag)
}
10 changes: 10 additions & 0 deletions pkg/packages/util_test.go
Expand Up @@ -28,6 +28,16 @@ func Test_stackCrusher(t *testing.T) {
stack: `/home/ldez/sources/go/src/github.com/golangci/golangci-lint/pkg/golinters/deadcode.go:20:32: cannot use mu (variable of type sync.Mutex) as goanalysis.Issue value in argument to append`,
expected: "/home/ldez/sources/go/src/github.com/golangci/golangci-lint/pkg/golinters/deadcode.go:20:32: cannot use mu (variable of type sync.Mutex) as goanalysis.Issue value in argument to append",
},
{
desc: "stack with message with parenthesis at the end",
stack: `/home/username/childapp/interfaces/IPanel.go:4:2: could not import github.com/gotk3/gotk3/gtk (/home/username/childapp/vendor/github.com/gotk3/gotk3/gtk/aboutdialog.go:5:8: could not import C (cgo preprocessing failed))`,
expected: "/home/username/childapp/vendor/github.com/gotk3/gotk3/gtk/aboutdialog.go:5:8: could not import C (cgo preprocessing failed)",
},
{
desc: "no stack but message with parenthesis at the end",
stack: `/home/ldez/sources/go/src/github.com/golangci/sandbox/main.go:11:17: ui.test undefined (type App has no field or method test)`,
expected: "/home/ldez/sources/go/src/github.com/golangci/sandbox/main.go:11:17: ui.test undefined (type App has no field or method test)",
},
}

for _, test := range testCases {
Expand Down

0 comments on commit c2c33dc

Please sign in to comment.