Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add debug_config options #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ lua require('dap-go').setup {
-- ignored by delve in dap mode.
build_flags = "",
},

-- When debugging tests, nvim-dap-go will use a custom launch configuration
-- not included in `dap_configurations`.
-- You can use the following option to add extra configuration arguments
-- for when running tests in debug mode.
-- This table is merged with the default debug launch configuration,
-- overriding any existing values.
extra_debug_opts = {
env = {
MY_ENV_VAR = "value",
},
cwd = "${workspaceFolder}",
}
}
```

Expand All @@ -90,11 +103,12 @@ lua require('dap-go').setup {

### Debugging individual tests


To debug the closest method above the cursor use you can run:

- `:lua require('dap-go').debug_test()`

Once a test was run, you can simply run it again from anywhere:

- `:lua require('dap-go').debug_last_test()`

It is better to define a mapping to invoke this command. See the mapping section below.
Expand All @@ -112,6 +126,7 @@ It is better to define a mapping to invoke this command. See the mapping section
### Debugging with dlv in headless mode

1. Register a new option to attach to a remote debugger:

```lua
lua require('dap-go').setup {
dap_configurations = {
Expand All @@ -124,10 +139,13 @@ lua require('dap-go').setup {
},
}
```

1. Start `dlv` in headless mode. You can specify subcommands and flags after `--`, e.g.,

```sh
dlv debug -l 127.0.0.1:38697 --headless ./main.go -- subcommand --myflag=xyz
```

1. Call `:lua require('dap').continue()` to start debugging.
1. Select the new registered option `Attach remote`.

Expand Down
12 changes: 8 additions & 4 deletions lua/dap-go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,26 @@ end
function M.setup(opts)
local config = vim.tbl_deep_extend("force", default_config, opts or {})
M.test_buildflags = config.delve.build_flags
M.extra_debug_opts = config.extra_debug_opts
local dap = load_module("dap")
setup_delve_adapter(dap, config)
setup_go_configuration(dap, config)
end

local function debug_test(testname, testpath, build_flags)
local function debug_test(testname, testpath, build_flags, extra_debug_opts)
local dap = load_module("dap")
dap.run({
local run_config = {
type = "go",
name = testname,
request = "launch",
mode = "test",
program = testpath,
args = { "-test.run", "^" .. testname .. "$" },
buildFlags = build_flags,
})
}
run_config = vim.tbl_deep_extend("force", run_config, extra_debug_opts or {})
vim.notify(vim.inspect(extra_debug_opts))
dap.run(run_config)
end

function M.debug_test()
Expand All @@ -155,7 +159,7 @@ function M.debug_test()

local msg = string.format("starting debug session '%s : %s'...", test.package, test.name)
vim.notify(msg)
debug_test(test.name, test.package, M.test_buildflags)
debug_test(test.name, test.package, M.test_buildflags, M.extra_debug_opts)

return true
end
Expand Down
Loading