gopls returns the error "gopls: no packages returned: packages.Load error" for github.com/Shopify/sarama

3.6k views Asked by At

I've checked out the main branch of github.com/Shopify/sarama (at commit 947343309601b4eb3c2fa3e7d15d701b503dd491 ) but I notice that in VS Code I can't "Go to definition" as usual. If I hover over the package name sarama in functional_consumer_group_test.go, I get the linter warning

No packages found for open file /Users/kurtpeek/go/src/github.com/Shopify/sarama/functional_consumer_group_test.go: <nil>.
If this file contains build tags, try adding "-tags=<build tag>" to your gopls "buildFlags" configuration (see (https://github.com/golang/tools/blob/master/gopls/doc/settings.md#buildflags-string).
Otherwise, see the troubleshooting guidelines for help investigating (https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md).go list

(See screenshot below).

enter image description here

From the command line, if I try to gopls that file, I get a similar error:

> gopls check functional_consumer_group_test.go 
gopls: no packages returned: packages.Load error

I suspect this has something to do with the build constraints (https://pkg.go.dev/cmd/go#hdr-Build_constraints) in that file, from https://github.com/Shopify/sarama/blob/947343309601b4eb3c2fa3e7d15d701b503dd491/functional_consumer_group_test.go#L1-L2,

//go:build functional
// +build functional

It's not clear to me, however, how to modify my VS Code settings.json to pass these build constraints. Does anyone know how to get this functional test to build?

3

There are 3 answers

0
Kurt Peek On BEST ANSWER

Following https://www.ryanchapin.com/configuring-vscode-to-use-build-tags-in-golang-to-separate-integration-and-unit-test-code/, I had to create a .vscode/settings.json file in the repository's root directory and add the following contents:

{
    "go.buildFlags": [
        "-tags=functional"
    ],
    "go.testTags": "functional",
}

Now VS Code works like normal in that file:

enter image description here

0
Cyril On

Have you tried go clean -cache?

And this link may help: https://github.com/golang/go/issues/42353

0
yakob abada On

This worked for me! by adding it in .vscode/settings.json

{
    "gopls.env": {
        "GOFLAGS": "-tags=test"
    }
}