From a833cc1600456c38357bb6f8abbd6f5aef7dbc51 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Sun, 4 Apr 2021 10:55:39 +0200 Subject: [PATCH] typecheck: improve error stack parsing. (#1886) --- .github/workflows/pr.yml | 3 +++ pkg/packages/util.go | 9 +++++++++ pkg/packages/util_test.go | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index b5380d939817..a5b624ff2bfb 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -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 diff --git a/pkg/packages/util.go b/pkg/packages/util.go index 7fb9fa1ff0b7..e4268897f452 100644 --- a/pkg/packages/util.go +++ b/pkg/packages/util.go @@ -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 { @@ -89,5 +94,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 {