英文:
gopls returns the error "gopls: no packages returned: packages.Load error" for github.com/Shopify/sarama
问题
我已经检查了github.com/Shopify/sarama
的main
分支(提交为947343309601b4eb3c2fa3e7d15d701b503dd491
),但我注意到在VS Code中,我无法像往常一样“转到定义”。如果我将鼠标悬停在functional_consumer_group_test.go
中的包名sarama
上,我会收到linter警告:
找不到打开文件/Users/kurtpeek/go/src/github.com/Shopify/sarama/functional_consumer_group_test.go的包:<nil>。
如果此文件包含构建标签,请尝试将“-tags=<build tag>”添加到您的gopls“buildFlags”配置中(请参阅(https://github.com/golang/tools/blob/master/gopls/doc/settings.md#buildflags-string)。
否则,请参阅故障排除指南以获取帮助(https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md)。go list
(请参见下面的屏幕截图)。
从命令行中,如果我尝试对该文件运行gopls
,我会得到类似的错误:
> gopls check functional_consumer_group_test.go
gopls: 未返回任何包:packages.Load 错误
我怀疑这与该文件中的构建约束(https://pkg.go.dev/cmd/go#hdr-Build_constraints)有关,来自https://github.com/Shopify/sarama/blob/947343309601b4eb3c2fa3e7d15d701b503dd491/functional_consumer_group_test.go#L1-L2,
//go:build functional
// +build functional
然而,我不清楚如何修改我的VS Code settings.json
以通过这些构建约束。有人知道如何使这个功能测试能够构建吗?
英文:
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).
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?
答案1
得分: 6
根据 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 Code在该文件中可以正常工作。
英文:
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:
答案2
得分: 1
你尝试过go clean -cache
吗?
这个链接可能会有帮助:
https://github.com/golang/go/issues/42353
英文:
Have you tried go clean -cache
?
And this link may help:
https://github.com/golang/go/issues/42353
答案3
得分: 0
这对我有用!通过将其添加到.vscode/settings.json
中:
{
"gopls.env": {
"GOFLAGS": "-tags=test"
}
}
英文:
This worked for me!
by adding it in .vscode/settings.json
{
"gopls.env": {
"GOFLAGS": "-tags=test"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论