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

Debug sub folder in Go project not work as expected #509

Closed
thuan1412 opened this issue Apr 10, 2022 · 7 comments
Closed

Debug sub folder in Go project not work as expected #509

thuan1412 opened this issue Apr 10, 2022 · 7 comments

Comments

@thuan1412
Copy link

thuan1412 commented Apr 10, 2022

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.

@thuan1412
Copy link
Author

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.

@adinb
Copy link

adinb commented Apr 11, 2022

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.

@thuan1412
Copy link
Author

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

@thuan1412
Copy link
Author

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.

@adinb
Copy link

adinb commented Apr 11, 2022

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

@thuan1412
Copy link
Author

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

@jeffdupont
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants