]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: return clear error for missing cmd.Path
authorRuss Cox <rsc@golang.org>
Tue, 3 May 2022 19:14:56 +0000 (15:14 -0400)
committerJulie Qiu <julieqiu@google.com>
Tue, 10 May 2022 17:29:08 +0000 (17:29 +0000)
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 <bcmills@google.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
src/os/exec/exec.go
src/os/exec/exec_test.go

index 8101e718ff3b8b460f4d0fce21447b857a494f45..f0dc7dab7dea0adf143c98c99451fb7f664fc874 100644 (file)
@@ -465,6 +465,9 @@ func lookExtensions(path, dir string) (string, error) {
 // The Wait method will return the exit code and release associated resources
 // once the command exits.
 func (c *Cmd) Start() error {
+       if c.Path == "" && c.Err == nil && c.lookPathErr == nil {
+               c.Err = errors.New("exec: no command")
+       }
        if c.Err != nil || c.lookPathErr != nil {
                c.closeDescriptors(c.closeAfterStart)
                c.closeDescriptors(c.closeAfterWait)
index c593cbd11d15c9654ce76adfbdc23f3aef276341..9cc14bdaca8d342242348564cf2ecbe576e86036 100644 (file)
@@ -1125,3 +1125,11 @@ func TestStringPathNotResolved(t *testing.T) {
                t.Errorf("String(%q, %q) = %q, want %q", "makemeasandwich", "-lettuce", got, want)
        }
 }
+
+func TestNoPath(t *testing.T) {
+       err := new(exec.Cmd).Start()
+       want := "exec: no command"
+       if err == nil || err.Error() != want {
+               t.Errorf("new(Cmd).Start() = %v, want %q", err, want)
+       }
+}