Skip to content

Commit f2487ae

Browse files
committed
fix: improve error stack parsing.
1 parent 3fee285 commit f2487ae

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pkg/packages/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ package packages
22

33
import (
44
"fmt"
5+
"regexp"
56
"strings"
67

78
"golang.org/x/tools/go/packages"
89
)
910

11+
// reFile matches a line who starts with path and position.
12+
// ex: `/example/main.go:11:17: foobar`
13+
var reFile = regexp.MustCompile(`^.+\.go:\d+:\d+: .+`)
14+
1015
func ExtractErrors(pkg *packages.Package) []packages.Error {
1116
errors := extractErrorsImpl(pkg, map[*packages.Package]bool{})
1217
if len(errors) == 0 {
@@ -89,5 +94,9 @@ func stackCrusher(msg string) string {
8994

9095
frag := msg[index+1 : lastIndex]
9196

97+
if !reFile.MatchString(frag) {
98+
return msg
99+
}
100+
92101
return stackCrusher(frag)
93102
}

pkg/packages/util_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ func Test_stackCrusher(t *testing.T) {
2828
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`,
2929
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",
3030
},
31+
{
32+
desc: "stack with message with parenthesis at the end",
33+
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))`,
34+
expected: "/home/username/childapp/vendor/github.com/gotk3/gotk3/gtk/aboutdialog.go:5:8: could not import C (cgo preprocessing failed)",
35+
},
36+
{
37+
desc: "no stack but message with parenthesis at the end",
38+
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)`,
39+
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)",
40+
},
3141
}
3242

3343
for _, test := range testCases {

0 commit comments

Comments
 (0)