mfussenegger / nvim-dap

Debug Adapter Protocol client implementation for Neovim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Debug sub folder in Go project not work as expected

thuan1412 opened this issue · comments

Debug adapter definition and debug configuration

dap.adapters.go = function(callback, _)
  local stdout = vim.loop.new_pipe(false)
  local handle, pid_or_err
  local port = 38697

  handle, pid_or_err = vim.loop.spawn("dlv", {
    stdio = { nil, stdout },
    args = { "dap", "-l", "127.0.0.1:" .. port },
    detached = true,
  }, function(code)
    stdout:close()
    handle:close()

    print("[delve] Exit Code:", code)
  end)

  assert(handle, "Error running dlv: " .. tostring(pid_or_err))

  stdout:read_start(function(err, chunk)
    assert(not err, err)

    if chunk then
      vim.schedule(function()
        require("dap.repl").append(chunk)
        print("[delve]", chunk)
      end)
    end
  end)

  -- Wait for delve to start
  vim.defer_fn(function()
    callback { type = "server", host = "127.0.0.1", port = port }
  end, 100)
end

dap.configurations.go = {
  {
    type = "go",
    name = "Debug cmd folder",
    request = "launch",
    program = "cmd",
    showLog = true,
    args = { "server" },
  },
}

Debug adapter version

1.8.2

Steps to Reproduce

Here is the structure of my Go project

├── cmd
│   └── main.go
├── go.mod
├── helper.go
└── main.go

The entry point of the project is the cmd/main.go file. To run the project, run go run cmd/*.go command.

Run the debug options named Debug cmd folder.

I have tried the same configuration in the VsCode and it works as expected, however, when I run that configuration on neovim, I got this error

[delve] DAP server listening at: 127.0.0.1:38697
Error on launch: Failed to launch
[delve] Exit Code: 0

Expected Result

The dap works correctly because that configuration works well on VsCode.

VsCode configuration

{
    "name": "Launch cmd",
    "type": "go",
    "request": "launch",
    "mode": "auto",
    "program": "cmd",
}

Actual Result

Error on launch: Failed to launch
[delve] Exit Code: 0

Cannot launch the debug.

I tried to load the configurations from .vscode/launch.json file by using require('dap.ext.vscode').load_launchjs(".vscode/launch.json", { "go" }) and get the same error.

You need to specify the full package path when using Go modules.

If your module path (specified by module directive in the module’s go.mod file) is mymodule, then use mymodule/cmd for the program name in your case.

Alternatively, use filesystem path: ${workspaceFolder}/cmd or ./cmd assuming your module root directory is your Neovim working directory.

I don't use VSCode, so take this with a grain of salt. I'm not sure why the launch.json works on VSCode, but they specified the following in the documentation:

Its program attribute needs to be either the go file or folder of the main package or test file.

Which aligns with the explanation above.

Do you mean set cwd property to ${workspaceFolder}/cmd? I've already tried, but still get the error.

Found the solution.

{
  type = "go",
  name = "Debug cmd folder",
  request = "launch",
  program = "${workspaceFolder}/cmd",
  cwd = "${workspaceFolder}",
  showLog = true,
  args = { "server" }
}

I need to set value to both program and cwd.

I believe cwd = "${workspaceFolder}" is not necessary. But if it's working then that's fine

You're correct, cwd = "${workspacFolder}" is not necessary.

for gloang, cwd should represent where the go.mod files are located... however this does not work. So if your project folder has automation tests in python outside the main golang app folder, you can't have the vscode/launch.json at the root of the project and have it cover all options, it must reside within the app folder to have access to the go.mod file.