Skip to content

Commit da439b2

Browse files
committed
Change startDebugging to connect to parent adapter if type=server with executable
With this change `startDebugging` should work out of the box for debug adapters configured with type=server and an executable. For example, for vscode-js-debug, instead of a custom adapter factory like: ```lua require("dap").adapters["pwa-node"] = function(on_config, config, parent) local target = config["__pendingTargetId"] if target and parent then local adapter = parent.adapter --[[@as ServerAdapter]] on_config({ type = "server", host = "localhost", port = adapter.port }) else on_config({ type = "server", host = "localhost", port = "${port}", executable = { command = "node", args = {"/path/to/js-debug/src/dapDebugServer.js", "${port}"}, } }) end end ``` It will be possible to use the simpler definition: ```lua require("dap").adapters["pwa-node"] = { type = "server", host = "localhost", port = "${port}", executable = { command = "node", args = {"/path/to/js-debug/src/dapDebugServer.js", "${port}"}, } } ```
1 parent 7389e85 commit da439b2

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

lua/dap/session.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,14 @@ local function start_debugging(self, request)
10151015
adapter = coroutine.yield()
10161016
end
10171017

1018+
-- Prefer connecting to root server again if it is of type server and
1019+
-- the new adapter would have an executable.
1020+
-- Spawning a new executable is likely the wrong thing to do
1021+
if self.adapter.type == "server" and adapter.executable then
1022+
adapter = vim.deepcopy(self.adapter)
1023+
adapter.executable = nil
1024+
end
1025+
10181026
local expected_types = {"executable", "server"}
10191027
if type(adapter) ~= "table" or not vim.tbl_contains(expected_types, adapter.type) then
10201028
local msg = "Invalid adapter definition. Expected a table with type `executable` or `server`: "

tests/server.lua

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ end
109109

110110
function M.spawn(opts)
111111
opts = opts or {}
112-
local server = uv.new_tcp()
112+
local server = assert(uv.new_tcp())
113113
local host = '127.0.0.1'
114114
local spy = {
115115
requests = {},
@@ -126,16 +126,22 @@ function M.spawn(opts)
126126
seq = 0,
127127
handlers = {},
128128
spy = spy,
129+
num_connected = 0,
129130
}
130131
setmetatable(client, {__index = Client})
131132
server:listen(128, function(err)
132133
assert(not err, err)
133-
local socket = uv.new_tcp()
134+
client.num_connected = client.num_connected + 1
135+
local socket = assert(uv.new_tcp())
134136
client.socket = socket
135137
server:accept(socket)
136-
socket:read_start(rpc.create_read_loop((function(body)
138+
local function on_chunk(body)
137139
client:handle_input(body)
138-
end)))
140+
end
141+
local function on_eof()
142+
client.num_connected = client.num_connected - 1
143+
end
144+
socket:read_start(rpc.create_read_loop(on_chunk, on_eof))
139145
end)
140146
return {
141147
client = client,

tests/sessions_spec.lua

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('sessions', function()
7171
}
7272
run_and_wait_until_initialized(conf1, srv1)
7373
srv1.client:send_request("startDebugging", {
74-
request = "laucnh",
74+
request = "launch",
7575
configuration = {
7676
type = "dummy2",
7777
name = "Subprocess"
@@ -88,4 +88,34 @@ describe('sessions', function()
8888
wait(function() return vim.tbl_count(dap.session().children) == 0 end)
8989
assert.are.same({}, dap.session().children)
9090
end)
91+
92+
it("startDebugging connects to root adapter if type server with executable", function()
93+
local conf1 = {
94+
type = 'dummy1',
95+
request = 'launch',
96+
name = 'Launch file 1',
97+
}
98+
local session = run_and_wait_until_initialized(conf1, srv1)
99+
assert.are.same(1, srv1.client.num_connected)
100+
dap.adapters.dummy2 = {
101+
type = "server",
102+
executable = {
103+
command = "echo",
104+
args = {"not", "used"},
105+
}
106+
}
107+
srv1.client:send_request("startDebugging", {
108+
request = "launch",
109+
configuration = {
110+
type = "dummy2",
111+
name = "Subprocess"
112+
}
113+
})
114+
wait(
115+
function() return vim.tbl_count(session.children) == 1 end,
116+
function() return dap.session() end
117+
)
118+
assert.are.same(2, srv1.client.num_connected)
119+
dap.terminate()
120+
end)
91121
end)

0 commit comments

Comments
 (0)