David
David

Reputation: 413

Debugging Go with tags in Visual Studio Code and Delve debugger

Answer: Based on putus answer, I figured out the following configuration to build and debug with one click

At first you need to add a task to build the binary with the respective tags.

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "0.1.0",
  "command": "bash",
  "isShellCommand": true,
  "args": [""],
  "showOutput": "always",
  "tasks": [
        {
            "taskName": "buildBinWithTag",
            "command": "go",
            "args": ["build", "-o", "BinaryName", "-tags", "THISISATAG"],
            "isShellCommand": true            
        }       
    ]
}

This task should be executed before the debugger launches.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "DebugBinWithTag",    //added config
      "type": "go",
      "request": "launch",
      "mode": "exec",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${workspaceRoot}/BinaryName",
      "env": {},
      "args": [],
      "showLog": true,
      "preLaunchTask": "buildBinWithTag"
    }
  ]
} 

Original question:I'm using build tags for compiling different versions of a Go program and I compile it with "go build -tags THISISAFLAG"

//+build THISISAFLAG

package main

This works perfectly. But is there a way to tell the debugger to use these flags. I've tried to use a launch configuration like the following, but it didn't work.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch",
      "mode": "debug",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${fileDirname}",
      "env": {},
      "args": ["-flags THISISAFLAG"],
      "showLog": true
    }
  ]
}

Upvotes: 4

Views: 7406

Answers (3)

atb00ker
atb00ker

Reputation: 1105

Here are my test configurations:

{
  "name": "Delve: Test",
  "type": "go",
  "request": "launch",
  "mode": "test",
  "buildFlags": "-tags 'unit_tests integration_tests all_tests'",
  "program": "${file}",
  "showLog": true
}
  1. It will keep erroring out unless the tags don't exist in the selected file.
  2. You may want to change "${file}" to "${fileDirname}" depending on your usecase, if you are using the latter, at least one of the files should have the mentioned tag.

Upvotes: 2

jpap
jpap

Reputation: 964

The Visual Studio Code Go plugin now supports a launch.json key called buildFlags that allows you to specify the build tags with a corresponding value of "-tags Tag". (There appears to be a bug disallowing multiple tags.).

Relevant excerpt from the plugin Wiki:

If your build needs build tags (e.g. go build -tags whatever_tag), then add the parameter buildFlags with the content "-tags whatever_tag".

If you have different build configurations each requiring their own build tag, you can create separate launch configurations for each.

Upvotes: 10

putu
putu

Reputation: 6454

You can attach pre-built binary to debugger.

  1. Build the application from command line, e.g. go build -o myapp.exe -tags THISISAFLAG
  2. Add configuration Launch Exe to launch.json

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Launch Debug",  //existing config
          "type": "go",
          "request": "launch",
          "mode": "debug",
          "remotePath": "",
          "port": 2345,
          "host": "127.0.0.1",
          "program": "${fileDirname}",
          "env": {},
          "args": [],
          "showLog": true
        },
        {
          "name": "Launch EXE",    //added config
          "type": "go",
          "request": "launch",
          "mode": "exec",
          "remotePath": "",
          "port": 2345,
          "host": "127.0.0.1",
          "program": "${workspaceRoot}/myapp.exe",
          "env": {},
          "args": [],
          "showLog": true
        }
      ]
    } 
    

Note:

Due to compiler optimization and this issue, some variables may not being displayed or displayed with different name during debug session (see below). In the future, you may add -gcflags='-N -l' when building the application to disable compiler optimization.

enter image description here

Upvotes: 1

Related Questions