How to properly configure nvim-dap for Golang in neovim?

355 Views Asked by At

I have been trying to setup the delve debugger for my Go projects in neovim using the nvim-dap plugin, but for some reason, it doesn't seem to work.

I have also consulted the installation wiki for info on how to setup DAP properly but to no avail.

Here's my DAP config: (I am using lazy.nvim as my package manager.)

-- Path: "~/.config/lua/plugins/dap-config.lua"

return {
    "mfussenegger/nvim-dap",
    dependencies = {
        "rcarriga/nvim-dap-ui",
        "theHamsta/nvim-dap-virtual-text",
        "leoluz/nvim-dap-go",
        "Weissle/persistent-breakpoints.nvim",
    },
    config = function()
        local dap = require("dap")

        -- Setup the go debug adapter
        require("dap-go").setup()

        -- Setup DAP virtual text
        require("nvim-dap-virtual-text").setup({})
        vim.g.dap_virtual_text = true

        -- Allows breakpoints to last between sessions
        require("persistent-breakpoints").setup({
            load_breakpoints_event = { "BufReadPost" },
        })

        -- Setup DAP UI
        local dapui = require("dapui")
        dapui.setup()

        -- Automatically open the DAP UI when the debugging session begins
        dap.listeners.before.attach.dapui_config = function()
            dapui.open()
        end
        dap.listeners.before.launch.dapui_config = function()
            dapui.open()
        end
        dap.listeners.before.event_terminated.dapui_config = function()
            dapui.close()
        end
        dap.listeners.before.event_exited.dapui_config = function()
            dapui.close()
        end

        -- Adding symbols for breakpoints and such
        vim.fn.sign_define("DapBreakpoint", { text = "", texthl = "", linehl = "", numhl = "" })
        vim.fn.sign_define("DapStopped", { text = "→", texthl = "", linehl = "", numhl = "" })

        -- Keymaps for debugging
        vim.keymap.set("n", "<leader>db", require("persistent-breakpoints.api").toggle_breakpoint)
        vim.keymap.set("n", "<leader>dc", dap.continue)
        vim.keymap.set("n", "<leader>dt", dapui.toggle)
    end,
}

But when I try to debug some Go code using <leader>dc, I get ECONNRESET (Error: Connection Reset) and dlv exited with code 2:

1

I also get this message in my dap.log.txt file:

[ INFO ] 2024-02-27T14:19:28Z+0530 ] ...ppData/Local/nvim-data/lazy/nvim-dap/lua/dap/session.lua:1789 ] "Session closed due to disconnect"
0

There are 0 best solutions below