Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode support for Bazel + Go project doesn't work correctly for new packages #3014

Open
rohitagarwal003 opened this issue Nov 23, 2021 · 10 comments
Assignees
Labels
IDE Go package driver and IDE support tools Support for editors, analysis, and refactoring tools

Comments

@rohitagarwal003
Copy link

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

@rohitagarwal003
Copy link
Author

The error in VSCode also goes away when I do:

  • Command Shift P and then
  • Search for Developer: Reload Window and press Enter

@steeve
Copy link
Contributor

steeve commented Nov 23, 2021

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!

@steeve
Copy link
Contributor

steeve commented Nov 23, 2021

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?

@rohitagarwal003
Copy link
Author

You can inspect the gopackagesdriver payload by running this command
echo {} | ./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.

@steeve
Copy link
Contributor

steeve commented Nov 23, 2021

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 ?

@rohitagarwal003
Copy link
Author

Perhaps. Though this is not an issue with regular non-bazel go tooling.

@steeve
Copy link
Contributor

steeve commented Nov 23, 2021

Can you enable gopls tracing see if at least the gopackagesdriver is called "regularly" ?

@rohitagarwal003
Copy link
Author

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

@robfig robfig added the tools Support for editors, analysis, and refactoring tools label Dec 1, 2021
@errb
Copy link

errb commented May 6, 2022

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.

@linzhp linzhp added the IDE Go package driver and IDE support label May 16, 2022
@EByrdS
Copy link

EByrdS commented Sep 19, 2023

Does anyone know if there is a solution to this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IDE Go package driver and IDE support tools Support for editors, analysis, and refactoring tools
Projects
None yet
Development

No branches or pull requests

6 participants