-
Notifications
You must be signed in to change notification settings - Fork 84
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
Set cwd for dlv #80
Comments
This comment has been minimized.
This comment has been minimized.
Hi! I am not opposed to add a configuration in nvim-dap-go to make it better collaborate with your plugin as long as it doesn't bring a big complexity to the project and is compatible with what we are trying to achieve. Your proposal is in sync with these terms and seems reasonable to me. However I'd like understand a bit better why this change is needed. You are changing the work directory used to start the delve process in DAP mode. In my understanding, this directory shouldn't matter as clients need to connect to the DAP server and request the binary or attach to a process. Can you please clarify why that is required and why your plugin needs to change that? |
Sounds very sane, and I support this kind of thinking too, not only with my open source projects. 👍😄
Of course! The short version is that if you work with monorepos and happen to have one or several Go projects (folder with go.mod) that is somewhere below the current working directory, $ pwd
/Users/fredrik/code/public/go-playground/nested_projects
$ tree
.
└── sub_project
├── go.mod
├── main.go
└── main_test.go
$ go test -v ./...
pattern ./...: directory prefix . does not contain main module or its selected dependencies Since $ cd sub_project
$ go test -v ./...
=== RUN TestSum
--- PASS: TestSum (0.00s)
PASS
ok github.com/fredrikaverpil/go-playground/nested_projects/sub_project (cached) So, in essence, if you launch neovim from your git monorepo root, neotest will not be able to execute the Go tests that resides in a Go project that is located further down the folder tree. I've mitigated this completely in the Now, on to DAP. I want to leverage nvim-dap-go as it defines how to configure and launch the delve server and I wish to debug a test by executing Since I'm providing the DAP strategy (here), it would've made a lot of sense to also provide the cwd for the adapter server here. But unfortunately, that's not how the DAP server config can be configured, it seems. So all in all, this is the only way I've managed to get tests and tests debugging to work, when your neovim instance is started outside the actual Go project; to set the |
Thank you for the detailed explanation! 👏🏻 It is clear to me what you are trying to achieve now.
That being said, it shouldn't matter in which folder you start the debug adapter process. What really matters is how the DAP client launch and provides the information about the debugee to the debug adapter. In #81 you are defining the There is a similar discussion here: mfussenegger/nvim-dap#509. It seems that if you just set the |
@leoluz yes, I tried Full DAP log
Debug console error, which details the problem:
This is the error ☝️ I get in all cases, except for when I set the If you'd like to try and repro this on your end;
local dap_config = {
type = "go",
name = "Neotest-golang",
request = "launch",
mode = "test",
program = "${workspaceFolder}/sub_project", -- or "${fileDirname}"
args = { "-test.run", "^TestSum$" },
}
I have dap logging set to |
Ok, I think I got it. I took a look at the specification and the delve doc and contrary to what I said before, what actually launches the application is the adapter. In this case, it explains why starting the delve process in the same directory where the go.mod file is located fixes the problem with monorepos. Thinking about nvim-dap-go use-cases, this configuration could be leveraged to allow debugging applications and test in mono-repos where the root dir doesn't have a go.mod file. It would be great to find a dynamic way to provide this path to the Thank you! |
created #85 based on this discussion. |
@leoluz Many thanks for taking your time to consider this change request 🥳 Don't hesitate to get in touch in case you discover a better way to deal with this use case, and I'll be happy to evaluate it together. I wholeheartedly agree it's somewhat backwards right now. |
The neotest-golang adapter now has support for setting and resetting the nvim-dap-go configuration, and will be able to find and debug tests for the monorepo use case 🎉 |
Tried to setup DAP for go following nvim-dap documentation and using delve directly, as described in https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#go-using-delve-directly However, I was struggling to get DAP working and was getting "Error on launch: Failed to launch" errors all the time, when I discovered (via dap-ui) that the defaults of nvim-dap-go expects go.mod to be in the root of the project. At the same time, the "native" neotest adapter for go seems to have the same behavior and does not seem to support projects where go.mod is not in the root. This was originally described here leoluz/nvim-dap-go#80 (comment), and there's an open issue for this in nvim-dap-go: leoluz/nvim-dap-go#85 There is, however, another adapter for go - fredrikaverpil/neotest-golang - which seems to be more flexible and does support projects where go.mod is not in the root. After replacing the adapter and adding configurations as per https://github.com/fredrikaverpil/neotest-golang/?tab=readme-ov-file#example-configuration-debugging, I was finally able to get DAP working for go.
Hi @leoluz and thanks for this very nice project! 👋
I've been having a lot of issues with neotest-go and I've also felt that it was really difficult to try and fix these problems in that codebase. Instead, I took the plunge and dove right down the rabbit hole and wrote my own neotest adapter for Go. I've successfully mitigated a bunch of the problems here: fredrikaverpil/neotest-golang.
Right now, I'm looking to enable debugging of a test which is part of a nested go project. This is doable with nvim-dap-go if we can just somehow pass in
cwd
into the server config, like this: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 = ..., }, options = { initialize_timeout_sec = config.delve.initialize_timeout_sec, }, } end
I've got this working locally ☝️.
It seems to me that the setup options could be extended to support a
cwd
argument, similar to how it currently supports thedetached
argument.Then my neotest adapter can invoke
require("dap-go").setup({ delve = { cwd = ... }})
prior to runningrequire("neotest").run.run({ strategy = "dap" })
. Again, it looks like this could work here, from what I have locally right now.What do you think about all this?
It's not the most beautiful approach, but I really would like to build on top of nvim-dap-go and not have to fork off logic from your project.
Would you accept a PR where
cwd
is another argument that can be passed into the dlv config, as outlined in the diff above?EDIT: here's the PR: #81 and here's a quick POC I threw together (needs cleaning up) to make my neotest adapter make use of this: fredrikaverpil/neotest-golang#3
The text was updated successfully, but these errors were encountered: