nvim-dap附加netcoredbg时processId缺失及配置异常求助
如何配置nvim-dap附加到netcoredbg调试服务器?
问题场景
我原本已有启动netcoredbg的调试配置,现在想配置nvim-dap附加到运行中的调试服务器,于是添加了coreclrattach适配器及对应的attach配置:
local dap, dapui = require("dap"), require("dapui") dap.adapters.coreclr = { type = 'executable', command = '/usr/local/netcoredbg', args = { '--interpreter=vscode' }, } dap.adapters.coreclrattach = { type = 'server', port = 4711, } dap.configurations.cs = { { type = "coreclr", name = "launch - netcoredbg", request = "launch", program = function() return vim.fn.input('Path to dll', vim.fn.getcwd() .. '/bin/Debug/', 'file') end, }, { type = "coreclrattach", name = "attach - netcoredbg", request = "attach", pid = require('dap.utils').pick_process, args = {}, }, }
我在shell中执行以下命令启动调试服务器:
/usr/local/netcoredbg --interpreter=vscode --server -- dotnet run -c Debug
选择attach配置并选中目标进程后,出现错误:
Error on attach: can't parse: [json.exception.out_of_range.403] key 'processId' not found
日志文件log.txt对应错误:
9827.176 I/NETCOREDBG(P60708, T60708): main.cpp: main(431) > Netcoredbg started 9853.273 I/NETCOREDBG(P60708, T60708): main.cpp: instantiate_protocol(113) > Creating protocol VSCodeProtocol 9853.274 I/NETCOREDBG(P60708, T60708): main.cpp: main(475) > pidDebugee 0 9853.274 I/NETCOREDBG(P60708, T60832): ioredirect.cpp: worker(164) > worker started 9853.275 E/NETCOREDBG(P60708, T60836): vscodeprotocol.cpp: HandleCommandJSON(855) > JSON error: [json.exception.out_of_range.403] key 'processId' not found 9853.280 I/NETCOREDBG(P60708, T60708): vscodeprotocol.cpp: ReadData(872) > EOF
之后我把配置中的pid改为processId:
{ type = "coreclrattach", name = "attach - netcoredbg", request = "attach", processId = require('dap.utils').pick_process, args = {}, },
再次启动调试会话后,出现新错误:
Failed command 'configurationDone' : 0x80070057
解决方法
1. 修正适配器与配置结构
不需要单独定义coreclrattach适配器,直接复用现有的coreclr适配器即可——附加模式同样通过netcoredbg的VSCode协议交互,无需单独的server类型适配器。
2. 调整attach配置参数
确保使用processId字段传递进程ID,同时可以根据需要指定调试服务器的地址和端口。
修改后的完整配置:
local dap, dapui = require("dap"), require("dapui") dap.adapters.coreclr = { type = 'executable', command = '/usr/local/netcoredbg', args = { '--interpreter=vscode' }, } dap.configurations.cs = { { type = "coreclr", name = "launch - netcoredbg", request = "launch", program = function() return vim.fn.input('Path to dll', vim.fn.getcwd() .. '/bin/Debug/', 'file') end, }, { type = "coreclr", name = "attach - netcoredbg", request = "attach", processId = require('dap.utils').pick_process, -- 若调试服务器不在本地,添加以下两行 -- serverAddress = "localhost", -- serverPort = 4711, }, }
3. 正确启动调试服务器
启动时明确指定监听端口(示例为4711),确保nvim-dap能正确连接:
/usr/local/netcoredbg --interpreter=vscode --server 4711 -- dotnet run -c Debug
错误原因说明
- 第一个错误是因为netcoredbg的VSCode协议要求传入
processId字段,原配置用了pid,导致协议解析失败。 - 第二个错误是因为错误地配置了
coreclrattach作为server类型适配器,nvim-dap直接尝试连接该端口,但netcoredbg的--server模式是等待调试客户端通过netcoredbg发起附加请求,而非直接接受TCP连接。
内容的提问来源于stack exchange,提问作者Kai Walter
相关产品推荐
相关产品推荐

