rules_go
rules_go copied to clipboard
VSCode support for Bazel + Go project doesn't work correctly for new packages
What version of rules_go are you using?
v0.29.0
What version of gazelle are you using?
v0.24.0
What version of Bazel are you using?
4.2.1
Does this issue reproduce with the latest releases of all the above?
I am using the latest versions already.
What operating system and processor architecture are you using?
Intel Mac
Any other potentially useful information about your toolchain?
Here's my .vscode/settings.json
file:
{
"go.goroot": "${workspaceFolder}/bazel-myrepo/external/go_sdk",
"go.toolsEnvVars": {
"GOPACKAGESDRIVER": "${workspaceFolder}/tools/editor/gopackagesdriver.sh"
},
"gopls": {
"formatting.local": "example.com"
}
}
Here's my tools/editor/gopackagesdriver.sh
file:
#!/bin/bash
exec bazel run -- @io_bazel_rules_go//go/tools/gopackagesdriver "$@"
What's working
We have a Go repo that's using Bazel. I am trying to make changes to that repo from VSCode. With the above mentioned files in the repo, "Go to Definition" etc. started working. Thank you! :)
What's not working
However, when I add a new package (along with its BUILD.bazel
file), I can't successfully import it from another package (even after adding the new package in the deps
of the go_library
rule).
The import shows a red squiggly line which says the following on hover:
could not import example.com/nodeinit/newpkg (cannot find package "example.com/nodeinit/newpkg" in any of
/Users/rohit_agarwal/repos/myrepo/bazel-myrepo/external/go_sdk/src/example.com/nodeinit/newpkg (from $GOROOT)
/Users/rohit_agarwal/go/src/example.com/nodeinit/newpkg (from $GOPATH)) compiler(BrokenImport)
If I restart VSCode, the error goes away.
IntelliJ IDEA (GoLand) also has a similar issue with new packages, however there the problem is fixed by Sync Project with BUILD Files
without restarting the IDE.
cc @steeve @linzhp
The error in VSCode also goes away when I do:
- Command Shift P and then
- Search for
Developer: Reload Window
and press Enter
~is the package you're trying to import declared in the BUILD.bazel
target? since the gopackagesdriver forks out to bazel and lets bazel be the source of truth, could it be the reason?
The whole package graph is crawled from the encompassing go_library
for a given .go
file.~
Sorry I just re-read your message!
You can inspect the gopackagesdriver
payload by running this command
echo {} | ./tools/gopackagesdriver.sh file=platform/pkg/myfile.go
Is there any error? Can you inspect the json payload and check that the package you're trying to import is indeed there?
You can inspect the
gopackagesdriver
payload by running this commandecho {} | ./tools/gopackagesdriver.sh file=platform/pkg/myfile.go
I had checked that and it had generated some valid json (that jq
could parse without errors). The new packages I created was present in there and also in the deps of the other package.
I think there's some bug in how reloading new packages work because the error goes away when I restart the Editor or "Reload Window". So somehow the editor continues using some old state and doesn't recognize a newly added package until restart.
Thanks, so the issue is indeed between perhaps gopackagesdriver, gopls and vscode perhaps? For some reason it's not "reloading" the packages? Maybe something with vscode itself ?
Perhaps. Though this is not an issue with regular non-bazel go tooling.
Can you enable gopls tracing see if at least the gopackagesdriver is called "regularly" ?
There's definitely a lot of chatter in the gopls (server)
logs after I set the following in my settings.json file:
"go.languageServerFlags": [
"-rpc.trace"
],
Including messages like:
[Trace - 14:06:59.227 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2021/11/23 14:06:59 background imports cache refresh starting\n"}
[Info - 2:06:59 PM] 2021/11/23 14:06:59 background imports cache refresh starting
[Trace - 14:06:59.229 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2021/11/23 14:06:59 background refresh finished after 1.632319ms\n"}
[Info - 2:06:59 PM] 2021/11/23 14:06:59 background refresh finished after 1.632319ms
[Trace - 14:08:41.961 PM] Sending request 'textDocument/hover - (277)'.
Params: {"textDocument":{"uri":"file:///Users/rohit_agarwal/scratch/mypkg/mypkg.go"},"position":{"line":2,"character":20}}
[Trace - 14:08:41.962 PM] Received response 'textDocument/hover - (277)' in 0ms.
Result: null
[Trace - 14:08:42.127 PM] Sending request 'textDocument/codeAction - (278)'.
Params: {"textDocument":{"uri":"file:///Users/rohit_agarwal/scratch/mypkg/mypkg.go"},"range":{"start":{"line":2,"character":7},"end":{"line":2,"character":33}},"context":{"diagnostics":[{"range":{"start":{"line":2,"character":7},"end":{"line":2,"character":33}},"message":"could not import example.com/mypkg/newpkg (cannot find package \"example.com/mypkg/newpkg\" in any of \n\t/Users/rohit_agarwal/scratch/mypkg/bazel-mypkg/external/go_sdk/src/example.com/mypkg/newpkg (from $GOROOT)\n\t/Users/rohit_agarwal/go/src/example.com/mypkg/newpkg (from $GOPATH))","code":"BrokenImport","codeDescription":{"href":"https://pkg.go.dev/golang.org/x/tools/internal/typesinternal?utm_source%3Dgopls#BrokenImport"},"severity":1,"source":"compiler"}],"only":["quickfix"]}}
[Trace - 14:08:42.127 PM] Received response 'textDocument/codeAction - (278)' in 0ms.
Result: null
I'd like to resurface this issue.
I'm seeing the exact same problem with Bazel v0.31.0. This happens not only with new repositories but with each new import added to a file. Makes it really irritating to use VSCode as you constantly need to reload the editor to suppress its warnings.
Does anyone know if there is a solution to this issue?