ray-x / go.nvim

G'day Nvimer, Joyful Gopher: Discover the Feature-Rich Go Plugin for Neovim
MIT License
2.12k stars 123 forks source link

Unable to debug code when launch.json is present #518

Closed phenpessoa closed 3 days ago

phenpessoa commented 6 days ago

I'm not sure if I'm doing something wrongly here, but I'm unable to debug my code if launch.json is present. If there is no launch.json file, then I can debug just fine.

I place my cursor on my main function, and simply run :GoDebug. It works. However, if I run :GoDbgConfig, which creates the launch.json file and then try to run :GoDebug again on the main function, I get the following error:

no test file found for /home/phenpessoa/dev/nubank/takehome/cmd/main_test.go
Error executing Lua callback: ...phenpessoa/.local/share/nvim/lazy/go.nvim/lua/go/dap.lua:571: attempt to call field 'go_select' (a nil value)
stack traceback:
        ...phenpessoa/.local/share/nvim/lazy/go.nvim/lua/go/dap.lua:571: in function 'run'
        ...essoa/.local/share/nvim/lazy/go.nvim/lua/go/commands.lua:62: in function <...essoa/.local/share/nvim/lazy/go.nvim/lua/go/commands.lua:57>

I didn't change anything in the launch.json file. I'm testing it with just how it comes from :GoDbgConfig.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "remotePath": "",
            "port": 38697,
            "host": "127.0.0.1",
            "program": "${workspaceFolder}",
            "env": {
            },
            "args": [],
            "cwd": "${workspaceFolder}",
            "envFile": "${workspaceFolder}/.env",
            "buildFlags":""
        },
        {
            "name": "Debug current package",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 38697,
            "host": "127.0.0.1",
            "program": "${fileDirname}",
            "env": {
            },
            "args": [],
            "cwd": "${workspaceFolder}",
            "envFile": "${workspaceFolder}/.env",
            "buildFlags":""
        },
        {
            "name": "Launch test function",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "program": "${workspaceFolder}",
            "args": [
                "-test.run",
                "MyTestFunction"
            ]
        },
        {
            "name": "Attach main",
            "type": "go",
            "request": "attach",
            "mode": "debug",
            "remotePath": "",
            "port": 38697,
            "host": "127.0.0.1",
            "program": "${workspaceFolder}/main.go",
            "env": {
            },
            "args": [],
            "cwd": "${workspaceFolder}",
            "processId":"",
            "envFile": "${workspaceFolder}/.env",
            "buildFlags":""
        },
        {
            "name": "Attach to Process",
            "type": "go",
            "request": "attach",
            "mode": "local",
            "processId": 0
        },
        {
            "name": "Launch file",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${file}"
        }
    ]
}

If I delete the launch.json file I'm able to debug my code again. Any help will be greatly appreciated, thanks.

Neovim version:

NVIM v0.10.2
Build type: Release
LuaJIT 2.1.1713484068

My config:

return {
    {
        "ray-x/go.nvim",
        dependencies = { -- optional packages
            "ray-x/guihua.lua",
            "neovim/nvim-lspconfig",
            "nvim-treesitter/nvim-treesitter",
        },
        config = function()
            require("go").setup({
                tag_options = "",
            })
        end,
        event = { "CmdlineEnter" },
        ft = { "go", "gomod" },
        build = ':lua require("go.install").update_all_sync()',
        keys = {
            {
                "<leader>gsj",
                "<cmd> GoAddTag json <CR>",
                desc = "Add json struct tags",
            },
            {
                "<leader>gsy",
                "<cmd> GoAddTag yaml <CR>",
                desc = "Add yaml struct tags",
            },
            {
                "<leader>gfs",
                "<cmd> GoFillStruct <cr>",
                desc = "Go Fill Struct",
            },
        },
    },
}

I'm running commit 3ea80d7.

ray-x commented 6 days ago

Thanks for reporting this issue. Pushed a fix addressing this. Pls give it a try. Thx!

phenpessoa commented 6 days ago

Thanks @ray-x !

I tested the latest commit and now I'm getting a different error:

Error executing Lua callback: ...soa/.local/share/nvim/lazy/guihua.lua/lua/guihua/gui.lua:243: on_choice: expected function, got nil
stack traceback:
        [C]: in function 'error'
        vim/shared.lua: in function 'validate'
        ...soa/.local/share/nvim/lazy/guihua.lua/lua/guihua/gui.lua:243: in function 'select'
        ...phenpessoa/.local/share/nvim/lazy/go.nvim/lua/go/dap.lua:571: in function 'run'
        ...essoa/.local/share/nvim/lazy/go.nvim/lua/go/commands.lua:62: in function <...essoa/.local/share/nvim/lazy/go.nvim/lua/go/commands.lua:57>
ray-x commented 5 days ago

The issue should be gone now. Could you give it another try? Thx!

phenpessoa commented 5 days ago

@ray-x This is still erroring out for me. I get this error now:

E5108: Error executing lua: ...e/phenpessoa/.local/share/nvim/lazy/nvim-dap/lua/dap.lua:566: dap.run() must be called with a valid configuration, got nil
stack traceback:
        [C]: in function 'assert'
        ...e/phenpessoa/.local/share/nvim/lazy/nvim-dap/lua/dap.lua:566: in function 'run'
        ...phenpessoa/.local/share/nvim/lazy/go.nvim/lua/go/dap.lua:572: in function 'on_confirm'
        ...l/share/nvim/lazy/guihua.lua/lua/guihua/listviewctrl.lua:485: in function 'on_confirm'
        ...l/share/nvim/lazy/guihua.lua/lua/guihua/listviewctrl.lua:110: in function <...l/share/nvim/lazy/guihua.lua/lua/guihua/listviewctrl.lua:110>

But I think I found the issue. If I change these lines (./lua/go/dap.lua 571-573):

vim.ui.select(launch_names, { prompt = 'which you would like to debug' }, function(index)
        dap.run(dap.configurations.go[index])
end)

To this:

  vim.ui.select(launch_names, { prompt = 'which you would like to debug' }, function(name)
    local index = 0
    for i, c in ipairs(dap.configurations.go) do
      if c.name == name then
        index = i
        break
      end
    end
    dap.run(dap.configurations.go[index])
  end)

The issue is fixed and I'm able to debug. However I'm not sure if this is the best solution.

ray-x commented 5 days ago

Thanks for the update. the select should accept idx as parameter. there is something wrong with guihua implementation. Please update both go.nvim and guihua.lua. It should work without your hack.

phenpessoa commented 5 days ago

Thanks! Can confirm that with the latest version of guihua and go.nvim the error is gone. I'm able to debug fine now! :tada:

ray-x commented 3 days ago

Cheers!