nvim-dap-go

repository·main·Indexed 20 days ago

https://github.com/leoluz/nvim-dap-go

An extension for nvim-dap that provides specialized configurations for debugging Go applications using Delve (dlv). It simplifies debugging individual tests via Treesitter and provides utilities to pass arguments and build flags during the launch process.

Tokens
1.3K
Snippets
6
Records
6
Agent score
20%

What's inside nvim-dap-go

  1. Debug using VSCode launch.json

    main

    Instead of defining all configurations in Neovim, you can use a .vscode/launch.json file in your project root. nvim-dap will automatically load this file if it exists. This is useful for setting dynamic ports or complex remote attachment settings.

    Example .vscode/launch.json:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Remote debug API server",
                "type": "go",
                "request": "attach",
                "mode": "remote",
                "port": 4444,
                "host": "127.0.0.1",
                "substitutePath": [
                    {
                        "from": "${workspaceFolder}",
                        "to": "/usr/src/app"
                    }
                ]
            }
        ]
    }
  2. Debug individual Go tests

    main

    Use the following commands to debug tests based on your cursor position using Treesitter:

    • require('dap-go').debug_test(): Runs the closest test located above the cursor.
    • require('dap-go').debug_last_test(): Runs the test that was most recently executed.

    It is recommended to map these to keys for easier access.

    " Recommended mapping
    nmap <silent> <leader>td :lua require('dap-go').debug_test()<CR>
  3. Install nvim-dap-go

    main

    Install nvim-dap-go using your preferred Neovim plugin manager.

    Prerequisites

    • Neovim >= 0.9.0
    • nvim-dap installed
    • delve (dlv) >= 1.7.0 in your PATH
    • Go treesitter parser installed (use :TSInstall go if using nvim-treesitter)
    -- Using vim-plug
    Plug 'leoluz/nvim-dap-go'
    
    -- Using packer.nvim
    use 'leoluz/nvim-dap-go'
  4. Configure nvim-dap-go

    main

    You can customize the plugin by passing a configuration table to setup().

    Configuration Keys

    dap_configurations

    A list of tables representing additional nvim-dap configurations. Each entry must have type = "go" to be recognized.

    delve

    Settings for the Delve debugger:

    • path: Path to the dlv executable (defaults to "dlv" on PATH).
    • initialize_timeout_sec: Seconds to wait for Delve to initialize (default: 20).
    • port: Port for the debugger (default: "${port}" for a random available port).
    • args: Additional arguments passed to dlv.
    • build_flags: Build flags passed to Delve (e.g., "-tags=unit"). Note: passing build flags via args is ineffective in dap mode.
    • detached: Whether the dlv process is created detached. Defaults to true on non-Windows systems. On Windows, Delve < 1.24.0 may require this to be false.
    • cwd: Current working directory for running dlv.

    tests

    • verbose: Boolean to enable verbosity when running tests (default: false).
    require('dap-go').setup {
      dap_configurations = {
        {
          type = "go",
          name = "Attach remote",
          mode = "remote",
          request = "attach",
        },
      },
      delve = {
        path = "dlv",
        initialize_timeout_sec = 20,
        port = "${port}",
        args = {},
        build_flags = {},
        detached = vim.fn.has("win32") == 0,
        cwd = nil,
      },
      tests = {
        verbose = false,
      },
    }
  5. Debug with interactive Build Flags and Arguments

    main

    To create custom debug configurations that prompt the user for input during the launch process, attach the following functions to the args or buildFlags fields in your dap_configurations:

    • require('dap-go').get_arguments: Prompts for command-line arguments.
    • require('dap-go').get_build_flags: Prompts for build flags.
    require("dap-go").setup({
        dap_configurations = {
            {
                type = "go",
                name = "Debug (Build Flags & Arguments)",
                request = "launch",
                program = "${file}",
                args = require("dap-go").get_arguments,
                buildFlags = require("dap-go").get_build_flags,
            },
        }
    })