Skip to content

Commit 8bea8e7

Browse files
authored
dev: dedicated package about IllTypedError parsing (#4675)
1 parent 54bfac8 commit 8bea8e7

File tree

7 files changed

+35
-30
lines changed

7 files changed

+35
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package goanalysis
1+
package pkgerrors
22

33
import (
44
"errors"
@@ -7,7 +7,6 @@ import (
77
"golang.org/x/tools/go/packages"
88

99
"github.com/golangci/golangci-lint/pkg/lint/linter"
10-
libpackages "github.com/golangci/golangci-lint/pkg/packages"
1110
"github.com/golangci/golangci-lint/pkg/result"
1211
)
1312

@@ -19,7 +18,7 @@ func (e *IllTypedError) Error() string {
1918
return fmt.Sprintf("errors in package: %v", e.Pkg.Errors)
2019
}
2120

22-
func buildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]result.Issue, error) {
21+
func BuildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]result.Issue, error) {
2322
var issues []result.Issue
2423
uniqReportedIssues := map[string]bool{}
2524

@@ -36,17 +35,17 @@ func buildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]resu
3635
continue
3736
}
3837

39-
for _, err := range libpackages.ExtractErrors(ill.Pkg) {
40-
i, perr := parseError(err)
38+
for _, err := range extractErrors(ill.Pkg) {
39+
issue, perr := parseError(err)
4140
if perr != nil { // failed to parse
4241
if uniqReportedIssues[err.Msg] {
4342
continue
4443
}
4544
uniqReportedIssues[err.Msg] = true
4645
lintCtx.Log.Errorf("typechecking error: %s", err.Msg)
4746
} else {
48-
i.Pkg = ill.Pkg // to save to cache later
49-
issues = append(issues, *i)
47+
issue.Pkg = ill.Pkg // to save to cache later
48+
issues = append(issues, *issue)
5049
}
5150
}
5251
}
@@ -57,16 +56,3 @@ func buildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]resu
5756

5857
return issues, nil
5958
}
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-
}

pkg/packages/util.go renamed to pkg/goanalysis/pkgerrors/extract.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package packages
1+
package pkgerrors
22

33
import (
44
"fmt"
@@ -12,7 +12,7 @@ import (
1212
// ex: `/example/main.go:11:17: foobar`
1313
var reFile = regexp.MustCompile(`^.+\.go:\d+:\d+: .+`)
1414

15-
func ExtractErrors(pkg *packages.Package) []packages.Error {
15+
func extractErrors(pkg *packages.Package) []packages.Error {
1616
errors := extractErrorsImpl(pkg, map[*packages.Package]bool{})
1717
if len(errors) == 0 {
1818
return errors
@@ -38,7 +38,7 @@ func ExtractErrors(pkg *packages.Package) []packages.Error {
3838
if len(pkg.GoFiles) != 0 {
3939
// errors were extracted from deps and have at least one file in package
4040
for i := range uniqErrors {
41-
if _, parseErr := ParseErrorPosition(uniqErrors[i].Pos); parseErr == nil {
41+
if _, parseErr := parseErrorPosition(uniqErrors[i].Pos); parseErr == nil {
4242
continue
4343
}
4444

pkg/packages/util_test.go renamed to pkg/goanalysis/pkgerrors/extract_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package packages
1+
package pkgerrors
22

33
import (
44
"testing"

pkg/packages/errors.go renamed to pkg/goanalysis/pkgerrors/parse.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1-
package packages
1+
package pkgerrors
22

33
import (
44
"errors"
55
"fmt"
66
"go/token"
77
"strconv"
88
"strings"
9+
10+
"golang.org/x/tools/go/packages"
11+
12+
"github.com/golangci/golangci-lint/pkg/result"
913
)
1014

11-
func ParseErrorPosition(pos string) (*token.Position, error) {
15+
func parseError(srcErr packages.Error) (*result.Issue, error) {
16+
pos, err := parseErrorPosition(srcErr.Pos)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
return &result.Issue{
22+
Pos: *pos,
23+
Text: srcErr.Msg,
24+
FromLinter: "typecheck",
25+
}, nil
26+
}
27+
28+
func parseErrorPosition(pos string) (*token.Position, error) {
1229
// file:line(<optional>:colon)
1330
parts := strings.Split(pos, ":")
1431
if len(parts) == 1 {

pkg/goanalysis/linter_test.go renamed to pkg/goanalysis/pkgerrors/parse_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package goanalysis
1+
package pkgerrors
22

33
import (
44
"fmt"
@@ -8,7 +8,7 @@ import (
88
"golang.org/x/tools/go/packages"
99
)
1010

11-
func TestParseError(t *testing.T) {
11+
func Test_parseError(t *testing.T) {
1212
cases := []struct {
1313
in, out string
1414
good bool

pkg/goanalysis/runner_action.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/golangci/golangci-lint/internal/errorutil"
1717
"github.com/golangci/golangci-lint/internal/pkgcache"
18+
"github.com/golangci/golangci-lint/pkg/goanalysis/pkgerrors"
1819
)
1920

2021
type actionAllocator struct {
@@ -184,7 +185,7 @@ func (act *action) analyze() {
184185
// It looks like there should be !pass.Analyzer.RunDespiteErrors
185186
// but govet's cgocall crashes on it. Govet itself contains !pass.Analyzer.RunDespiteErrors condition here,
186187
// but it exits before it if packages.Load have failed.
187-
act.err = fmt.Errorf("analysis skipped: %w", &IllTypedError{Pkg: act.pkg})
188+
act.err = fmt.Errorf("analysis skipped: %w", &pkgerrors.IllTypedError{Pkg: act.pkg})
188189
} else {
189190
startedAt = time.Now()
190191
act.result, act.err = pass.Analyzer.Run(pass)

pkg/goanalysis/runners.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"golang.org/x/tools/go/packages"
1414

1515
"github.com/golangci/golangci-lint/internal/pkgcache"
16+
"github.com/golangci/golangci-lint/pkg/goanalysis/pkgerrors"
1617
"github.com/golangci/golangci-lint/pkg/lint/linter"
1718
"github.com/golangci/golangci-lint/pkg/logutils"
1819
"github.com/golangci/golangci-lint/pkg/result"
@@ -74,7 +75,7 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
7475
return retIssues
7576
}
7677

77-
errIssues, err := buildIssuesFromIllTypedError(errs, lintCtx)
78+
errIssues, err := pkgerrors.BuildIssuesFromIllTypedError(errs, lintCtx)
7879
if err != nil {
7980
return nil, err
8081
}

0 commit comments

Comments
 (0)