使用Visual Studio Code和Delve调试器在Go中使用标签进行调试

huangapple go评论114阅读模式
英文:

Debugging Go with tags in Visual Studio Code and Delve debugger

问题

根据putus的答案,我找到了以下配置,可以实现一键构建和调试。

首先,你需要添加一个任务来使用相应的标签构建二进制文件。

{
  "version": "0.1.0",
  "command": "bash",
  "isShellCommand": true,
  "args": [""],
  "showOutput": "always",
  "tasks": [
    {
      "taskName": "buildBinWithTag",
      "command": "go",
      "args": ["build", "-o", "BinaryName", "-tags", "THISISATAG"],
      "isShellCommand": true
    }
  ]
}

这个任务应该在调试器启动之前执行。

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

原始问题:我正在使用构建标签来编译不同版本的Go程序,并使用"go build -tags THISISAFLAG"进行编译。

//+build THISISAFLAG

package main

这个方法完美地工作。但是有没有办法告诉调试器使用这些标签?我尝试使用以下的启动配置,但没有成功。

{
  "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
    }
  ]
}
英文:

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
    }
  ]
}

答案1

得分: 6

Visual Studio Code Go插件现在支持一个名为buildFlagslaunch.json键,允许您使用相应值"-tags Tag"指定构建标签(存在一个不允许多个标签的错误)。

插件Wiki中相关的摘录如下:

如果您的构建需要构建标签(例如go build -tags whatever_tag),则添加参数buildFlags,内容为"-tags whatever_tag"

如果您有不同的构建配置,每个配置都需要自己的构建标签,您可以为每个配置创建单独的启动配置。

英文:

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.

答案2

得分: 1

你可以将预构建的二进制文件附加到调试器上。

  1. 通过命令行构建应用程序,例如 go build -o myapp.exe -tags THISISAFLAG
  2. launch.json 中添加配置 Launch Exe
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Debug",  //现有配置
      "type": "go",
      "request": "launch",
      "mode": "debug",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${fileDirname}",
      "env": {},
      "args": [],
      "showLog": true
    },
    {
      "name": "Launch EXE",    //新增配置
      "type": "go",
      "request": "launch",
      "mode": "exec",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${workspaceRoot}/myapp.exe",
      "env": {},
      "args": [],
      "showLog": true
    }
  ]
}

注意:

由于编译器优化和此问题,在调试会话期间某些变量可能不会显示或以不同的名称显示(如下图所示)。将来,您可以在构建应用程序时添加 -gcflags='-N -l' 来禁用编译器优化。

使用Visual Studio Code和Delve调试器在Go中使用标签进行调试

英文:

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.

使用Visual Studio Code和Delve调试器在Go中使用标签进行调试

答案3

得分: 0

以下是我的测试配置:

{
  "name": "Delve: Test",
  "type": "go",
  "request": "launch",
  "mode": "test",
  "buildFlags": "-tags 'unit_tests integration_tests all_tests'",
  "program": "${file}",
  "showLog": true
}
  1. 除非所选文件中不存在这些标签,否则它将持续出错。
  2. 根据您的用例,您可能需要将"${file}"更改为"${fileDirname}",如果您使用后者,至少一个文件应该具有所提到的标签。
英文:

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.

huangapple
  • 本文由 发表于 2017年4月24日 15:20:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/43582024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定