diff options
| author | Bryan C. Mills <bcmills@google.com> | 2022-11-15 12:34:31 -0500 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2022-11-16 22:02:42 +0000 |
| commit | 17de9e2d18a62e28bb8032b04d81835e7705d4c7 (patch) | |
| tree | 344f08284e572adfc24a49d1ecbf0187ab1662e2 /src/cmd/fix/typecheck.go | |
| parent | 52d9e41ac303cfed4c4cfe86ec6d663a18c3448d (diff) | |
| download | go-17de9e2d18a62e28bb8032b04d81835e7705d4c7.tar.xz | |
cmd/fix: disallow cgo errors in tests
The 'cgo' command invoked by 'go fix' was not valid when built with
-trimpath, but the test was not failing because errors from the
command were being logged and ignored instead of causing tests to
fail. Changing the code and test not to ignore the errors revealed
that a number of existing tests were always, unconditionally
triggering cgo errors which were then ignored.
This change updates those tests to no longer produce cgo errors,
and to check their results when cgo is enabled.
For #51473.
Updates #51461.
Change-Id: Ib9d1ea93f26d30daa824d75ed634eaf530af086d
Reviewed-on: https://go-review.googlesource.com/c/go/+/450714
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/fix/typecheck.go')
| -rw-r--r-- | src/cmd/fix/typecheck.go | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/cmd/fix/typecheck.go b/src/cmd/fix/typecheck.go index 27042e05a5..b115987390 100644 --- a/src/cmd/fix/typecheck.go +++ b/src/cmd/fix/typecheck.go @@ -170,7 +170,16 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[any]string, assign map[ if err != nil { return err } - cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), "tool", "cgo", "-objdir", dir, "-srcdir", dir, "in.go") + goCmd := "go" + if goroot := runtime.GOROOT(); goroot != "" { + goCmd = filepath.Join(goroot, "bin", "go") + } + cmd := exec.Command(goCmd, "tool", "cgo", "-objdir", dir, "-srcdir", dir, "in.go") + if reportCgoError != nil { + // Since cgo command errors will be reported, also forward the error + // output from the command for debugging. + cmd.Stderr = os.Stderr + } err = cmd.Run() if err != nil { return err @@ -206,7 +215,11 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[any]string, assign map[ return nil }() if err != nil { - fmt.Fprintf(os.Stderr, "go fix: warning: no cgo types: %s\n", err) + if reportCgoError == nil { + fmt.Fprintf(os.Stderr, "go fix: warning: no cgo types: %s\n", err) + } else { + reportCgoError(err) + } } } @@ -285,6 +298,10 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[any]string, assign map[ return typeof, assign } +// reportCgoError, if non-nil, reports a non-nil error from running the "cgo" +// tool. (Set to a non-nil hook during testing if cgo is expected to work.) +var reportCgoError func(err error) + func makeExprList(a []*ast.Ident) []ast.Expr { var b []ast.Expr for _, x := range a { |
