Skip to content

Commit 018445b

Browse files
committed
feat: stack crucher
1 parent e9a5d50 commit 018445b

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed

pkg/golinters/goanalysis/runner_action.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,19 @@ func (act *action) analyze() {
177177
act.r.passToPkg[pass] = act.pkg
178178
act.r.passToPkgGuard.Unlock()
179179

180-
var err error
181180
if act.pkg.IllTyped {
182181
// It looks like there should be !pass.Analyzer.RunDespiteErrors
183182
// but govet's cgocall crashes on it. Govet itself contains !pass.Analyzer.RunDespiteErrors condition here
184183
// but it exit before it if packages.Load have failed.
185-
err = errors.Wrap(&IllTypedError{Pkg: act.pkg}, "analysis skipped")
184+
act.err = errors.Wrap(&IllTypedError{Pkg: act.pkg}, "analysis skipped")
186185
} else {
187186
startedAt = time.Now()
188-
act.result, err = pass.Analyzer.Run(pass)
187+
act.result, act.err = pass.Analyzer.Run(pass)
189188
analyzedIn := time.Since(startedAt)
190189
if analyzedIn > time.Millisecond*10 {
191190
debugf("%s: run analyzer in %s", act, analyzedIn)
192191
}
193192
}
194-
act.err = err
195193

196194
// disallow calls after Run
197195
pass.ExportObjectFact = nil

pkg/golinters/goanalysis/runners.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
7979
}
8080

8181
issues = append(issues, errIssues...)
82-
if len(errIssues) == 0 {
83-
issues = append(issues, buildAllIssues()...)
84-
}
82+
issues = append(issues, buildAllIssues()...)
8583

8684
return issues, nil
8785
}

pkg/packages/util.go

+31-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package packages
22

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

67
"golang.org/x/tools/go/packages"
78
)
@@ -15,22 +16,30 @@ func ExtractErrors(pkg *packages.Package) []packages.Error {
1516
seenErrors := map[string]bool{}
1617
var uniqErrors []packages.Error
1718
for _, err := range errors {
18-
if seenErrors[err.Msg] {
19+
msg := stackCrusher(err.Error())
20+
if seenErrors[msg] {
1921
continue
2022
}
21-
seenErrors[err.Msg] = true
23+
24+
if msg != err.Error() {
25+
continue
26+
}
27+
28+
seenErrors[msg] = true
29+
2230
uniqErrors = append(uniqErrors, err)
2331
}
2432

2533
if len(pkg.GoFiles) != 0 {
26-
// errors were extracted from deps and have at leat one file in package
34+
// errors were extracted from deps and have at least one file in package
2735
for i := range uniqErrors {
28-
_, parseErr := ParseErrorPosition(uniqErrors[i].Pos)
29-
if parseErr != nil {
30-
// change pos to local file to properly process it by processors (properly read line etc)
31-
uniqErrors[i].Msg = fmt.Sprintf("%s: %s", uniqErrors[i].Pos, uniqErrors[i].Msg)
32-
uniqErrors[i].Pos = fmt.Sprintf("%s:1", pkg.GoFiles[0])
36+
if _, parseErr := ParseErrorPosition(uniqErrors[i].Pos); parseErr == nil {
37+
continue
3338
}
39+
40+
// change pos to local file to properly process it by processors (properly read line etc)
41+
uniqErrors[i].Msg = fmt.Sprintf("%s: %s", uniqErrors[i].Pos, uniqErrors[i].Msg)
42+
uniqErrors[i].Pos = fmt.Sprintf("%s:1", pkg.GoFiles[0])
3443
}
3544

3645
// some errors like "code in directory expects import" don't have Pos, set it here
@@ -55,7 +64,7 @@ func extractErrorsImpl(pkg *packages.Package, seenPackages map[*packages.Package
5564
return nil
5665
}
5766

58-
if len(pkg.Errors) != 0 {
67+
if len(pkg.Errors) > 0 {
5968
return pkg.Errors
6069
}
6170

@@ -69,3 +78,16 @@ func extractErrorsImpl(pkg *packages.Package, seenPackages map[*packages.Package
6978

7079
return errors
7180
}
81+
82+
func stackCrusher(msg string) string {
83+
index := strings.Index(msg, "(")
84+
lastIndex := strings.LastIndex(msg, ")")
85+
86+
if index == -1 || index == len(msg)-1 || lastIndex == -1 || lastIndex != len(msg)-1 {
87+
return msg
88+
}
89+
90+
frag := msg[index+1 : lastIndex]
91+
92+
return stackCrusher(frag)
93+
}

pkg/packages/util_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package packages
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
//nolint:lll
10+
func Test_stackCrusher(t *testing.T) {
11+
testCases := []struct {
12+
desc string
13+
stack string
14+
expected string
15+
}{
16+
{
17+
desc: "large stack",
18+
stack: `/home/ldez/sources/go/src/github.com/golangci/golangci-lint/pkg/result/processors/nolint.go:13:2: /home/ldez/sources/go/src/github.com/golangci/golangci-lint/pkg/result/processors/nolint.go:13:2: could not import github.com/golangci/golangci-lint/pkg/lint/lintersdb (/home/ldez/sources/go/src/github.com/golangci/golangci-lint/pkg/lint/lintersdb/manager.go:13:2: could not import github.com/golangci/golangci-lint/pkg/golinters (/home/ldez/sources/go/src/github.com/golangci/golangci-lint/pkg/golinters/deadcode.go:21:9: undeclared name: linterName))`,
19+
expected: "/home/ldez/sources/go/src/github.com/golangci/golangci-lint/pkg/golinters/deadcode.go:21:9: undeclared name: linterName",
20+
},
21+
{
22+
desc: "no stack",
23+
stack: `/home/ldez/sources/go/src/github.com/golangci/golangci-lint/pkg/golinters/deadcode.go:45:3: undeclared name: linterName`,
24+
expected: "/home/ldez/sources/go/src/github.com/golangci/golangci-lint/pkg/golinters/deadcode.go:45:3: undeclared name: linterName",
25+
},
26+
{
27+
desc: "no stack but message with parenthesis",
28+
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`,
29+
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",
30+
},
31+
}
32+
33+
for _, test := range testCases {
34+
test := test
35+
t.Run(test.desc, func(t *testing.T) {
36+
t.Parallel()
37+
38+
actual := stackCrusher(test.stack)
39+
40+
assert.Equal(t, test.expected, actual)
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)