From 3ce4389cd016fa5224de5ac9923778fae60957de Mon Sep 17 00:00:00 2001 From: "Hana (Hyang-Ah) Kim" Date: Fri, 6 Oct 2023 10:26:59 -0400 Subject: [PATCH] service/dap: fix bugs in stdout/stderr handling Fixes bugs introduced in v1.21.1 * Avoid dropping the last bytes from stderr/stdout when Read returns an error. (Read returns n>0). And skip sending Output event if Read returns n==0. * Fix the bug that drops all stdout in the existing noDebug mode. For #3253 --- service/dap/server.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/service/dap/server.go b/service/dap/server.go index fbf9d3f86d..94953c827f 100644 --- a/service/dap/server.go +++ b/service/dap/server.go @@ -1050,6 +1050,15 @@ func (s *Session) onLaunchRequest(request *dap.LaunchRequest) { var out [1024]byte for { n, err := reader.Read(out[:]) + if n > 0 { + outs := string(out[:n]) + s.send(&dap.OutputEvent{ + Event: *newEvent("output"), + Body: dap.OutputEventBody{ + Output: outs, + Category: category, + }}) + } if err != nil { if err == io.EOF { return @@ -1057,13 +1066,6 @@ func (s *Session) onLaunchRequest(request *dap.LaunchRequest) { s.config.log.Errorf("failed read by %s - %v ", category, err) return } - outs := string(out[:n]) - s.send(&dap.OutputEvent{ - Event: *newEvent("output"), - Body: dap.OutputEventBody{ - Output: outs, - Category: category, - }}) } } @@ -1186,7 +1188,7 @@ func (s *Session) newNoDebugProcess(program string, targetArgs []string, wd stri return nil, err } } else { - cmd.Stdout, cmd.Stderr = os.Stdin, os.Stderr + cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr } if err = cmd.Start(); err != nil {