#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 {
i := TypeCheck{}.parseError(err)
i, _ := TypeCheck{}.parseError(err)
if i == nil {
return err
}

View File

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

View File

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

View File

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