Skip to content

Commit 5d04557

Browse files
committed
#156, #157: properly detect type errors in dependencies
1 parent e17b954 commit 5d04557

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

pkg/golinters/megacheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (m Megacheck) Desc() string {
6060
}
6161

6262
func prettifyCompilationError(err error) error {
63-
i := TypeCheck{}.parseError(err)
63+
i, _ := TypeCheck{}.parseError(err)
6464
if i == nil {
6565
return err
6666
}

pkg/golinters/typecheck.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package golinters
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57
"go/token"
68
"strconv"
79
"strings"
@@ -20,17 +22,19 @@ func (TypeCheck) Desc() string {
2022
return "Like the front-end of a Go compiler, parses and type-checks Go code"
2123
}
2224

23-
func (lint TypeCheck) parseError(err error) *result.Issue {
25+
func (lint TypeCheck) parseError(srcErr error) (*result.Issue, error) {
26+
// TODO: cast srcErr to types.Error and just use it
27+
2428
// file:line(<optional>:colon): message
25-
parts := strings.Split(err.Error(), ":")
29+
parts := strings.Split(srcErr.Error(), ":")
2630
if len(parts) < 3 {
27-
return nil
31+
return nil, errors.New("too few colons")
2832
}
2933

3034
file := parts[0]
3135
line, err := strconv.Atoi(parts[1])
3236
if err != nil {
33-
return nil
37+
return nil, fmt.Errorf("can't parse line number %q: %s", parts[1], err)
3438
}
3539

3640
var column int
@@ -48,7 +52,7 @@ func (lint TypeCheck) parseError(err error) *result.Issue {
4852

4953
message = strings.TrimSpace(message)
5054
if message == "" {
51-
return nil
55+
return nil, fmt.Errorf("empty message")
5256
}
5357

5458
return &result.Issue{
@@ -59,19 +63,17 @@ func (lint TypeCheck) parseError(err error) *result.Issue {
5963
},
6064
Text: message,
6165
FromLinter: lint.Name(),
62-
}
66+
}, nil
6367
}
6468

6569
func (lint TypeCheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
66-
if lintCtx.NotCompilingPackages == nil {
67-
return nil, nil
68-
}
69-
7070
var res []result.Issue
7171
for _, pkg := range lintCtx.NotCompilingPackages {
7272
for _, err := range pkg.Errors {
73-
i := lint.parseError(err)
74-
if i != nil {
73+
i, perr := lint.parseError(err)
74+
if perr != nil {
75+
lintCtx.Log.Warnf("Can't parse type error %s: %s", err, perr)
76+
} else {
7577
res = append(res, *i)
7678
}
7779
}

pkg/golinters/typecheck_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestParseError(t *testing.T) {
3535

3636
lint := TypeCheck{}
3737
for _, c := range cases {
38-
i := lint.parseError(errors.New(c.in))
38+
i, _ := lint.parseError(errors.New(c.in))
3939
if !c.good {
4040
assert.Nil(t, i)
4141
continue

pkg/lint/load.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,14 @@ func buildSSAProgram(lprog *loader.Program, log logutils.Log) *ssa.Program {
231231
func separateNotCompilingPackages(lintCtx *linter.Context) {
232232
prog := lintCtx.Program
233233

234+
notCompilingPackagesSet := map[*loader.PackageInfo]bool{}
235+
234236
if prog.Created != nil {
235237
compilingCreated := make([]*loader.PackageInfo, 0, len(prog.Created))
236238
for _, info := range prog.Created {
237239
if len(info.Errors) != 0 {
238240
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info)
241+
notCompilingPackagesSet[info] = true
239242
} else {
240243
compilingCreated = append(compilingCreated, info)
241244
}
@@ -245,10 +248,27 @@ func separateNotCompilingPackages(lintCtx *linter.Context) {
245248

246249
if prog.Imported != nil {
247250
for k, info := range prog.Imported {
248-
if len(info.Errors) != 0 {
251+
if len(info.Errors) == 0 {
252+
continue
253+
}
254+
255+
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info)
256+
notCompilingPackagesSet[info] = true
257+
delete(prog.Imported, k)
258+
}
259+
}
260+
261+
if prog.AllPackages != nil {
262+
for k, info := range prog.AllPackages {
263+
if len(info.Errors) == 0 {
264+
continue
265+
}
266+
267+
if !notCompilingPackagesSet[info] {
249268
lintCtx.NotCompilingPackages = append(lintCtx.NotCompilingPackages, info)
250-
delete(prog.Imported, k)
269+
notCompilingPackagesSet[info] = true
251270
}
271+
delete(prog.AllPackages, k)
252272
}
253273
}
254274

0 commit comments

Comments
 (0)