From 6be7e01d83016eed104c7e3b0c485db135ba0f3f Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Wed, 26 Jun 2024 12:28:25 -0500 Subject: [PATCH] fix: do not add executable to adapter in launch request The adapter would add the `executable` property to the adapter regardless of whether the request was `launch` or `attach`. When `attach` was used, it would attempt to start `dlv` on the same port that it was attempting to configure. This changes it so the executable is not added if `attach` is used and adds some protection code to prevent the random port assignment from being used with the attach configuration (where it wouldn't work anyway). --- lua/dap-go.lua | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/lua/dap-go.lua b/lua/dap-go.lua index a365043..edddfd8 100644 --- a/lua/dap-go.lua +++ b/lua/dap-go.lua @@ -48,19 +48,32 @@ local function setup_delve_adapter(dap, config) local args = { "dap", "-l", "127.0.0.1:" .. config.delve.port } vim.list_extend(args, config.delve.args) - dap.adapters.go = { - type = "server", - port = config.delve.port, - executable = { - command = config.delve.path, - args = args, - detached = config.delve.detached, - cwd = config.delve.cwd, - }, - options = { - initialize_timeout_sec = config.delve.initialize_timeout_sec, - }, - } + local delve = config.delve + dap.adapters.go = function(cb, cfg) + local adapter = { + type = "server", + port = (cfg.port or delve.port), + options = { + initialize_timeout_sec = delve.initialize_timeout_sec, + }, + } + + assert(adapter.port, "`port` is required for go configuration") + assert( + cfg.request ~= "attach" or adapter.port ~= "{port}", + "`port` cannot be randomly assigned when using `attach` configuration" + ) + + if cfg.request == "launch" then + adapter.executable = { + command = delve.path, + args = args, + detached = delve.detached, + cwd = delve.cwd, + } + end + cb(adapter) + end end local function setup_go_configuration(dap, configs)