TIL: How to Debug a Test in Golang With Build Tags in Neovim

I was having issues with my debugger today (well technically yesterday because I am publishing this a day later to spread out my blog posts but same difference) and it took me a few hours to realise what was going on. In my case, I was trying to debug a test written in Golang using nvim-dap-go on Neovim.

The reason the test was failing because the test file had build tags so delve (the debugger) couldn’t compile the file, i.e. using the unit build tag in the example below.

snippet.go
//go:build unit
// +build unit

package options_test
go 4 lines
"snippet.go" 4L

When we set up the dag-go plugin, we need to pass an extra field called build_flags:

snippet.lua
lua require('dap-go').setup {
  delve = {
    build_flags = "-tags=unit,integration,e2e",
  },
}
lua 5 lines
"snippet.lua" 5L

Replace the above with the tags you use! If you are using NixVim you should be able to do something like this 1:

snippet.nix
{
  programs.nixvim = {
    plugins = {
      dap.extensions.dap-go = {
        enable = true;
        delve = {
          path = "${pkgs.delve}/bin/dlv";
          buildFlags = "-tags=unit,integration,e2e";
        };
      };
    };
  };
}
nix 13 lines
"snippet.nix" 13L

Then I have a keybinding to debug the nearest test like so, again using NixVim.

snippet.nix
  {
    action = "<cmd> lua require('dap-go').debug_test()<CR>";
    key = "<leader>td";
    options = {
      desc = "Debug Nearest (Go)";
    };
    mode = [
      "n"
    ];
  }
nix 10 lines
"snippet.nix" 10L

in lua:

snippet.lua
vim.keymap.set("n", "<leader>td", "<cmd>lua require('dap-go').debug_test()<CR>")
lua 1 line
"snippet.lua" 1L