Skip to content

Commit

Permalink
typecheck: improve error stack parsing. (#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 4, 2021
1 parent 3fee285 commit a833cc1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/pr.yml
Expand Up @@ -33,6 +33,9 @@ jobs:
uses: golangci/golangci-lint-action@v2.5.1
with:
version: latest
# skip cache because of flaky behaviors
skip-build-cache: true

tests-on-windows:
needs: golangci-lint # run after golangci-lint action to not produce duplicated errors
runs-on: windows-latest
Expand Down
9 changes: 9 additions & 0 deletions pkg/packages/util.go
Expand Up @@ -2,11 +2,16 @@ package packages

import (
"fmt"
"regexp"
"strings"

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

// reFile matches a line who starts with path and position.
// ex: `/example/main.go:11:17: foobar`
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 +94,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 a833cc1

Please sign in to comment.