Skip to content

dev: dedicated package about IllTypedError parsing #4675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions pkg/goanalysis/errors.go → pkg/goanalysis/pkgerrors/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goanalysis
package pkgerrors

import (
"errors"
Expand All @@ -7,7 +7,6 @@ import (
"golang.org/x/tools/go/packages"

"github.com/golangci/golangci-lint/pkg/lint/linter"
libpackages "github.com/golangci/golangci-lint/pkg/packages"
"github.com/golangci/golangci-lint/pkg/result"
)

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

func buildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]result.Issue, error) {
func BuildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]result.Issue, error) {
var issues []result.Issue
uniqReportedIssues := map[string]bool{}

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

for _, err := range libpackages.ExtractErrors(ill.Pkg) {
i, perr := parseError(err)
for _, err := range extractErrors(ill.Pkg) {
issue, perr := parseError(err)
if perr != nil { // failed to parse
if uniqReportedIssues[err.Msg] {
continue
}
uniqReportedIssues[err.Msg] = true
lintCtx.Log.Errorf("typechecking error: %s", err.Msg)
} else {
i.Pkg = ill.Pkg // to save to cache later
issues = append(issues, *i)
issue.Pkg = ill.Pkg // to save to cache later
issues = append(issues, *issue)
}
}
}
Expand All @@ -57,16 +56,3 @@ func buildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]resu

return issues, nil
}

func parseError(srcErr packages.Error) (*result.Issue, error) {
pos, err := libpackages.ParseErrorPosition(srcErr.Pos)
if err != nil {
return nil, err
}

return &result.Issue{
Pos: *pos,
Text: srcErr.Msg,
FromLinter: "typecheck",
}, nil
}
6 changes: 3 additions & 3 deletions pkg/packages/util.go → pkg/goanalysis/pkgerrors/extract.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package packages
package pkgerrors

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package packages
package pkgerrors

import (
"testing"
Expand Down
21 changes: 19 additions & 2 deletions pkg/packages/errors.go → pkg/goanalysis/pkgerrors/parse.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package packages
package pkgerrors

import (
"errors"
"fmt"
"go/token"
"strconv"
"strings"

"golang.org/x/tools/go/packages"

"github.com/golangci/golangci-lint/pkg/result"
)

func ParseErrorPosition(pos string) (*token.Position, error) {
func parseError(srcErr packages.Error) (*result.Issue, error) {
pos, err := parseErrorPosition(srcErr.Pos)
if err != nil {
return nil, err
}

return &result.Issue{
Pos: *pos,
Text: srcErr.Msg,
FromLinter: "typecheck",
}, nil
}

func parseErrorPosition(pos string) (*token.Position, error) {
// file:line(<optional>:colon)
parts := strings.Split(pos, ":")
if len(parts) == 1 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goanalysis
package pkgerrors

import (
"fmt"
Expand All @@ -8,7 +8,7 @@ import (
"golang.org/x/tools/go/packages"
)

func TestParseError(t *testing.T) {
func Test_parseError(t *testing.T) {
cases := []struct {
in, out string
good bool
Expand Down
3 changes: 2 additions & 1 deletion pkg/goanalysis/runner_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/golangci/golangci-lint/internal/errorutil"
"github.com/golangci/golangci-lint/internal/pkgcache"
"github.com/golangci/golangci-lint/pkg/goanalysis/pkgerrors"
)

type actionAllocator struct {
Expand Down Expand Up @@ -184,7 +185,7 @@ func (act *action) analyze() {
// It looks like there should be !pass.Analyzer.RunDespiteErrors
// but govet's cgocall crashes on it. Govet itself contains !pass.Analyzer.RunDespiteErrors condition here,
// but it exits before it if packages.Load have failed.
act.err = fmt.Errorf("analysis skipped: %w", &IllTypedError{Pkg: act.pkg})
act.err = fmt.Errorf("analysis skipped: %w", &pkgerrors.IllTypedError{Pkg: act.pkg})
} else {
startedAt = time.Now()
act.result, act.err = pass.Analyzer.Run(pass)
Expand Down
3 changes: 2 additions & 1 deletion pkg/goanalysis/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/tools/go/packages"

"github.com/golangci/golangci-lint/internal/pkgcache"
"github.com/golangci/golangci-lint/pkg/goanalysis/pkgerrors"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
Expand Down Expand Up @@ -74,7 +75,7 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
return retIssues
}

errIssues, err := buildIssuesFromIllTypedError(errs, lintCtx)
errIssues, err := pkgerrors.BuildIssuesFromIllTypedError(errs, lintCtx)
if err != nil {
return nil, err
}
Expand Down
Loading