Skip to content

Commit 960ffa9

Browse files
rscjulieqiu
authored andcommitted
os/exec: return clear error for missing cmd.Path
Following up on CL 403694, there is a bit of confusion about when Path is and isn't set, along with now the exported Err field. Catch the case where Path and Err (and lookPathErr) are all unset and give a helpful error. Fixes #52574 Followup after #43724. Change-Id: I03205172aef3801c3194f5098bdb93290c02b1b6 Reviewed-on: https://go-review.googlesource.com/c/go/+/403759 Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent 7a574ff commit 960ffa9

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/os/exec/exec.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ func lookExtensions(path, dir string) (string, error) {
465465
// The Wait method will return the exit code and release associated resources
466466
// once the command exits.
467467
func (c *Cmd) Start() error {
468+
if c.Path == "" && c.Err == nil && c.lookPathErr == nil {
469+
c.Err = errors.New("exec: no command")
470+
}
468471
if c.Err != nil || c.lookPathErr != nil {
469472
c.closeDescriptors(c.closeAfterStart)
470473
c.closeDescriptors(c.closeAfterWait)

src/os/exec/exec_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,3 +1125,11 @@ func TestStringPathNotResolved(t *testing.T) {
11251125
t.Errorf("String(%q, %q) = %q, want %q", "makemeasandwich", "-lettuce", got, want)
11261126
}
11271127
}
1128+
1129+
func TestNoPath(t *testing.T) {
1130+
err := new(exec.Cmd).Start()
1131+
want := "exec: no command"
1132+
if err == nil || err.Error() != want {
1133+
t.Errorf("new(Cmd).Start() = %v, want %q", err, want)
1134+
}
1135+
}

0 commit comments

Comments
 (0)