简体   繁体   中英

How can I setup vscode for Go project built with bazel?

I am trying to browse code for gVisor<\/a> with VScode.

Maybe this prevents gVisor<\/code> packages from being searched by VSCode go extension. Go-to-definition is not working in most cases, except for cases where the definition can be found under the same directory.

Especially gVisor. Thanks!

gVisor recently added a gopath BUILD rule that creates a canonical GOPATH tree from the source.

You may be able to use that the edit more effectively from VScode.

The linked gVisor rule now proxies a more canonical implementation

This setup worked for me:

  1. In your workspace's root BUILD file you can add the following build rule
# in BUILD.bazel
load("@io_bazel_rules_go//go:def.bzl", "go_path")

go_path(
    name = "gopath",
    mode = "link",
    deps = [
        "//my/binary/here",
        "//any/other/binaries/you/want/linked",
    ],
)

(if you don't yet have a gazelle import for the bazelbuild/go_rules, you would need to import it for bazel)

# in WORKSPACE
http_archive(
    name = "io_bazel_rules_go",
    sha256 = "8e968b5fcea1d2d64071872b12737bbb5514524ee5f0a4f54f5920266c261acb",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.28.0/rules_go-v0.28.0.zip",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.28.0/rules_go-v0.28.0.zip",
    ],
)
  1. Build this command to create a symlinked folder in your blaze-out at bazel-bin/gopath that contains links to each of your dependencies. You'll have to do this any time you add a new dependency. You will see a line for each symlink created.
bazel build :gopath
  1. (assuming you're using VSCode with the Golang extension ) Set your workspace settings for the go extension to point to this gopath. Note you'll need to have it be a worspace trusted extension in order for this to work.
// In .vscode/settings.json
{
    "go.gopath": "/YOUR ABSOLUTE PATH TO YOUR WORKSPACE//bazel-bin/gopath"
}
  1. Restart VSCode

  2. Enjoy!

NOTE: if you have a go.mod file in your root dir, this will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM