在VScode中为Golang构建应用程序安装Bazel

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

Install Bazel for Golang build applications in VScode

问题

如何在VScode中为Golang应用程序构建安装Bazel?

英文:

How to install bazel for golang application building in VScode?

答案1

得分: 3

目前还没有直接支持Bazel的功能,根据golang/go issue 37205("x/tools/gopls: support for bazel-based projects"),还没有对Bazel提供直接支持。

bazelbuild/rules_go wiki/Editor-and-tool-integration中有一个用于构建编辑器和工具集成的设计文档:一个新的gopackagesdriver二进制文件,将Bazel+rules_go与golang.org/x/tools/go/packages集成。但是它还没有完全实现(2022年第一季度)。

在此期间,您可以按照Bazel IDE设置进行操作,包括:

  • 创建一个启动脚本,比如tools/gopackagesdriver.sh:
#!/usr/bin/env bash
exec bazel run -- @io_bazel_rules_go//go/tools/gopackagesdriver "${@}"
  • 在工作区的.vscode/settings.json中添加以下内容:
{
  "go.goroot": "${workspaceFolder}/bazel-${workspaceFolderBasename}/external/go_sdk",
  "go.toolsEnvVars": {
    "GOPACKAGESDRIVER": "${workspaceFolder}/tools/gopackagesdriver.sh"
  },
  "go.enableCodeLens": {
    "references": false,
    "runtest": false
  },
  "gopls": {
    "build.directoryFilters": [
      "-bazel-bin",
      "-bazel-out",
      "-bazel-testlogs",
      "-bazel-mypkg",
    ],
    "formatting.gofumpt": true,
    "formatting.local": "github.com/my/mypkg",
    "ui.completion.usePlaceholders": true,
    "ui.semanticTokens": true,
    "ui.codelenses": {
      "gc_details": false,
      "regenerate_cgo": false,
      "generate": false,
      "test": false,
      "tidy": false,
      "upgrade_dependency": false,
      "vendor": false
    },
  },
  "go.useLanguageServer": true,
  "go.buildOnSave": "off",
  "go.lintOnSave": "off",
  "go.vetOnSave": "off",
}
英文:

There is no direct support for Bazel yet, as per golang/go issue 37205 ("x/tools/gopls: support for bazel-based projects")

bazelbuild/rules_go wiki/Editor-and-tool-integration has a design document for building editor and tool integration: a new gopackagesdriver binary that integrates Bazel+rules_go with golang.org/x/tools/go/packages.
But it is not fully implemented yet (Q1 2022).

In the meantime, you can follow Bazel IDE setup, which involves:

  • Create a launcher script, say tools/gopackagesdriver.sh:
#!/usr/bin/env bash
exec bazel run -- @io_bazel_rules_go//go/tools/gopackagesdriver "${@}"
  • adding in the .vscode/settings.json of your workspace:
{
  "go.goroot": "${workspaceFolder}/bazel-${workspaceFolderBasename}/external/go_sdk",
  "go.toolsEnvVars": {
    "GOPACKAGESDRIVER": "${workspaceFolder}/tools/gopackagesdriver.sh"
  },
  "go.enableCodeLens": {
    "references": false,
    "runtest": false
  },
  "gopls": {
    "build.directoryFilters": [
      "-bazel-bin",
      "-bazel-out",
      "-bazel-testlogs",
      "-bazel-mypkg",
    ],
    "formatting.gofumpt": true,
    "formatting.local": "github.com/my/mypkg",
    "ui.completion.usePlaceholders": true,
    "ui.semanticTokens": true,
    "ui.codelenses": {
      "gc_details": false,
      "regenerate_cgo": false,
      "generate": false,
      "test": false,
      "tidy": false,
      "upgrade_dependency": false,
      "vendor": false
    },
  },
  "go.useLanguageServer": true,
  "go.buildOnSave": "off",
  "go.lintOnSave": "off",
  "go.vetOnSave": "off",
}

huangapple
  • 本文由 发表于 2022年1月11日 14:16:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/70662364.html
匿名

发表评论

匿名网友

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

确定