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

Issue

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?

Solution

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

Answered By – Kurt Peek

Answer Checked By – Mary Flores (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.