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>
This commit is contained in:
Ian Lance Taylor 2023-02-20 13:38:42 -08:00 коммит произвёл Gopher Robot
Родитель b13f40e221
Коммит 6877dccfc2
4 изменённых файлов: 24 добавлений и 2 удалений

Просмотреть файл

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

Просмотреть файл

@ -7,6 +7,12 @@
package execabs
import "os/exec"
func isGo119ErrDot(err error) bool {
return false
}
func isGo119ErrFieldSet(cmd *exec.Cmd) bool {
return false
}

Просмотреть файл

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

Просмотреть файл

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