From d8a2a07971905ff1e4f5a39e245ffca53f7bfaba Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Tue, 26 Jan 2021 18:31:21 -0500 Subject: [PATCH] go/packages: improve go invocation errors Rather than coming up with an ad-hoc error message, use the friendly errors from the gocommand package. I noticed this because the latter is missing the verb. I *think* this is generally an improvement; instead of seeing a massive command line, users just see the actual error from the go command. Even if it's not, I'd rather improve the single source of the error than play whack-a-mole. Change-Id: Ib110eaac6c3984e617339cf14cffe3b59f33591b Reviewed-on: https://go-review.googlesource.com/c/tools/+/287033 Trust: Heschi Kreinick Run-TryBot: Heschi Kreinick gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Rebecca Stambler --- go/packages/golist.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/packages/golist.go b/go/packages/golist.go index ec417ba830..f89b05bb14 100644 --- a/go/packages/golist.go +++ b/go/packages/golist.go @@ -10,7 +10,6 @@ import ( "encoding/json" "fmt" "go/types" - exec "golang.org/x/sys/execabs" "io/ioutil" "log" "os" @@ -23,6 +22,7 @@ import ( "sync" "unicode" + exec "golang.org/x/sys/execabs" "golang.org/x/tools/go/internal/packagesdriver" "golang.org/x/tools/internal/gocommand" "golang.org/x/xerrors" @@ -865,7 +865,7 @@ func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer, if gocmdRunner == nil { gocmdRunner = &gocommand.Runner{} } - stdout, stderr, _, err := gocmdRunner.RunRaw(cfg.Context, inv) + stdout, stderr, friendlyErr, err := gocmdRunner.RunRaw(cfg.Context, inv) if err != nil { // Check for 'go' executable not being found. if ee, ok := err.(*exec.Error); ok && ee.Err == exec.ErrNotFound { @@ -886,7 +886,7 @@ func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer, // Related to #24854 if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "unexpected directory layout") { - return nil, fmt.Errorf("%s", stderr.String()) + return nil, friendlyErr } // Is there an error running the C compiler in cgo? This will be reported in the "Error" field @@ -999,7 +999,7 @@ func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer, // TODO(matloob): Remove these once we can depend on go list to exit with a zero status with -e even when // packages don't exist or a build fails. if !usesExportData(cfg) && !containsGoFile(args) { - return nil, fmt.Errorf("go %v: %s: %s", args, exitErr, stderr) + return nil, friendlyErr } } return stdout, nil