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 <bcmills@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
diff --git a/execabs/execabs.go b/execabs/execabs.go
index b981cfb..3bf40fd 100644
--- a/execabs/execabs.go
+++ b/execabs/execabs.go
@@ -63,7 +63,7 @@
 }
 
 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 6ab5f50..2000064 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 46c5b52..f364b34 100644
--- a/execabs/execabs_go119.go
+++ b/execabs/execabs_go119.go
@@ -15,3 +15,7 @@
 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 1e8cf78..cc312f1 100644
--- a/execabs/execabs_test.go
+++ b/execabs/execabs_test.go
@@ -12,6 +12,7 @@
 	"os/exec"
 	"path/filepath"
 	"runtime"
+	"strings"
 	"testing"
 )
 
@@ -87,7 +88,7 @@
 		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 @@
 		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)
+	}
+}