Skip to content

Commit 5e2e09d

Browse files
fmeumtyler-french
authored andcommitted
Only print type-checking error once in nogo (#4083)
**What type of PR is this?** Bug fix **What does this PR do? Why is it needed?** Previously, it was printed once by analyzer. **Which issues(s) does this PR fix?** Fixes #4080 **Other notes for review**
1 parent 046d5bc commit 5e2e09d

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

go/tools/builders/nogo_main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,7 @@ func (act *action) execOnce() {
372372
act.pass = pass
373373

374374
var err error
375-
if act.pkg.illTyped && !pass.Analyzer.RunDespiteErrors {
376-
err = fmt.Errorf("analysis skipped due to type-checking error: %v", act.pkg.typeCheckError)
377-
} else {
375+
if !act.pkg.illTyped || pass.Analyzer.RunDespiteErrors {
378376
act.result, err = pass.Analyzer.Run(pass)
379377
if err == nil {
380378
if got, want := reflect.TypeOf(act.result), pass.Analyzer.ResultType; got != want {
@@ -471,7 +469,13 @@ func checkAnalysisResults(actions []*action, pkg *goPackage) string {
471469
if cwd == "" || err != nil {
472470
errs = append(errs, fmt.Errorf("nogo failed to get CWD: %w", err))
473471
}
472+
numSkipped := 0
474473
for _, act := range actions {
474+
if act.pkg.illTyped && !act.a.RunDespiteErrors {
475+
// Don't report type-checking errors once per analyzer.
476+
numSkipped++
477+
continue
478+
}
475479
if act.err != nil {
476480
// Analyzer failed.
477481
errs = append(errs, fmt.Errorf("analyzer %q failed: %v", act.a.Name, act.err))
@@ -543,6 +547,9 @@ func checkAnalysisResults(actions []*action, pkg *goPackage) string {
543547
}
544548
}
545549
}
550+
if numSkipped > 0 {
551+
errs = append(errs, fmt.Errorf("%d analyzers skipped due to type-checking error: %v", numSkipped, pkg.typeCheckError))
552+
}
546553
if len(diagnostics) == 0 && len(errs) == 0 {
547554
return ""
548555
}

tests/core/nogo/custom/custom_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestMain(m *testing.M) {
3131
Nogo: "@//:nogo",
3232
Main: `
3333
-- BUILD.bazel --
34-
load("@io_bazel_rules_go//go:def.bzl", "go_library", "nogo")
34+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "nogo")
3535
3636
nogo(
3737
name = "nogo",
@@ -124,6 +124,12 @@ go_library(
124124
importpath = "dep",
125125
)
126126
127+
go_binary(
128+
name = "type_check_fail",
129+
srcs = ["type_check_fail.go"],
130+
pure = "on",
131+
)
132+
127133
-- foofuncname.go --
128134
// foofuncname checks for functions named "Foo".
129135
// It has the same package name as another check to test the checks with
@@ -434,6 +440,16 @@ func Foo() bool { // This should fail foofuncname
434440
return Bar()
435441
}
436442
443+
-- type_check_fail.go --
444+
package type_check_fail
445+
446+
import (
447+
"strings"
448+
)
449+
450+
func Foo() bool {
451+
return strings.Split("a,b,c", ",") // This fails type-checking
452+
}
437453
`,
438454
})
439455
}
@@ -443,6 +459,7 @@ func Test(t *testing.T) {
443459
desc, config, target string
444460
wantSuccess bool
445461
includes, excludes []string
462+
bazelArgs []string
446463
}{
447464
{
448465
desc: "default_config",
@@ -507,6 +524,16 @@ func Test(t *testing.T) {
507524
// note the cross platform regex :)
508525
`examplepkg[\\/]pure_src_with_err_calling_native.go:.*function must not be named Foo \(foofuncname\)`,
509526
},
527+
}, {
528+
desc: "type_check_fail",
529+
config: "config.json",
530+
target: "//:type_check_fail",
531+
wantSuccess: false,
532+
includes: []string{
533+
"4 analyzers skipped due to type-checking error: type_check_fail.go:8:10:",
534+
},
535+
// Ensure that nogo runs even though compilation fails
536+
bazelArgs: []string{"--keep_going"},
510537
}, {
511538
desc: "no_errors",
512539
target: "//:no_errors",
@@ -527,7 +554,7 @@ func Test(t *testing.T) {
527554
defer replaceInFile("BUILD.bazel", customConfig, origConfig)
528555
}
529556

530-
cmd := bazel_testing.BazelCmd("build", test.target)
557+
cmd := bazel_testing.BazelCmd(append([]string{"build", test.target}, test.bazelArgs...)...)
531558
stderr := &bytes.Buffer{}
532559
cmd.Stderr = stderr
533560
if err := cmd.Run(); err == nil && !test.wantSuccess {

0 commit comments

Comments
 (0)