21

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.

5
  • Have you tried GO111MODULE=off golangci-lint run --build-tags="unit contract container", i.e. disabling modules for the execution of the command? (I didn't quite get where exactly you placed the export GO111MODULE=off. Also you mention "With some recent updates", what updates? Commented Jun 16, 2021 at 10:26
  • thanks, I will clarify my question. And yes, I tried running GO111MODULE=off golangci-lint run --build-tags="unit contract container" Commented Jun 16, 2021 at 10:33
  • 2
    Golang (even the v1.16 branch) got lots of security fixes after 1.16.4, and go has great backward compatibility. Why are you using that specific version? Commented Sep 5, 2022 at 12:00
  • Thank you for updating your question with the solution you found! Could you please reformat it as an answer to your own question? stackoverflow.com/help/self-answer Commented Feb 7, 2023 at 16:56
  • when I resolved all the issues listed by go mod tidy, this error went away Commented Jan 30, 2024 at 18:10

2 Answers 2

9

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
Sign up to request clarification or add additional context in comments.

2 Comments

go get ./... solved the problem for me.
go mod tidy also resolves the issue.
0

I had a similiar issue, for me solutions was to install linter with a shell script instead of snap `-,-

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.