#156, #157: properly detect type errors in dependencies

This commit is contained in:
Denis Isaev 2018-07-28 23:32:29 +03:00 committed by Isaev Denis
parent e17b9543e7
commit 5d04557a1f
4 changed files with 39 additions and 17 deletions

View File

@ -60,7 +60,7 @@ func (m Megacheck) Desc() string {
} }
func prettifyCompilationError(err error) error { func prettifyCompilationError(err error) error {
i := TypeCheck{}.parseError(err) i, _ := TypeCheck{}.parseError(err)
if i == nil { if i == nil {
return err return err
} }

View File

@ -2,6 +2,8 @@ package golinters
import ( import (
"context" "context"
"errors"
"fmt"
"go/token" "go/token"
"strconv" "strconv"
"strings" "strings"
@ -20,17 +22,19 @@ func (TypeCheck) Desc() string {
return "Like the front-end of a Go compiler, parses and type-checks Go code" return "Like the front-end of a Go compiler, parses and type-checks Go code"
} }
func (lint TypeCheck) parseError(err error) *result.Issue { func (lint TypeCheck) parseError(srcErr error) (*result.Issue, error) {
// TODO: cast srcErr to types.Error and just use it
// file:line(<optional>:colon): message // file:line(<optional>:colon): message
parts := strings.Split(err.Error(), ":") parts := strings.Split(srcErr.Error(), ":")
if len(parts) < 3 { if len(parts) < 3 {
return nil return nil, errors.New("too few colons")
} }
file := parts[0] file := parts[0]
line, err := strconv.Atoi(parts[1]) line, err := strconv.Atoi(parts[1])
if err != nil { if err != nil {
return nil return nil, fmt.Errorf("can't parse line number %q: %s", parts[1], err)
} }
var column int var column int
@ -48,7 +52,7 @@ func (lint TypeCheck) parseError(err error) *result.Issue {
message = strings.TrimSpace(message) message = strings.TrimSpace(message)
if message == "" { if message == "" {
return nil return nil, fmt.Errorf("empty message")
} }
return &result.Issue{ return &result.Issue{
@ -59,19 +63,17 @@ func (lint TypeCheck) parseError(err error) *result.Issue {
}, },
Text: message, Text: message,
FromLinter: lint.Name(), FromLinter: lint.Name(),
} }, nil
} }
func (lint TypeCheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) { func (lint TypeCheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
if lintCtx.NotCompilingPackages == nil {
return nil, nil
}
var res []result.Issue var res []result.Issue
for _, pkg := range lintCtx.NotCompilingPackages { for _, pkg := range lintCtx.NotCompilingPackages {
for _, err := range pkg.Errors { for _, err := range pkg.Errors {
i := lint.parseError(err) i, perr := lint.parseError(err)
if i != nil { if perr != nil {
lintCtx.Log.Warnf("Can't parse type error %s: %s", err, perr)
} else {
res = append(res, *i) res = append(res, *i)
} }
} }

View File

@ -35,7 +35,7 @@ func TestParseError(t *testing.T) {
lint := TypeCheck{} lint := TypeCheck{}
for _, c := range cases { for _, c := range cases {
i := lint.parseError(errors.New(c.in)) i, _ := lint.parseError(errors.New(c.in))
if !c.good { if !c.good {
assert.Nil(t, i) assert.Nil(t, i)
continue continue

View File

@ -231,11 +231,14 @@ func buildSSAProgram(lprog *loader.Program, log logutils.Log) *ssa.Program {
func separateNotCompilingPackages(lintCtx *linter.Context) { func separateNotCompilingPackages(lintCtx *linter.Context) {
prog := lintCtx.Program prog := lintCtx.Program
notCompilingPackagesSet := map[*loader.PackageInfo]bool{}
if prog.Created != nil { if prog.Created != nil {
compilingCreated := make([]*loader.PackageInfo, 0, len(prog.Created)) compilingCreated := make([]*loader.PackageInfo, 0, len(prog.Created))
for _, info := range prog.Created { for _, info := range prog.Created {
if len(info.Errors) != 0 { if len(info.Errors) != 0 {
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info) lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info)
notCompilingPackagesSet[info] = true
} else { } else {
compilingCreated = append(compilingCreated, info) compilingCreated = append(compilingCreated, info)
} }
@ -245,11 +248,28 @@ func separateNotCompilingPackages(lintCtx *linter.Context) {
if prog.Imported != nil { if prog.Imported != nil {
for k, info := range prog.Imported { for k, info := range prog.Imported {
if len(info.Errors) != 0 { if len(info.Errors) == 0 {
continue
}
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info) lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info)
notCompilingPackagesSet[info] = true
delete(prog.Imported, k) delete(prog.Imported, k)
} }
} }
if prog.AllPackages != nil {
for k, info := range prog.AllPackages {
if len(info.Errors) == 0 {
continue
}
if !notCompilingPackagesSet[info] {
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info)
notCompilingPackagesSet[info] = true
}
delete(prog.AllPackages, k)
}
} }
if len(lintCtx.NotCompilingPackages) != 0 { if len(lintCtx.NotCompilingPackages) != 0 {