From 9959cb55a5e42f55cb9d98f8b4534512cb869bfe Mon Sep 17 00:00:00 2001 From: mcoqzeug Date: Sun, 22 Sep 2024 21:07:53 +0800 Subject: [PATCH 1/3] set cwd for dlv based on program This potentially resolves #85. vscode-go sets dlv cwd based on the value of program. Reference: https://github.com/golang/vscode-go/blob/master/extension/src/goDebugConfiguration.ts#L545 --- lua/dap-go.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lua/dap-go.lua b/lua/dap-go.lua index 6e125a9..f808059 100644 --- a/lua/dap-go.lua +++ b/lua/dap-go.lua @@ -81,6 +81,21 @@ local function setup_delve_adapter(dap, config) } dap.adapters.go = function(callback, client_config) + if client_config.program ~= nil then + local program = client_config.program + local path = require("plenary.path") + local program_path = path:new(program) + local program_absolute = program_path:absolute() + + client_config.program = program_absolute + + if program_path:is_dir() then + delve_config.executable.cwd = program_absolute + elseif program:match("^.+(%..+)$") == ".go" then -- file extension is '.go' + delve_config.executable.cwd = tostring(program_path:parent()) + end + end + if client_config.port == nil then callback(delve_config) return From b49e81a6735ebc76eb2cf2be3efdf01582dbb74b Mon Sep 17 00:00:00 2001 From: jxl Date: Sun, 6 Oct 2024 00:24:44 +0800 Subject: [PATCH 2/3] replace plenary.path with vim.loop functions --- lua/dap-go.lua | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/lua/dap-go.lua b/lua/dap-go.lua index f808059..1f0ad2c 100644 --- a/lua/dap-go.lua +++ b/lua/dap-go.lua @@ -62,6 +62,25 @@ local function filtered_pick_process() return require("dap.utils").pick_process(opts) end +local get_parent = (function() + local sep = "\\" + + local os = string.lower(jit.os) + if os ~= "windows" then + sep = "/" + end + + local pattern = string.format("^(.+)%s[^%s]+", sep, sep) + + return function(abs_path) + local parent = abs_path:match(pattern) + if parent ~= nil and not parent:find(sep) then + return parent .. sep + end + return parent + end +end)() + 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) @@ -82,17 +101,20 @@ local function setup_delve_adapter(dap, config) dap.adapters.go = function(callback, client_config) if client_config.program ~= nil then - local program = client_config.program - local path = require("plenary.path") - local program_path = path:new(program) - local program_absolute = program_path:absolute() - - client_config.program = program_absolute - - if program_path:is_dir() then - delve_config.executable.cwd = program_absolute - elseif program:match("^.+(%..+)$") == ".go" then -- file extension is '.go' - delve_config.executable.cwd = tostring(program_path:parent()) + local program_absolute = vim.loop.fs_realpath(client_config.program) + + if program_absolute ~= nil then + client_config.program = program_absolute + + local is_dir = vim.loop.fs_stat(program_absolute).type == "directory" + if is_dir then + delve_config.executable.cwd = program_absolute + elseif program_absolute:sub(-3) == ".go" then -- file extension is '.go' + local parent = get_parent(program_absolute) + if parent ~= nil then + delve_config.executable.cwd = parent + end + end end end From 6c292874c8964712c1718a6370652f323cfbd630 Mon Sep 17 00:00:00 2001 From: jxl Date: Thu, 17 Oct 2024 17:18:26 +0800 Subject: [PATCH 3/3] use fnamemodify to get parent path instead of writing our own function --- lua/dap-go.lua | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/lua/dap-go.lua b/lua/dap-go.lua index 1f0ad2c..caaee92 100644 --- a/lua/dap-go.lua +++ b/lua/dap-go.lua @@ -62,25 +62,6 @@ local function filtered_pick_process() return require("dap.utils").pick_process(opts) end -local get_parent = (function() - local sep = "\\" - - local os = string.lower(jit.os) - if os ~= "windows" then - sep = "/" - end - - local pattern = string.format("^(.+)%s[^%s]+", sep, sep) - - return function(abs_path) - local parent = abs_path:match(pattern) - if parent ~= nil and not parent:find(sep) then - return parent .. sep - end - return parent - end -end)() - 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) @@ -109,8 +90,8 @@ local function setup_delve_adapter(dap, config) local is_dir = vim.loop.fs_stat(program_absolute).type == "directory" if is_dir then delve_config.executable.cwd = program_absolute - elseif program_absolute:sub(-3) == ".go" then -- file extension is '.go' - local parent = get_parent(program_absolute) + elseif vim.fn.fnamemodify(program_absolute, ":e") == ".go" then -- file extension is '.go' + local parent = vim.fn.fnamemodify(program_absolute, ":p:h") if parent ~= nil then delve_config.executable.cwd = parent end