diff --git a/service/dap/server.go b/service/dap/server.go index 69bf59e3..d2c45d6c 100644 --- a/service/dap/server.go +++ b/service/dap/server.go @@ -436,8 +436,9 @@ func (s *Server) onInitializeRequest(request *dap.InitializeRequest) { s.send(response) } -// Output path for the compiled binary in debug or test modes. -const debugBinary string = "./__debug_bin" +// Default output file pathname for the compiled binary in debug or test modes, +// relative to the current working directory of the server. +const defaultDebugBinary string = "./__debug_bin" func cleanExeName(name string) string { if runtime.GOOS == "windows" && filepath.Ext(name) != ".exe" { @@ -471,9 +472,9 @@ func (s *Server) onLaunchRequest(request *dap.LaunchRequest) { if mode == "debug" || mode == "test" { output, ok := request.Arguments["output"].(string) if !ok || output == "" { - output = cleanExeName(debugBinary) + output = cleanExeName(defaultDebugBinary) } - debugname, err := filepath.Abs(output) + debugbinary, err := filepath.Abs(output) if err != nil { s.sendInternalErrorResponse(request.Seq, err.Error()) return @@ -491,11 +492,12 @@ func (s *Server) onLaunchRequest(request *dap.LaunchRequest) { } } + s.log.Debugf("building binary at %s", debugbinary) switch mode { case "debug": - err = gobuild.GoBuild(debugname, []string{program}, buildFlags) + err = gobuild.GoBuild(debugbinary, []string{program}, buildFlags) case "test": - err = gobuild.GoTestBuild(debugname, []string{program}, buildFlags) + err = gobuild.GoTestBuild(debugbinary, []string{program}, buildFlags) } if err != nil { s.sendErrorResponse(request.Request, @@ -503,8 +505,8 @@ func (s *Server) onLaunchRequest(request *dap.LaunchRequest) { fmt.Sprintf("Build error: %s", err.Error())) return } - program = debugname - s.binaryToRemove = debugname + program = debugbinary + s.binaryToRemove = debugbinary } s.setLaunchAttachArgs(request) diff --git a/service/dap/server_test.go b/service/dap/server_test.go index 6f1d640d..67d18025 100644 --- a/service/dap/server_test.go +++ b/service/dap/server_test.go @@ -2315,8 +2315,9 @@ func TestLaunchDebugRequest(t *testing.T) { // We reuse the harness that builds, but ignore the built binary, // only relying on the source to be built in response to LaunchRequest. runDebugSession(t, client, "launch", func() { + wd, _ := os.Getwd() client.LaunchRequestWithArgs(map[string]interface{}{ - "mode": "debug", "program": fixture.Source, "output": "__mydir"}) + "mode": "debug", "program": fixture.Source, "output": filepath.Join(wd, "__mybin")}) }, fixture.Source) }) // Wait for the test to finish to capture all stderr @@ -2346,14 +2347,14 @@ func TestLaunchRequestDefaults(t *testing.T) { runTest(t, "increment", func(client *daptest.Client, fixture protest.Fixture) { runDebugSession(t, client, "launch", func() { client.LaunchRequestWithArgs(map[string]interface{}{ - "mode": "" /*"debug" by default*/, "program": fixture.Source, "output": "__mydir"}) + "mode": "" /*"debug" by default*/, "program": fixture.Source, "output": "__mybin"}) }, fixture.Source) }) runTest(t, "increment", func(client *daptest.Client, fixture protest.Fixture) { runDebugSession(t, client, "launch", func() { // Use the default output directory. client.LaunchRequestWithArgs(map[string]interface{}{ - /*"mode":"debug" by default*/ "program": fixture.Source, "output": "__mydir"}) + /*"mode":"debug" by default*/ "program": fixture.Source, "output": "__mybin"}) }, fixture.Source) }) runTest(t, "increment", func(client *daptest.Client, fixture protest.Fixture) { @@ -2373,7 +2374,7 @@ func TestLaunchRequestDefaultsNoDebug(t *testing.T) { "noDebug": true, "mode": "", /*"debug" by default*/ "program": fixture.Source, - "output": cleanExeName("__mydir")}) + "output": cleanExeName("__mybin")}) }, fixture.Source) }) runTest(t, "increment", func(client *daptest.Client, fixture protest.Fixture) { @@ -2383,7 +2384,7 @@ func TestLaunchRequestDefaultsNoDebug(t *testing.T) { "noDebug": true, /*"mode":"debug" by default*/ "program": fixture.Source, - "output": cleanExeName("__mydir")}) + "output": cleanExeName("__mybin")}) }, fixture.Source) }) runTest(t, "increment", func(client *daptest.Client, fixture protest.Fixture) { @@ -2445,7 +2446,7 @@ func TestLaunchRequestWithBuildFlags(t *testing.T) { // We reuse the harness that builds, but ignore the built binary, // only relying on the source to be built in response to LaunchRequest. client.LaunchRequestWithArgs(map[string]interface{}{ - "mode": "debug", "program": fixture.Source, "output": "__mydir", + "mode": "debug", "program": fixture.Source, "output": "__mybin", "buildFlags": "-ldflags '-X main.Hello=World'"}) }, fixture.Source) })