From 6877dccfc214b4f74cf5fb07094a86679645675c Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 20 Feb 2023 13:38:42 -0800 Subject: [PATCH] execabs: don't override Go 1.19 error with our error Go 1.19 incorporates the functionality of execabs directly. If it has already reported an error, don't report our own error. In particular Go 1.19 moved the error from lookPathErr to Err. The code was already checking to not override lookPathErr. With this change we also do not override Err. Tested with Go 1.17 through Go 1.20. Fixes golang/go#58606 Change-Id: I110127a3925f3800cc058d93e704604a59aa38f7 Reviewed-on: https://go-review.googlesource.com/c/sys/+/469735 Reviewed-by: Bryan Mills Run-TryBot: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Auto-Submit: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- execabs/execabs.go | 2 +- execabs/execabs_go118.go | 6 ++++++ execabs/execabs_go119.go | 4 ++++ execabs/execabs_test.go | 14 +++++++++++++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/execabs/execabs.go b/execabs/execabs.go index b981cfbb..3bf40fdf 100644 --- a/execabs/execabs.go +++ b/execabs/execabs.go @@ -63,7 +63,7 @@ func LookPath(file string) (string, error) { } func fixCmd(name string, cmd *exec.Cmd) { - if filepath.Base(name) == name && !filepath.IsAbs(cmd.Path) { + if filepath.Base(name) == name && !filepath.IsAbs(cmd.Path) && !isGo119ErrFieldSet(cmd) { // exec.Command was called with a bare binary name and // exec.LookPath returned a path which is not absolute. // Set cmd.lookPathErr and clear cmd.Path so that it diff --git a/execabs/execabs_go118.go b/execabs/execabs_go118.go index 6ab5f508..2000064a 100644 --- a/execabs/execabs_go118.go +++ b/execabs/execabs_go118.go @@ -7,6 +7,12 @@ package execabs +import "os/exec" + func isGo119ErrDot(err error) bool { return false } + +func isGo119ErrFieldSet(cmd *exec.Cmd) bool { + return false +} diff --git a/execabs/execabs_go119.go b/execabs/execabs_go119.go index 46c5b525..f364b341 100644 --- a/execabs/execabs_go119.go +++ b/execabs/execabs_go119.go @@ -15,3 +15,7 @@ import ( func isGo119ErrDot(err error) bool { return errors.Is(err, exec.ErrDot) } + +func isGo119ErrFieldSet(cmd *exec.Cmd) bool { + return cmd.Err != nil +} diff --git a/execabs/execabs_test.go b/execabs/execabs_test.go index 1e8cf786..cc312f11 100644 --- a/execabs/execabs_test.go +++ b/execabs/execabs_test.go @@ -12,6 +12,7 @@ import ( "os/exec" "path/filepath" "runtime" + "strings" "testing" ) @@ -87,7 +88,7 @@ func TestCommand(t *testing.T) { expectedErr := fmt.Sprintf("execabs-test resolves to executable in current directory (.%c%s)", filepath.Separator, executable) if err = cmd("execabs-test").Run(); err == nil { t.Fatalf("Command.Run didn't fail when exec.LookPath returned a relative path") - } else if err.Error() != expectedErr { + } else if err.Error() != expectedErr && !isGo119ErrDot(err) { t.Errorf("Command.Run returned unexpected error: want %q, got %q", expectedErr, err.Error()) } } @@ -130,3 +131,14 @@ func TestLookPath(t *testing.T) { t.Errorf("LookPath returned unexpected error: want %q, got %q", expectedErr, err.Error()) } } + +// Issue #58606 +func TestDoesNotExist(t *testing.T) { + err := Command("this-executable-should-not-exist").Start() + if err == nil { + t.Fatal("command should have failed") + } + if strings.Contains(err.Error(), "resolves to executable in current directory") { + t.Errorf("error (%v) should not refer to current directory", err) + } +}