Skip to content

Commit 13fe9f1

Browse files
committed
The ULTIMATE fix
1 parent feb0867 commit 13fe9f1

File tree

6 files changed

+38
-68
lines changed

6 files changed

+38
-68
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ require (
3333
github.com/gordonklaus/ineffassign v0.0.0-20210225214923-2e10b2664254
3434
github.com/gostaticanalysis/forcetypeassert v0.0.0-20200621232751-01d4955beaa5
3535
github.com/gostaticanalysis/nilerr v0.1.1
36+
github.com/hashicorp/go-multierror v1.0.0
3637
github.com/jgautheron/goconst v1.4.0
3738
github.com/jingyugao/rowserrcheck v0.0.0-20210315055705-d907ca737bb1
3839
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/goanalysis/errors.go

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package goanalysis
22

33
import (
44
"fmt"
5-
"strings"
65

76
"github.com/pkg/errors"
87
"golang.org/x/tools/go/packages"
@@ -20,58 +19,24 @@ func (e *IllTypedError) Error() string {
2019
return fmt.Sprintf("errors in package: %v", e.Pkg.Errors)
2120
}
2221

23-
type FailedPrerequisitesError struct {
24-
errors map[string][]string
25-
}
26-
27-
func (f FailedPrerequisitesError) NotEmpty() bool {
28-
return len(f.errors) > 0
29-
}
22+
func buildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]result.Issue, error) {
23+
var issues []result.Issue
24+
uniqReportedIssues := map[string]bool{}
3025

31-
func (f *FailedPrerequisitesError) Consume(name string, err error) {
32-
if f.errors == nil {
33-
f.errors = map[string][]string{}
34-
}
35-
k := fmt.Sprintf("%v", err)
36-
f.errors[k] = append(f.errors[k], name)
37-
}
26+
var other error
3827

39-
func (f FailedPrerequisitesError) Error() string {
40-
var errs []string
41-
for err := range f.errors {
42-
errs = append(errs, err)
43-
}
44-
var groups []groupedPrerequisiteErr
4528
for _, err := range errs {
46-
groups = append(groups, groupedPrerequisiteErr{
47-
err: err,
48-
names: f.errors[err],
49-
})
50-
}
51-
return fmt.Sprintf("failed prerequisites: %s", groups)
52-
}
53-
54-
type groupedPrerequisiteErr struct {
55-
names []string
56-
err string
57-
}
29+
err := err
5830

59-
func (g groupedPrerequisiteErr) String() string {
60-
if len(g.names) == 1 {
61-
return fmt.Sprintf("%s: %s", g.names[0], g.err)
62-
}
63-
return fmt.Sprintf("(%s): %s", strings.Join(g.names, ", "), g.err)
64-
}
65-
66-
func buildIssuesFromErrorsForTypecheckMode(errs []error, lintCtx *linter.Context) ([]result.Issue, error) {
67-
var issues []result.Issue
68-
uniqReportedIssues := map[string]bool{}
69-
for _, err := range errs {
70-
itErr, ok := errors.Cause(err).(*IllTypedError)
71-
if !ok {
72-
return nil, err
31+
var ill *IllTypedError
32+
if !errors.As(err, &ill) {
33+
if other == nil {
34+
other = err
35+
}
36+
continue
7337
}
74-
for _, err := range libpackages.ExtractErrors(itErr.Pkg) {
38+
39+
for _, err := range libpackages.ExtractErrors(ill.Pkg) {
7540
i, perr := parseError(err)
7641
if perr != nil { // failed to parse
7742
if uniqReportedIssues[err.Msg] {
@@ -80,11 +45,16 @@ func buildIssuesFromErrorsForTypecheckMode(errs []error, lintCtx *linter.Context
8045
uniqReportedIssues[err.Msg] = true
8146
lintCtx.Log.Errorf("typechecking error: %s", err.Msg)
8247
} else {
83-
i.Pkg = itErr.Pkg // to save to cache later
48+
i.Pkg = ill.Pkg // to save to cache later
8449
issues = append(issues, *i)
8550
}
8651
}
8752
}
53+
54+
if len(issues) == 0 && other != nil {
55+
return nil, other
56+
}
57+
8858
return issues, nil
8959
}
9060

pkg/golinters/goanalysis/linter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"strings"
88

9-
"github.com/golangci/golangci-lint/pkg/logutils"
109
"github.com/pkg/errors"
1110
"golang.org/x/tools/go/analysis"
1211

pkg/golinters/goanalysis/runner_action.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"runtime/debug"
88
"time"
99

10+
"github.com/hashicorp/go-multierror"
1011
"github.com/pkg/errors"
1112
"golang.org/x/tools/go/analysis"
1213
"golang.org/x/tools/go/packages"
@@ -117,15 +118,20 @@ func (act *action) analyze() {
117118
}(time.Now())
118119

119120
// Report an error if any dependency failures.
120-
var depErr FailedPrerequisitesError
121+
var depErrors *multierror.Error
121122
for _, dep := range act.deps {
122123
if dep.err == nil {
123124
continue
124125
}
125-
depErr.Consume(dep.String(), dep.err)
126+
127+
depErrors = multierror.Append(depErrors, errors.Cause(dep.err))
126128
}
127-
if depErr.NotEmpty() {
128-
act.err = depErr
129+
if depErrors != nil {
130+
depErrors.ErrorFormat = func(e []error) string {
131+
return fmt.Sprintf("failed prerequisites: %v", e)
132+
}
133+
134+
act.err = depErrors
129135
return
130136
}
131137

pkg/golinters/goanalysis/runners.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,16 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
7474
return retIssues
7575
}
7676

77-
if cfg.isTypecheckMode() {
78-
errIssues, err := buildIssuesFromErrorsForTypecheckMode(errs, lintCtx)
79-
if err != nil {
80-
return nil, err
81-
}
82-
83-
issues = append(issues, errIssues...)
84-
issues = append(issues, buildAllIssues()...)
85-
86-
return issues, nil
77+
errIssues, err := buildIssuesFromIllTypedError(errs, lintCtx)
78+
if err != nil {
79+
return nil, err
8780
}
8881

89-
// Don't print all errs: they can duplicate.
90-
if len(errs) != 0 {
91-
return nil, errs[0]
82+
issues = append(issues, errIssues...)
83+
if len(errIssues) == 0 {
84+
issues = append(issues, buildAllIssues()...)
9285
}
9386

94-
issues = append(issues, buildAllIssues()...)
9587
return issues, nil
9688
}
9789

0 commit comments

Comments
 (0)