tl;dr: Follow this guide for your editor (has instructions for NeoVim, VSCode, Sublime Text, Helix Code): https://github.com/bazel-contrib/rules_go/wiki/Editor-setup
This solution does not require adding additional rules to your Bazel project. As long as you've loaded the @rules_go
repository (which you presumably have to work with Go files), all you need to do is add a shell script somewhere for the language server to call into.
Also spent a lot of time digging around for this. It's annoying and surprisingly hard to find the solution! Thankfully it also annoyed other people: https://github.com/bazel-contrib/rules_go/issues/512
That Github solution explains that the Go tooling team has built up tooling for this a while ago. Basically any editor that uses the gopls
language server can use this solution. The gopls
editor is setup such that the binary it calls into for package resolution can be modified to point at a custom user script. The Go Bazel rules expose a target which will resolve Bazel built Go packages appropriately.