Golang linter报错:’上下文加载失败:没有要分析的Go文件’。

huangapple go评论228阅读模式
英文:

Golang linter issues 'context loading failed: no go files to analyze'

问题

我们在项目中使用以下工具进行 Golang 代码的 lint 操作:

  • golangci-lint 版本 1.40.1
  • Golang 版本 1.16.4

到目前为止,我们是通过运行以下 bash 脚本(从我们仓库的根目录)来进行 lint 操作:

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 环境变量。当我像这样运行 lint 工具时,关闭了 GO111MODULE

GO111MODULE=off golangci-lint run --build-tags="unit contract container"

上述错误消息消失了,但是我却得到了很多错误的 linting 报告,例如:

api/router.go:152:5: undeclared name: `PermissionUpdatePackage` (typecheck)
                                PermissionUpdatePackage,
                                ^

我的 Go 环境如下:

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 来安装 lint 工具,根据这份文档这是推荐的方式。

英文:

We are using

  • golangci-lint version 1.40.1 together with
  • golang version 1.16.4

in our project for linting our Golang code.

Until now, what we did is running this bash script (from the root
directory of our repository):

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"

With some recent updates of Golang and golangci-lint, we suddenly face this error message:

ERRO Running error: context loading failed: no go files to analyze 

There is a lengthy post on GitHub regarding this issue but the only useful suggestion there is to turn off the GO111MODULE env variable. When I run the linter with GO111MODULE turned off like

GO111MODULE=off golangci-lint run --build-tags="unit contract container"

the upper error message disappears but instead I am getting lots of false linting errors like:

api/router.go:152:5: undeclared name: `PermissionUpdatePackage` (typecheck)
                                PermissionUpdatePackage,
                                ^

My go environment looks like this:

GO111MODULE=on
GOPATH=/Users/USER/workspace/go
GOROOT=/usr/local/opt/go/libexec
GOPRIVATE=*.CUSTOMER.com
GOSS_PATH=/usr/local/bin/goss

I tried to install the linter via go get... as well as go install ... and finally brew install golangci-lint which seems to be the recommended way following this documentation.

答案1

得分: 6

在项目的根目录中运行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_version='1.40.1'
if ! [ -x "$(command -v golangci-lint)" ]; then
    echo "Fetching linter..."
    # 我们不能将代码检查工具安装在项目目录中,否则会出现依赖错误
    # 因此,临时跳转到/tmp目录
    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
英文:

Running a go get ./... in the root of the project eventually solved the issues. In between we ran the following commands that probably cleared some (module?) caches that might have caused trouble as well:

golangci-lint cache clean && go clean -modcache -cache -i
golangci-lint run -v --timeout=5s

The error message

ERRO Running error: context loading failed: failed to load packages: timed out to load packages: context deadline exceeded 

in the latter command pointed us to this GitHub post that made me try out go get ./...

For installing the linter (with a specified version), we ended up with this script:

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

huangapple
  • 本文由 发表于 2021年6月16日 17:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/68000066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定