/nvim-dap-go

An extension for nvim-dap providing configurations for launching go debugger (delve) and debugging individual tests

Primary LanguageLua

nvim-dap-go

An extension for nvim-dap providing configurations for launching go debugger (delve) and debugging individual tests.

Features

  • Auto launch Delve. No configuration needed. You just have to have dlv in your path.
  • Run just the closest test from the cursor in debug mode (uses treesitter). See debugging individual tests section bellow for more details.
  • Configuration to attach nvim-dap and Delve into a running process and start a debug session.
  • Configuration to start a debug session in the main function.
  • Configuration to run tests in a debug session.

Pre-reqs

This plugin extension make usage of treesitter to find the nearest test to debug. Make sure you have the Go treesitter parser installed. If using nvim-treesitter plugin you can install with :TSInstall go.

Installation

  • Install like any other neovim plugin:

Usage

Register the plugin

Call the setup function in your init.vim to register the go adapter and the configurations to debug go tests:

lua require('dap-go').setup()

Use nvim-dap as usual

  • Call :lua require('dap').continue() to start debugging.
  • All pre-configured debuggers will be displayed for you to choose from.
  • See :help dap-mappings and :help dap-api.

Debugging individual tests

To debug the closest method above the cursor use you can run:

  • :lua require('dap-go').debug_test()

It is better to define a mapping to invoke this command. See the mapping section bellow.

Attaching to a Remote Debuggee / Server

You can attach to a remote debug server using :lua require('dap').continue() and selecting the pre-configured debug option of Attach to Remote Server.

The default port is set to 9999. Use the following config to override it:

  • :lua require('dap-go').set_remote_debug_port(<port>)

The default remote host is "127.0.0.1". Set the following config to override it:

  • :lua require('dap-go').set_remote_debug_host('hostname')

Debugging with command-line arguments

  1. Select the option Debug (Arguments)
  2. Enter each argument separated by a space (i.e. option1 option2 option3)
  3. Press enter

Start Debug Session with Arguments Enter Arguments Begin Debugging

Mappings

nmap <silent> <leader>td :lua require('dap-go').debug_test()<CR>

Acknowledgement

Thanks to the nvim-dap-python for the inspiration.