我们正在使用
golangci-lint
版本1.40.1
与1.16.4
在我们的 Golang 代码检查项目中。
到目前为止,我们所做的是运行这个 bash 脚本(从根目录) 我们存储库的目录):
if ! [ -x "$(command -v golangci-lint)" ]; then
echo "Fetching linter..."
go install github.com/golangci/golangci-lint/cmd/golangci-lint
go mod tidy
fi
golangci-lint run --build-tags="unit contract container"
随着 Golang 和
golangci-lint
最近的一些更新,我们突然面临这个错误消息:
ERRO Running error: context loading failed: no go files to analyze
GitHub 上有一篇关于此问题的长篇文章,但唯一有用的建议是关闭
GO111MODULE
环境变量。当我运行 linter 并关闭 GO111MODULE
时,就像
GO111MODULE=off golangci-lint run --build-tags="unit contract container"
上面的错误消息消失了,但我收到了很多错误的 linting 错误,例如:
api/router.go:152:5: undeclared name: `PermissionUpdatePackage` (typecheck)
PermissionUpdatePackage,
^
我的围棋环境是这样的:
GO111MODULE=on
GOPATH=/Users/USER/workspace/go
GOROOT=/usr/local/opt/go/libexec
GOPRIVATE=*.CUSTOMER.com
GOSS_PATH=/usr/local/bin/goss
我尝试通过
go get...
以及go install ...
和最后brew install golangci-lint
安装linter,这似乎是遵循本文档的推荐方式。
在项目根目录中运行
go get ./...
最终解决了问题。在这之间,我们运行了以下命令,这些命令可能清除了一些可能也造成问题的(模块?)缓存:
golangci-lint cache clean && go clean -modcache -cache -i
golangci-lint run -v --timeout=5s
错误信息
ERRO Running error: context loading failed: failed to load packages: timed out to load packages: context deadline exceeded
在后一个命令中向我们指出了这个 GitHub 帖子,它让我尝试了
go get ./...
为了安装 linter(具有指定版本),我们最终得到了这个脚本:
linter_version='1.40.1'
if ! [ -x "$(command -v golangci-lint)" ]; then
echo "Fetching linter..."
# we cannot install linter in the project directory, otherwise we get dependency errors
# hence, temporarily jump into the /tmp directory
pushd /tmp > /dev/null
GO111MODULE=on go get github.com/golangci/golangci-lint/cmd/golangci-lint@v"${linter_version}" 2>&1
popd >/dev/null
fi
我也有类似的问题, 对我来说,解决方案是使用 shell 脚本安装 linter,而不是 snap `-,-