Neovim — Debugging Application

Debugging application written in Python, Golang, Rust and Javascript/Typescript.

Neovim Debugging Application

Overview

In previous article on Neovim LSP and DAP, I setup DAP using nvim-dap. In this article, let’s set up debugger for Python, Golang, Rust and Javascript/Typescript using vimspector.

Setup

Plugin Installation

I am going to leverage the dotfiles I configured in my previous article.

Inlua/plugins.lua, add the following line to install vimspector. You can use whatever plugin manager that you prefer.

-- Debugging
use { 'puremourning/vimspector' }

Run :luafile % and :PackerInstall to install the plugin.

Adapter Installation

Create a file called vimspector.vim under the plugin folder with the following content.

let g:vimspector_enable_mappings = 'HUMAN'nmap <leader>vl :call vimspector#Launch()<CR>
nmap <leader>vr :VimspectorReset<CR>
nmap <leader>ve :VimspectorEval
nmap <leader>vw :VimspectorWatch
nmap <leader>vo :VimspectorShowOutput
nmap <leader>vi <Plug>VimspectorBalloonEval
xmap <leader>vi <Plug>VimspectorBalloonEval
let g:vimspector_install_gadgets = [ 'debugpy', 'vscode-go', 'CodeLLDB', 'vscode-node-debug2' ]

Below are the adapters used for each language.

Make sure you have the dependencies installed, e.g. vscode-go needs delve to be installed.

Run :VimspectorInstall and the 4 adapters should be installed.

Vimspector Adapter Installation

Run :VimspectorToggleLog to see location of VIMSPECTOR_HOME and gadgetDir.

Vimspector Log

I configure HUMAN mode so that I can use Function keys for debugging, e.g. F5 to start debugging, F9 to toggle breakpoint, F10to step over.

Python Debugging

Under the project root folder, create a file called .vimspector.json with the following content. Change the Python path accordingly.

{
"configurations": {
"<name>: Launch": {
"adapter": "debugpy",
"configuration": {
"name": "Python: Launch",
"type": "python",
"request": "launch",
"python": "~/miniconda3/bin/python",
"stopOnEntry": true,
"console": "externalTerminal",
"debugOptions": [],
"program": "${file}"
}
}
}
}

Press F9 to toggle breakpoint and F5 to start debugging.

Python Debugging

Golang Debugging

Under the project root folder, create a file called .vimspector.json with the following content. Do change the dlvToolPath path accordingly.

{
"configurations": {
"run": {
"adapter": "vscode-go",
"configuration": {
"request": "launch",
"program": "${fileDirname}",
"mode": "debug",
"dlvToolPath": "$HOME/workspace/development/go-lang/bin/dlv"
}
}
}
}

Press F9 to toggle breakpoint and F5 to start debugging.

Golang Debugging

Rust Debugging

Under the project root folder, create a file called .vimspector.json with the following content. Do change the program path accordingly.

{
"configurations": {
"launch": {
"adapter": "CodeLLDB",
"configuration": {
"request": "launch",
"program": "${workspaceRoot}/target/debug/myapp1"
}
}
}
}

Press F9 to toggle breakpoint and F5 to start debugging.

Rust Debugging

Javascript Debugging

Under the project root folder, create a file called .vimspector.json with the following content. Do change the program file accordingly.

{
"configurations": {
"run": {
"adapter": "vscode-node",
"configuration": {
"request": "launch",
"protocol": "auto",
"stopOnEntry": true,
"console": "integratedTerminal",
"program": "${workspaceRoot}/hello.js",
"cwd": "${workspaceRoot}"
}
}
}
}

Press F9 to toggle breakpoint and F5 to start debugging.

Javascript Debugging

Integration with telescope-vimspector

You can now integrate vimspector with telescope-vimspector.

In lua/plugins.lua, add the line below to install the plugin.

use { 'nvim-telescope/telescope-vimspector.nvim' }

Run :luafile % and :PackerInstall to install the plugin.

I also added the following mapping in plugin/vimspector.vim.

" Integration with telescope.nvim
nmap <leader>vc :lua require('telescope').extensions.vimspector.configurations()<CR>

Now when I press <Leader>vc I can see my .vimspector.json launch configurations and select which one I want to debug.

telescope-vimspector

Summary

It is very easy to use vimspector to debug your application. It is stable and easy to configure. If you are looking for a plugin to help you debugging, try it out!

The dotfiles that I used are available in this repository.

Do check out the following articles!

Programmer and occasional blogger.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store