From 8f249a1648fcc76f3d199643ac427b4e20262098 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Fri, 22 Mar 2019 11:37:48 -0400 Subject: [PATCH] go/packages: detect missing binary via exec.ErrNotFound error exec.Command already runs exec.LookPath when given a name that contains no path separators. There's no need to call exec.LookPath a second time to detect that cmd.Run failed because of a missing executable file. It can be detected from the returned error. Do so because it's cleaner. Also improve the error text to say that the problem was that the go executable file was not found in $PATH (or %PATH%, etc., depending on the underlying operating system). In the general case, we can't know if Go is or isn't installed. Example error text on macOS: gopackages: 'go list' driver requires 'go', but executable file not found in $PATH Updates golang/go#29552 Change-Id: I769553f125240dccd02098c22641f6a1ed10063c Reviewed-on: https://go-review.googlesource.com/c/tools/+/168897 Run-TryBot: Dmitri Shuralyov TryBot-Result: Gobot Gobot Reviewed-by: Michael Matloob --- go/packages/golist.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/go/packages/golist.go b/go/packages/golist.go index c6c08a6f2f..4035086e69 100644 --- a/go/packages/golist.go +++ b/go/packages/golist.go @@ -7,7 +7,6 @@ package packages import ( "bytes" "encoding/json" - "errors" "fmt" "go/types" "io/ioutil" @@ -739,16 +738,15 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) { } if err := cmd.Run(); err != nil { + // Check for 'go' executable not being found. + if ee, ok := err.(*exec.Error); ok && ee.Err == exec.ErrNotFound { + return nil, fmt.Errorf("'go list' driver requires 'go', but %s", exec.ErrNotFound) + } + exitErr, ok := err.(*exec.ExitError) if !ok { // Catastrophic error: - // - executable not found // - context cancellation - - // Check for executable not existing. - if _, err := exec.LookPath("go"); err != nil { - return nil, errors.New("'go list' driver requires 'go' to be installed") - } return nil, fmt.Errorf("couldn't exec 'go %v': %s %T", args, err, err) }