dap: pass exit code through exited event

Pass the exit code through the exited event back to the client and
ensure that the printed text is printed completely.

Previously, the exited event just had a big todo and the printer would
sometimes fail to send messages to the connected client. This moves the
printer wait to before the debug adapter is closed to ensure that all
messages get sent through the connection to the editor. While there, I
also plumbed in the exit code to exited. It's not necessarily the real
exit code but it will produce a zero on build success and a non-zero
code on build failure so that should be good enough.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
Jonathan A. Sternberg
2026-03-20 14:02:21 -05:00
parent ba04f8f7b4
commit 180cfd9e41
6 changed files with 106 additions and 20 deletions
+10 -5
View File
@@ -398,10 +398,6 @@ func runBuild(ctx context.Context, dockerCli command.Cli, debugOpts debuggerOpti
done := timeBuildCommand(mp, attributes)
resp, inputs, retErr := runBuildWithOptions(ctx, dockerCli, opts, dbg, printer)
if err := printer.Wait(); retErr == nil {
retErr = err
}
done(retErr)
if retErr != nil {
return retErr
@@ -462,12 +458,21 @@ func runBuildWithOptions(ctx context.Context, dockerCli command.Cli, opts *Build
if err := dbg.Start(printer, opts); err != nil {
return nil, nil, err
}
defer dbg.Stop()
defer func() { dbg.Stop(retErr) }()
bh = dbg.Handler()
dockerCli.SetIn(nil)
}
// Ensure messages sent to the printer are flushed before the debugger completes.
// This prevents late messages from not being sent because the connection was
// terminated before completion of the debugger.
defer func() {
if err := printer.Wait(); retErr == nil {
retErr = err
}
}()
in := dockerCli.In()
for {
resp, inputs, err := RunBuild(ctx, dockerCli, opts, in, printer, &bh)
+2 -2
View File
@@ -91,9 +91,9 @@ func (d *adapterProtocolDebugger) Start(printer *progress.Printer, opts *BuildOp
return nil
}
func (d *adapterProtocolDebugger) Stop() error {
func (d *adapterProtocolDebugger) Stop(retErr error) error {
defer d.conn.Close()
return d.Adapter.Stop()
return d.Adapter.Stop(retErr)
}
func dapAttachCmd() *cobra.Command {
+2 -2
View File
@@ -41,7 +41,7 @@ type debuggerOptions interface {
type debuggerInstance interface {
Start(printer *progress.Printer, opts *BuildOptions) error
Handler() build.Handler
Stop() error
Stop(retErr error) error
Out() io.Writer
}
@@ -98,7 +98,7 @@ func (d *monitorDebuggerInstance) Handler() build.Handler {
return d.m.Handler()
}
func (d *monitorDebuggerInstance) Stop() error {
func (d *monitorDebuggerInstance) Stop(_ error) error {
return d.m.Close()
}