@@ -126,7 +126,7 @@ func (gc *GenericCommand) Run(expect *Expected) {
126126 var env []string
127127 output := & bytes.Buffer {}
128128 stdout := ""
129- errorGroup := & errgroup.Group {}
129+ copyGroup := & errgroup.Group {}
130130 var tty * os.File
131131 var psty * os.File
132132 if ! gc .async {
@@ -138,17 +138,37 @@ func (gc *GenericCommand) Run(expect *Expected) {
138138 iCmdCmd .Stdin = tty
139139 iCmdCmd .Stdout = tty
140140
141- gc .result = icmd .StartCmd (iCmdCmd )
142-
143- for _ , writer := range gc .ptyWriters {
144- _ = writer (psty )
145- }
146-
147141 // Copy from the master
148- errorGroup .Go (func () error {
142+ copyGroup .Go (func () error {
149143 _ , _ = io .Copy (output , psty )
150144 return nil
151145 })
146+
147+ // Cautiously start the command
148+ startGroup := & errgroup.Group {}
149+ startGroup .Go (func () error {
150+ gc .result = icmd .StartCmd (iCmdCmd )
151+ if gc .result .Error != nil {
152+ gc .t .Log ("start command failed" )
153+ gc .t .Log (gc .result .ExitCode )
154+ gc .t .Log (gc .result .Error )
155+ return gc .result .Error
156+ }
157+
158+ for _ , writer := range gc .ptyWriters {
159+ err := writer (psty )
160+ if err != nil {
161+ gc .t .Log ("writing to the pty failed" )
162+ gc .t .Log (err )
163+ return err
164+ }
165+ }
166+
167+ return nil
168+ })
169+
170+ // Let the error through for WaitOnCmd to handle
171+ _ = startGroup .Wait ()
152172 } else {
153173 // Run it
154174 gc .result = icmd .StartCmd (iCmdCmd )
@@ -161,7 +181,7 @@ func (gc *GenericCommand) Run(expect *Expected) {
161181 if gc .pty {
162182 _ = tty .Close ()
163183 _ = psty .Close ()
164- _ = errorGroup .Wait ()
184+ _ = copyGroup .Wait ()
165185 }
166186
167187 stdout = result .Stdout ()
0 commit comments