Skip to content

Commit 8f249a1

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent e779aa4 commit 8f249a1

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

go/packages/golist.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package packages
77
import (
88
"bytes"
99
"encoding/json"
10-
"errors"
1110
"fmt"
1211
"go/types"
1312
"io/ioutil"
@@ -739,16 +738,15 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
739738
}
740739

741740
if err := cmd.Run(); err != nil {
741+
// Check for 'go' executable not being found.
742+
if ee, ok := err.(*exec.Error); ok && ee.Err == exec.ErrNotFound {
743+
return nil, fmt.Errorf("'go list' driver requires 'go', but %s", exec.ErrNotFound)
744+
}
745+
742746
exitErr, ok := err.(*exec.ExitError)
743747
if !ok {
744748
// Catastrophic error:
745-
// - executable not found
746749
// - context cancellation
747-
748-
// Check for executable not existing.
749-
if _, err := exec.LookPath("go"); err != nil {
750-
return nil, errors.New("'go list' driver requires 'go' to be installed")
751-
}
752750
return nil, fmt.Errorf("couldn't exec 'go %v': %s %T", args, err, err)
753751
}
754752

0 commit comments

Comments
 (0)