Skip to content

Commit 9aea4ae

Browse files
authored
typecheck: display compilation errors as report instead of error (#1861)
1 parent ccb5bd0 commit 9aea4ae

18 files changed

+1509
-1423
lines changed

go.mod

+1
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

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/goanalysis/errors.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package goanalysis
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/pkg/errors"
7+
"golang.org/x/tools/go/packages"
8+
9+
"github.com/golangci/golangci-lint/pkg/lint/linter"
10+
libpackages "github.com/golangci/golangci-lint/pkg/packages"
11+
"github.com/golangci/golangci-lint/pkg/result"
12+
)
13+
14+
type IllTypedError struct {
15+
Pkg *packages.Package
16+
}
17+
18+
func (e *IllTypedError) Error() string {
19+
return fmt.Sprintf("errors in package: %v", e.Pkg.Errors)
20+
}
21+
22+
func buildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]result.Issue, error) {
23+
var issues []result.Issue
24+
uniqReportedIssues := map[string]bool{}
25+
26+
var other error
27+
28+
for _, err := range errs {
29+
err := err
30+
31+
var ill *IllTypedError
32+
if !errors.As(err, &ill) {
33+
if other == nil {
34+
other = err
35+
}
36+
continue
37+
}
38+
39+
for _, err := range libpackages.ExtractErrors(ill.Pkg) {
40+
i, perr := parseError(err)
41+
if perr != nil { // failed to parse
42+
if uniqReportedIssues[err.Msg] {
43+
continue
44+
}
45+
uniqReportedIssues[err.Msg] = true
46+
lintCtx.Log.Errorf("typechecking error: %s", err.Msg)
47+
} else {
48+
i.Pkg = ill.Pkg // to save to cache later
49+
issues = append(issues, *i)
50+
}
51+
}
52+
}
53+
54+
if len(issues) == 0 && other != nil {
55+
return nil, other
56+
}
57+
58+
return issues, nil
59+
}
60+
61+
func parseError(srcErr packages.Error) (*result.Issue, error) {
62+
pos, err := libpackages.ParseErrorPosition(srcErr.Pos)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
return &result.Issue{
68+
Pos: *pos,
69+
Text: srcErr.Msg,
70+
FromLinter: "typecheck",
71+
}, nil
72+
}

0 commit comments

Comments
 (0)