Skip to content

Commit 6877dcc

Browse files
ianlancetaylorgopherbot
authored andcommitted
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>
1 parent b13f40e commit 6877dcc

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

execabs/execabs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func LookPath(file string) (string, error) {
6363
}
6464

6565
func fixCmd(name string, cmd *exec.Cmd) {
66-
if filepath.Base(name) == name && !filepath.IsAbs(cmd.Path) {
66+
if filepath.Base(name) == name && !filepath.IsAbs(cmd.Path) && !isGo119ErrFieldSet(cmd) {
6767
// exec.Command was called with a bare binary name and
6868
// exec.LookPath returned a path which is not absolute.
6969
// Set cmd.lookPathErr and clear cmd.Path so that it

execabs/execabs_go118.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
package execabs
99

10+
import "os/exec"
11+
1012
func isGo119ErrDot(err error) bool {
1113
return false
1214
}
15+
16+
func isGo119ErrFieldSet(cmd *exec.Cmd) bool {
17+
return false
18+
}

execabs/execabs_go119.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ import (
1515
func isGo119ErrDot(err error) bool {
1616
return errors.Is(err, exec.ErrDot)
1717
}
18+
19+
func isGo119ErrFieldSet(cmd *exec.Cmd) bool {
20+
return cmd.Err != nil
21+
}

execabs/execabs_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os/exec"
1313
"path/filepath"
1414
"runtime"
15+
"strings"
1516
"testing"
1617
)
1718

@@ -87,7 +88,7 @@ func TestCommand(t *testing.T) {
8788
expectedErr := fmt.Sprintf("execabs-test resolves to executable in current directory (.%c%s)", filepath.Separator, executable)
8889
if err = cmd("execabs-test").Run(); err == nil {
8990
t.Fatalf("Command.Run didn't fail when exec.LookPath returned a relative path")
90-
} else if err.Error() != expectedErr {
91+
} else if err.Error() != expectedErr && !isGo119ErrDot(err) {
9192
t.Errorf("Command.Run returned unexpected error: want %q, got %q", expectedErr, err.Error())
9293
}
9394
}
@@ -130,3 +131,14 @@ func TestLookPath(t *testing.T) {
130131
t.Errorf("LookPath returned unexpected error: want %q, got %q", expectedErr, err.Error())
131132
}
132133
}
134+
135+
// Issue #58606
136+
func TestDoesNotExist(t *testing.T) {
137+
err := Command("this-executable-should-not-exist").Start()
138+
if err == nil {
139+
t.Fatal("command should have failed")
140+
}
141+
if strings.Contains(err.Error(), "resolves to executable in current directory") {
142+
t.Errorf("error (%v) should not refer to current directory", err)
143+
}
144+
}

0 commit comments

Comments
 (0)