Gopls返回错误“Gopls:未返回包:包,www.example.com的加载错误github.com/Shopify/sarama

pqwbnv8z  于 2023-02-17  发布在  Go
关注(0)|答案(3)|浏览(316)

我已经检查了github.com/Shopify/saramamain分支(在提交947343309601b4eb3c2fa3e7d15d701b503dd491时),但我注意到在VS代码中我不能像往常一样“转到定义”。如果我将鼠标悬停在functional_consumer_group_test.go中的包名称sarama上,我会收到linter警告

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屏幕截图如下)。

在命令行中,如果我尝试gopls该文件,则会出现类似的错误:

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

我怀疑这与该文件中的构建约束(https://pkg.go.dev/cmd/go#hdr-Build_constraints)有关,来自https://github.com/Shopify/sarama/blob/947343309601 b4 eb 3c 2fa 3e 7 d15 d 701 b503 dd 491/functional_consumer_group_test.go#L1-L2,

//go:build functional
// +build functional

但是,我不清楚如何修改VS代码settings.json以通过这些构建约束。有人知道如何构建这个功能测试吗?

uhry853o

uhry853o1#

https://www.ryanchapin.com/configuring-vscode-to-use-build-tags-in-golang-to-separate-integration-and-unit-test-code/之后,我必须在存储库的根目录中创建一个.vscode/settings.json文件,并添加以下内容:

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

现在VS代码在该文件中正常工作:

ee7vknir

ee7vknir2#

你试过go clean -cache吗?
这个链接可能会有帮助:https://github.com/golang/go/issues/42353

u59ebvdq

u59ebvdq3#

这对我很有效!通过将其添加到.vscode/settings.json

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

相关问题