From c2c33dce089c09a6029dd4b32388df957d30c48f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 27 Mar 2021 19:41:49 +0100 Subject: [PATCH] fix: improve error stack parsing. --- pkg/packages/util.go | 7 +++++++ pkg/packages/util_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/pkg/packages/util.go b/pkg/packages/util.go index 7fb9fa1ff0b7..b00c1fec3ac6 100644 --- a/pkg/packages/util.go +++ b/pkg/packages/util.go @@ -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 { @@ -89,5 +92,9 @@ func stackCrusher(msg string) string { frag := msg[index+1 : lastIndex] + if !reFile.MatchString(frag) { + return msg + } + return stackCrusher(frag) } diff --git a/pkg/packages/util_test.go b/pkg/packages/util_test.go index 47cbcb9440d7..89584b7ff96a 100644 --- a/pkg/packages/util_test.go +++ b/pkg/packages/util_test.go @@ -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 {