@@ -2,8 +2,8 @@ package replpkg
22
33import (
44 "bytes"
5+ "errors"
56 "fmt"
6- "io"
77 "io/ioutil"
88 "os"
99 "os/exec"
@@ -206,8 +206,7 @@ func (s *Session) evalStmt(in string, noPrint bool) error {
206206 cmd .Stderr = b
207207 err = cmd .Run ()
208208 if err != nil {
209- os .Stderr .WriteString ("Error running goimports:\n " )
210- io .Copy (os .Stderr , b )
209+ err = errors .New (b .String ())
211210 return err
212211 }
213212
@@ -217,7 +216,7 @@ func (s *Session) evalStmt(in string, noPrint bool) error {
217216 }
218217
219218 if err = s .importFile (functproxy ); err != nil {
220- errorf ("%s" , err )
219+ errorf ("%s" , err . Error () )
221220 if _ , ok := err .(scanner.ErrorList ); ok {
222221 return ErrContinue
223222 }
@@ -369,7 +368,7 @@ func (s *Session) Eval(in string) (string, bytes.Buffer, error) {
369368 if err == ErrQuit {
370369 return "" , bytes.Buffer {}, err
371370 }
372- errorf ("%s: %s" , command .name , err )
371+ errorf ("%s: %s" , command .name , err . Error () )
373372 }
374373 }
375374 }
@@ -386,14 +385,14 @@ func (s *Session) Eval(in string) (string, bytes.Buffer, error) {
386385 // Extract statements.
387386 priorListLength := len (s .mainBody .List )
388387 if err := s .separateEvalStmt (in ); err != nil {
389- return "" , bytes.Buffer {} , err
388+ return "" , * bytes .NewBuffer ([] byte ( err . Error ())) , err
390389 }
391390
392391 s .doQuickFix ()
393392
394- output , strerr , err := s .Run ()
395- if err != nil {
396- if exitErr , ok := err .(* exec.ExitError ); ok {
393+ output , stderr , runErr := s .Run ()
394+ if runErr != nil {
395+ if exitErr , ok := runErr .(* exec.ExitError ); ok {
397396 // if failed with status 2, remove the last statement
398397 if st , ok := exitErr .ProcessState .Sys ().(syscall.WaitStatus ); ok {
399398 if st .ExitStatus () == 2 {
@@ -402,24 +401,24 @@ func (s *Session) Eval(in string) (string, bytes.Buffer, error) {
402401 }
403402 }
404403 }
405- errorf ("%s" , err )
404+ errorf ("%s" , runErr . Error () )
406405 }
407406
408407 // Cleanup the session file.
409408 s .mainBody .List = s .mainBody .List [0 :priorListLength ]
410409 if err := s .cleanEvalStmt (in ); err != nil {
411- return "" , bytes. Buffer {} , err
410+ return string ( output ), stderr , err
412411 }
413412 f , err := os .Create (s .FilePath )
414413 if err != nil {
415- return "" , bytes. Buffer {} , err
414+ return string ( output ), stderr , err
416415 }
417416 err = printer .Fprint (f , s .Fset , s .File )
418417 if err != nil {
419- return "" , bytes. Buffer {} , err
418+ return string ( output ), stderr , err
420419 }
421420
422- return string (output ), strerr , err
421+ return string (output ), stderr , runErr
423422}
424423
425424// separateEvalStmt separates what can be evaluated via evalExpr from what cannot.
@@ -445,7 +444,9 @@ func (s *Session) separateEvalStmt(in string) error {
445444 }
446445
447446 if strings .LastIndex (line , "}" ) == len (line )- 1 {
448- bracketCount --
447+ if ! strings .HasSuffix (line , "{}" ) {
448+ bracketCount --
449+ }
449450 }
450451 if strings .LastIndex (line , "{" ) == len (line )- 1 {
451452 bracketCount ++
@@ -525,17 +526,17 @@ func (s *Session) includeFiles(files []string) {
525526func (s * Session ) includeFile (file string ) {
526527 content , err := ioutil .ReadFile (file )
527528 if err != nil {
528- errorf ("%s" , err )
529+ errorf ("%s" , err . Error () )
529530 return
530531 }
531532
532533 if err = s .importPackages (content ); err != nil {
533- errorf ("%s" , err )
534+ errorf ("%s" , err . Error () )
534535 return
535536 }
536537
537538 if err = s .importFile (content ); err != nil {
538- errorf ("%s" , err )
539+ errorf ("%s" , err . Error () )
539540 }
540541
541542 infof ("added file %s" , file )
0 commit comments