windowsgodebuggingneovimneovim-plugin

Error debugging Go code with Neovim and nvim-dap-go on Windows: dlv exited with code 2


I've been encountering issues while trying to debug Go code using Neovim and nvim-dap-go on Windows. I've configured everything according to the documentation, but when I try to start a debug session, I keep getting errors related to dlv.

Here's a summary of my setup and the problem:

I'm using Neovim with nvim-dap and nvim-dap-go plugins for Go debugging. I've installed dlv and configured its path in my Neovim configuration. dlv works fine in dap mode outside of Neovim. I've set up my environment variables (GOPATH, GOROOT, GOBIN) correctly. When I attempt to start a debug session, a command prompt window with go.exe briefly appears, then closes, and I receive the error message:

"Debug adapter didn't respond. Either the adapter is slow (then wait and ignore this) or there is a problem with your adapter or go configuration. Check the logs for errors (
dap.set_log_level)".

:lua require('dap.repl).open()
\`\[debug-adapter stderr\] panic: runtime error: invalid memory address or nil pointer dereference
\[signal 0xc0000005 code=0x0 addr=0x38
\[debug-adapter stderr\]  pc=0x71d062\]

goroutine 12 \[running, locked to thread\]:
github.com/go-delve/delve/pkg/proc/native.(\*nativeProcess).addThread(0x0, 0x250, 0x28f8, 0xb0?, 0x0, 0x0)
/go/pkg/mod/github.com/go-delve/delve@v1.22.1/pkg/proc/native/proc_windows.go:302 +0x42
github.com/go-delve/delve/pkg/proc/native.(\*processGroup).waitForDebugEvent(0xc000444de0, 0x1)
/go/pkg/mod/github.com/go-delve/delve@v1.22.1/pkg/proc/native/proc_windows.go:406 +0x5e7
github.com/go-delve/delve/pkg/proc/native.initialize.func1()
/go/pkg/mod/github.com/go-delve/delve@v1.22.1/pkg/proc/native/proc_windows.go:84 +0x25
github.com/go-delve/delve/pkg/proc/native.(\*ptraceThread).handlePtraceFuncs(0xc000008888)
/go/pkg/mod/github.com/go-delve/delve@v1.22.1/pkg/proc/native/proc.go:405 +0x48
created by github.com/go-delve/delve/pkg/proc/native.newPtraceThread in goroutine 6
/go/pkg/mod/github.com/go-delve/delve@v1.22.1/pkg/proc/native/proc.go:492 +0xc6\`

Here's the content from the dap.log file:

[ DEBUG ] 2024-05-26T18:57:47Z+0500 ] ...ppData/Local/nvim-data/lazy/nvim-dap/lua/dap/session.lua:957 ] 4   {
  body = {
    supportsClipboardContext = true,
    supportsConditionalBreakpoints = true,
    supportsConfigurationDoneRequest = true,
    supportsDelayedStackTraceLoading = true,
    supportsDisassembleRequest = true,
    supportsEvaluateForHovers = true,
    supportsExceptionInfoRequest = true,
    supportsFunctionBreakpoints = true,
    supportsInstructionBreakpoints = true,
    supportsLogPoints = true,
    supportsSetVariable = true,
    supportsSteppingGranularity = true
  },
  command = "initialize",
  request_seq = 1,
  seq = 0,
  success = true,
  type = "response"
}
[ DEBUG ] 2024-05-26T18:57:47Z+0500 ] ...ppData/Local/nvim-data/lazy/nvim-dap/lua/dap/session.lua:1700 ]    "request"   {
  arguments = {
    args = { "-test.run", "^Test_reorderList$" },
    buildFlags = "",
    mode = "test",
    name = "Test_reorderList",
    program = "./linkedlist",
    request = "launch",
    type = "go"
  },
  command = "launch",
  seq = 2,
  type = "request"
}

My Neovim configuration (plugins/init.lua) for nvim-dap-go looks like this:

 {
    "mfussenegger/nvim-dap",
 },

 {
    "leoluz/nvim-dap-go",
    ft = "go",
    dependencies = { "mfussenegger/nvim-dap" },
    config = function(_, opts)
      require("dap-go").setup(opts)
      require('dap').set_log_level('TRACE')
    end,
 },

 {
    "rcarriga/nvim-dap-ui",
    dependencies = {
      "mfussenegger/nvim-dap",
      "nvim-neotest/nvim-nio",
    },
 },

  {
    "theHamsta/nvim-dap-virtual-text",
  },

a command prompt window with go.exe

I've searched online for solutions, but haven't found any that address my specific issue. Has anyone else encountered similar problems while debugging Go with Neovim on Windows? It's worth noting that this configuration works well on Linux Ubuntu, and both my Windows and Linux setups have the same versions of Go, dlv, and Neovim.

I've attempted to reinstall all the plugins, verify my environment settings, and reboot my computer. I expected that after these actions, the issue would disappear, and I'd be able to debug my Go code successfully in Neovim. However, even after restarting Neovim, I still receive the error message and can't initiate debugging. The problem persists. Worth noting, the same configuration worked flawlessly in Linux.


Solution

  • This is a known issue with nvim-dap-go with the default configuration for windows. As can be seen in the configuring part of the docs (that was added in this commit), the detached option must be set to false if you are on windows.

    Your config for windows should look something like:

     {
    "leoluz/nvim-dap-go",
    ft = "go",
    dependencies = { "mfussenegger/nvim-dap" },
    opts = {
        delve = {
            detached = false,
        },
    },
    config = function(_, opts)
      require("dap-go").setup(opts)
      require('dap').set_log_level('TRACE')
    end,
    

    },

    This should allow delve to run correctly when being called from neovim.

    if you need your configuration to be dettached only on windows, implement the solution on this pr.

    Please feel free to try and implement this and get back to me if the solution isn't helpful.