1
0
mirror of https://github.com/golang/go synced 2024-11-19 00:34:40 -07:00

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 <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Dmitri Shuralyov 2019-03-22 11:37:48 -04:00
parent e779aa49e3
commit 8f249a1648

View File

@ -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)
}