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
+69
View File
@@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"slices"
"strings"
@@ -87,6 +88,7 @@ var dapBuildTests = []func(t *testing.T, sb integration.Sandbox){
testDapBuildStepOut,
testDapBuildVariables,
testDapBuildDeferredEval,
testDapBuildExitedEvent,
}
func testDapBuild(t *testing.T, sb integration.Sandbox) {
@@ -914,6 +916,73 @@ func testDapBuildDeferredEval(t *testing.T, sb integration.Sandbox) {
require.ErrorAs(t, done(true), &exitErr)
}
func testDapBuildExitedEvent(t *testing.T, sb integration.Sandbox) {
t.Run("success", func(t *testing.T) {
dir := createTestProject(t)
client, done, err := dapBuildCmd(t, sb)
require.NoError(t, err)
ch := make(chan *dap.ExitedEvent, 1)
client.RegisterEvent("exited", func(em dap.EventMessage) {
ch <- em.(*dap.ExitedEvent)
close(ch)
})
// Project should just build normally.
doLaunch(t, client, commands.LaunchConfig{
Dockerfile: path.Join(dir, "Dockerfile"),
ContextPath: dir,
})
select {
case exited := <-ch:
require.Equal(t, 0, exited.Body.ExitCode)
case <-time.After(5 * time.Second):
require.Fail(t, "timeout reached")
}
require.NoError(t, done(true))
})
t.Run("failure", func(t *testing.T) {
dir := createTestProject(t)
client, done, err := dapBuildCmd(t, sb)
require.NoError(t, err)
ch := make(chan *dap.ExitedEvent, 1)
client.RegisterEvent("exited", func(em dap.EventMessage) {
ch <- em.(*dap.ExitedEvent)
close(ch)
})
// Delete foo from the test project so this will fail.
err = os.Remove(filepath.Join(dir, "foo"))
require.NoError(t, err)
interruptCh := pollInterruptEvents(client)
doLaunch(t, client, commands.LaunchConfig{
Dockerfile: path.Join(dir, "Dockerfile"),
ContextPath: dir,
})
// We will hit an interrupt because of the failure.
stopped := waitForInterrupt[*dap.StoppedEvent](t, interruptCh)
require.Equal(t, "exception", stopped.Body.Reason)
// Continue execution which should trigger the exited event.
doNext(t, client, stopped.Body.ThreadId)
select {
case exited := <-ch:
require.NotEqual(t, 0, exited.Body.ExitCode)
case <-time.After(time.Second):
require.Fail(t, "timeout reached")
}
var exitErr *exec.ExitError
require.ErrorAs(t, done(false), &exitErr)
})
}
func doLaunch(t *testing.T, client *daptest.Client, config commands.LaunchConfig, bps ...dap.SourceBreakpoint) {
t.Helper()