golangci-lint icon indicating copy to clipboard operation
golangci-lint copied to clipboard

Panic since update to v1.43.0 (load embedded ruleguard rules: rules/rules.go:13: can't load fmt)

Open Antonboom opened this issue 3 years ago • 23 comments

Welcome

  • [X] Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • [X] Yes, I've searched similar issues on GitHub and didn't find any.
  • [X] Yes, I've included all information below (version, config, etc).
  • [X] Yes, I've tried with the standalone linter if available. (https://golangci-lint.run/usage/linters/)

Description of the problem

Please read

https://github.com/go-critic/go-critic/issues/1157

Version of golangci-lint

$ golangci-lint --version
panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt

goroutine 1 [running]:
github.com/go-critic/go-critic/checkers.init.9()
        github.com/go-critic/[email protected]/checkers/checkers.go:58 +0x4b4

Configuration file

No file.

Go environment

$ go version && go env
go version go1.17.1 darwin/arm64
GO111MODULE="on"
GOARCH="arm64"
GOBIN="/Users/anthony/golang_workspace/bin"
GOCACHE="/Users/anthony/Library/Caches/go-build"
GOENV="/Users/anthony/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/anthony/golang_workspace/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/anthony/golang_workspace"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.17.1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/anthony/****/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/6t/v80c8sfs5zqf38b2yhzq592h0000gn/T/go-build1630597575=/tmp/go-build -gno-record-gcc-switches -fno-common"

Verbose output of running

$ golangci-lint cache clean
panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt

goroutine 1 [running]:
github.com/go-critic/go-critic/checkers.init.9()
        github.com/go-critic/[email protected]/checkers/checkers.go:58 +0x4b4


$ golangci-lint run -v
panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt

goroutine 1 [running]:
github.com/go-critic/go-critic/checkers.init.9()
        github.com/go-critic/[email protected]/checkers/checkers.go:58 +0x4b4

Antonboom avatar Nov 23 '21 07:11 Antonboom

TL;DR

  • check what your Go is for ARM
  • check env (go version & GOARCH) in IDEA
  • do not install golangci-lint via brew

I decided to compare the output of go env in IDEA & zsh:

$ diff /tmp/idea.env /tmp/zsh.env
1,2c1,2
< GO111MODULE=""
< GOARCH="amd64"
---
> GO111MODULE="on"
> GOARCH="arm64"
9c9
< GOHOSTARCH="amd64"
---
> GOHOSTARCH="arm64"
22c22
< GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
---
> GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
24c24
< GOVERSION="go1.17.2"
---
> GOVERSION="go1.17.1"
37c37
< GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/6t/v80c8sfs5zqf38b2yhzq592h0000gn/T/go-build2865944367=/tmp/go-build -gno-record-gcc-switches -fno-common"
\ No newline at end of file
---
> GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/6t/v80c8sfs5zqf38b2yhzq592h0000gn/T/go-build3978166248=/tmp/go-build -gno-record-gcc-switches -fno-common"

Two questions:

  1. why different archs (amd & arm)?
  2. why different Go versions (1.17.2 & 1.17.1)?

I didn't remember installing multiple versions of Go. This IDEA project was new & he picked up the environment automatically. And it was:

$ /usr/local/Cellar/go/1.17.2/bin/go env
GO111MODULE="on"
GOARCH="amd64"
...
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
...
GOOS="darwin"
...
GOROOT="/usr/local/go"         <----------------------- WTF? It's a path of original 1.17.1 ARM Go
...
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVERSION="go1.17.2"
...

That's where the dog is buried. 1.17.2 AMD Go flew in together with new golangci-lint:

$ brew install golangci-lint
==> Downloading https://ghcr.io/v2/homebrew/core/go/manifests/1.17.2
...
==> Installing dependencies for golangci-lint: go
==> Installing golangci-lint dependency: go
==> Pouring go--1.17.2.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/go/1.17.2: 10,814 files, 565.8MB
==> Installing golangci-lint
...

Solution:

  • rm -rf /usr/local/go /usr/local/opt/go /usr/local/Cellar/go
  • rm -rf /usr/local/bin/golangci-lint
  • Install go via https://go.dev/dl/ (be careful - https://go.dev/doc/install will download AMD64 Go)
  • Install golangci-lint via https://github.com/golangci/golangci-lint/releases
$ golangci-lint version
golangci-lint has version 1.43.0 built from 861262b7 on 2021-11-03T11:57:46Z

$ golangci-lint run ./...
$?                    
zsh: command not found: 0

@ldez, FYI You can close this issue & open other about brew. leave it to you, thanks!

Antonboom avatar Nov 25 '21 05:11 Antonboom

Reproduced the error when installing golangci-lint v1.43.0 via the provided install.sh script using the golang:1.18beta1 image:

$ docker container run --rm -it golang:1.18beta1
root@6e2d2d993681:/go# curl -sSfLO https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh install.sh -b "$(go env GOPATH)/bin" v1.43.0 && golangci-lint --version
golangci/golangci-lint info checking GitHub for tag 'v1.43.0'
golangci/golangci-lint info found version: 1.43.0 for v1.43.0/linux/amd64
golangci/golangci-lint info installed /go/bin/golangci-lint
panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt

goroutine 1 [running]:
github.com/go-critic/go-critic/checkers.init.9()
	github.com/go-critic/[email protected]/checkers/checkers.go:58 +0x4b4

atc0005 avatar Dec 15 '21 10:12 atc0005

go1.18beta1 is currently not supported by golangci-lint.

I flag this as off-topic for now.

ldez avatar Dec 15 '21 11:12 ldez

Building golangci-lint from sources on the target machine results in no panic.

$ docker container run --rm -it golang:1.18beta1
root@6e2d2d993681:/go# git clone --depth 1 https://github.com/golangci/golangci-lint.git
root@6e2d2d993681:/go/golangci-lint# cd golangci-lint
root@6e2d2d993681:/go/golangci-lint# go build -o bin/golangci-lint ./cmd/golangci-lint
root@82327daf6a68:/go/golangci-lint# ./bin/golangci-lint --version
golangci-lint has version (devel) built from (unknown, mod sum: "") on (unknown)

Same for gocritic.

I can reproduce the issue using the install.sh, but I don't know how it works and what's the difference between that and from-source installation.

quasilyte avatar Jan 03 '22 12:01 quasilyte

@quasilyte the difference is the version of Go used to build and the commits used to build:

  • with the install script it's the tag v1.43.0 (built with go1.17) and it's just a copy of a binary.
  • with the "build from source" approach you are using go1.18, and the HEAD of master

the relation between go1.18 and go-critic/ruleguard -> #2414

ldez avatar Jan 03 '22 12:01 ldez

The comments marked as "off topic" resolved my issue for me when I encountered this. My build environment was updated to 1.18 automatically, so this upgrade broke golangci-lint for me. Thus, I had to build from source in order to resolve the issue.

willbeason avatar Jan 18 '22 22:01 willbeason

@willbeason please keep in mind that this new release is an unknown ground for most of us. For many maintainers of any kinds of linters it's hard to adapt before 1.18 is released completely. It's like living in two realities, etc. Not to mention that there are some issues in the relevant libraries like /x/tools that make the transition hard (most tools rely on them in one way or another). While building from the source may be helpful, it also means the issue in the underlying tools we're using to bundle our tools. Which is another hint that the main issue may not be on the side of the maintainers (they can't update the world, etc.)

So let's try to be patient. This is a big release for Go.

quasilyte avatar Jan 19 '22 08:01 quasilyte

I'm not sure how to update x/tool because our project has a lot of packages which have multiple transitive dependencies on old x/tools

Here the debug output:

#10 [5/5] RUN gofmt -d $(find . -type f -name '*.go' -not -path "./vendor/*") &&     golangci-lint run &&     go test -mod=vendor -cover ./... &&     go build -mod=vendor
#10 sha256:23794d00eb35d59d177437edfb5f4b1ecc1941264a992a7c6130cdf1fdfc1875
#10 5.329 debug: failed to import "fmt":
#10 5.329 debug:   gc importer: can't find import: "fmt"
#10 5.329 debug:   source importer: type-checking package "fmt" failed (/usr/local/go/src/fmt/errors.go:7:8: could not import errors (type-checking package "errors" failed (/usr/local/go/src/errors/wrap.go:8:2: could not import internal/reflectlite (type-checking package "internal/reflectlite" failed (/usr/local/go/src/internal/reflectlite/value.go:10:2: could not import runtime (type-checking package "runtime" failed (/usr/local/go/src/runtime/chan.go:21:2: could not import internal/abi (type-checking package "internal/abi" failed (/usr/local/go/src/internal/abi/abi.go:117:19: undeclared name: any)))))))))
#10 5.329 debug:   GOROOT="/usr/local/go" GOPATH="/go"
#10 9.796 debug: failed to import "fmt":
#10 9.796 debug:   gc importer: can't find import: "fmt"
#10 9.796 debug:   source importer: type-checking package "fmt" failed (/usr/local/go/src/fmt/errors.go:7:8: could not import errors (type-checking package "errors" failed (/usr/local/go/src/errors/wrap.go:8:2: could not import internal/reflectlite (type-checking package "internal/reflectlite" failed (/usr/local/go/src/internal/reflectlite/value.go:10:2: could not import runtime (type-checking package "runtime" failed (/usr/local/go/src/runtime/chan.go:21:2: could not import internal/abi (type-checking package "internal/abi" failed (/usr/local/go/src/internal/abi/abi.go:117:19: undeclared name: any)))))))))
#10 9.796 debug:   GOROOT="/usr/local/go" GOPATH="/go"
#10 9.798 panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt
#10 9.798 
#10 9.798 goroutine 1 [running]:
#10 9.798 github.com/go-critic/go-critic/checkers.init.22()
#10 9.798 	github.com/go-critic/[email protected]/checkers/embedded_rules.go:46 +0x4b4
#10 ERROR: executor failed running [/bin/sh -c gofmt -d $(find . -type f -name '*.go' -not -path "./vendor/*") &&     golangci-lint run &&     go test -mod=vendor -cover ./... &&     go build -mod=vendor]: exit code: 2

oneumyvakin avatar Mar 16 '22 09:03 oneumyvakin

Installing golangci-lint from source I don't observe the same error as when using the released binary.

Binary

wget https://github.com/golangci/golangci-lint/releases/download/v1.44.2/golangci-lint-1.44.2-linux-amd64.tar.gz
tar -xf golangci-lint-1.44.2-linux-amd64.tar.gz
./golangci-lint-1.44.2-linux-amd64/golangci-lint --version
panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt

Install from source

go install github.com/golangci/golangci-lint/cmd/[email protected]

golangci-lint --version
golangci-lint has version v1.44.2 built from (unknown, mod sum: "h1:MzvkDt1j1OHkv42/feNJVNNXRFACPp7aAWBWDo5aYQw=") on (unknown)

mikkeloscar avatar Mar 16 '22 11:03 mikkeloscar

If you are using go1.18, please follow the issue #2649

ldez avatar Mar 16 '22 11:03 ldez

IMHO this issue is not caused by multiple go versions: I happen to come across that exact same error while using the same go version (1.17).

I am running go on a docker container with this image: golang:1.17-alpine3.14

Here is my docker's go env:

GO111MODULE="" GOARCH="arm64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="arm64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/go/pkg/mod" GONOPROXY="bitbucket.org/atlassian/*,bitbucket.org/hipchat/*" GONOSUMDB="bitbucket.org/atlassian/*,bitbucket.org/hipchat/*" GOOS="linux" GOPATH="/go" GOPRIVATE="bitbucket.org/atlassian/*,bitbucket.org/hipchat/*" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_arm64" GOVCS="" GOVERSION="go1.17.10" GCCGO="gccgo" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/go/src/bitbucket.org/atlassian/perms-cassandra-monitor/go.mod" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1535814144=/tmp/go-build -gno-record-gcc-switches"

It seems to be pretty specific to that go version. Yesterday I tested the cli and it was working with go 1.18

Downgrading to version 1.42.1 did the trick but I am stuck at that version, so it is hardly a permanent solution.

juan-carvajal avatar May 18 '22 21:05 juan-carvajal

I, too, have a singular Go version:

$ go version
go version go1.17.7 darwin/arm64

When I download and run golangci-lint 1.46.2 (from the releases page), I get this error:

$ ~/Downloads/golangci-lint-1.46.2-darwin-arm64/golangci-lint version
panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt

goroutine 1 [running]:
github.com/go-critic/go-critic/checkers.init.22()
	github.com/go-critic/[email protected]/checkers/embedded_rules.go:47 +0x494

If I build from source, I see:

$ make
go build -o golangci-lint ./cmd/golangci-lint
...
PASS
ok  	github.com/golangci/golangci-lint/test	130.527s
testing: warning: no tests to run
PASS
ok  	github.com/golangci/golangci-lint/test/bench	1.214s [no tests to run]
?   	github.com/golangci/golangci-lint/test/ruleguard	[no test files]
?   	github.com/golangci/golangci-lint/test/testdata_etc/abspath	[no test files]
?   	github.com/golangci/golangci-lint/test/testdata_etc/unused_exported	[no test files]
testing: warning: no tests to run
PASS
ok  	github.com/golangci/golangci-lint/test/testdata_etc/unused_exported/lib0.144s [no tests to run]
?   	github.com/golangci/golangci-lint/test/testshared	[no test files]
$ ./golangci-lint version
golangci-lint has version (devel) built from (unknown, mod sum: "") on (unknown)

I'll build from source, locally, for the time being.

faunaee avatar May 27 '22 17:05 faunaee

@faunaee 's solution do not work for me. go version go1.17.11 darwin/amd64

panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt

goroutine 1 [running]:
github.com/go-critic/go-critic/checkers.init.22()
	github.com/go-critic/[email protected]/checkers/embedded_rules.go:47 +0x494

duval1024 avatar Jun 10 '22 09:06 duval1024

Solution that works for me for:

go version go1.17.11 darwin/arm64
  1. Uninstall all versions of Go from Homebrew
  2. Uninstall all versions of golangci-lint from Homebrew
  3. Do some directory cleanup using find / -type d -name go 2> /dev/null
  4. Do some directory cleanup using find / -type d -name "golangci-lint" 2> /dev/null
  5. Install go1.17.11.darwin-arm64.pkg via go.dev/dl using the installer (.pkg)
  6. Install golangci-lint using go install github.com/golangci/golangci-lint/cmd/[email protected] (current latest version)

revett avatar Jun 10 '22 14:06 revett

Thanks. It works.

duval1024 avatar Jun 12 '22 02:06 duval1024

This is stll a problem with the most recent release -- since the release is built with go1.18, it fails to run when using go1.17. Is there a way to release a golangci-lint binary which works with both go1.17 and 1.18? Or are you effectively dropping support for go1.17 in binary releases, and rely on everyone compiling themselves?

cfstras avatar Jul 01 '22 08:07 cfstras

This is stll a problem with the most recent release -- since the release is built with go1.18, it fails to run when using go1.17. Is there a way to release a golangci-lint binary which works with both go1.17 and 1.18? Or are you effectively dropping support for go1.17 in binary releases, and rely on everyone compiling themselves?

Have you tried my workaround above, as that works with the current latest release (v1.46.2) and v1.17.11 of Go.

revett avatar Jul 01 '22 14:07 revett

Thanks @revett - also getting the same issue with latest 1.47.0 release and prebuilt darwin/arm64 releases.

Installing from source via:

go install github.com/golangci/golangci-lint/cmd/[email protected]

did the trick 👍

magnetikonline avatar Jul 19 '22 02:07 magnetikonline

Just to circle back - v1.47.3 was just released - https://github.com/golangci/golangci-lint/releases/tag/v1.47.3

Downloaded https://github.com/golangci/golangci-lint/releases/download/v1.47.3/golangci-lint-1.47.3-darwin-arm64.tar.gz from this release - can confirm, it now works without the panic() 🎉.

Maybe a few others can confirm - but this issue can probably be closed out now.

magnetikonline avatar Aug 02 '22 03:08 magnetikonline

I am on go version go1.18.4 darwin/amd64.

When I update to 1.47.3, it works. Thanks for everyone's hard work.

thetonus avatar Aug 02 '22 16:08 thetonus

Thanks, everyone, for the great work!

Should go1.19.0 already work with 1.47.3? Locally this combination works great for me:

image

But on the GH action I'm seeing the same error as outlined in the OP:

image

Here's a link to the GH Action run with this error.

EDIT: If I downgrade the go version to go1.18 on the GH action it runs, but now the rules don't match anymore. gofumpt wants a file to look different with go1.18 on the action, then my local go1.19 version does.

EDIT2: It's only a single file that differs, so for now my workaround is downgrade the GH action to go1.18 and put a //nolint:gofumpt in front of the affected code block.

etiennedi avatar Aug 02 '22 19:08 etiennedi

should go1.19.0 already work with 1.47.3?

currently, we support go1.17 and go1.18.

https://golangci-lint.run/usage/faq/#which-go-versions-are-supported

You can follow #3037 for go1.19 support

ldez avatar Aug 02 '22 20:08 ldez

@ldez this issue is back on Go 1.19 and 1.48.0

andig avatar Aug 09 '22 20:08 andig

The same with docker build, inside the image it does not work

...
Successfully built 3849205e305f
Successfully tagged cpr:1.2.0
root@:/opt/prod/cpr# docker run --rm -it cpr:1.2.0 golangci-lint --version
panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt

goroutine 1 [running]:
github.com/go-critic/go-critic/checkers.init.22()
	github.com/go-critic/[email protected]/checkers/embedded_rules.go:47 +0x4b4
root@:/opt/prod/cpr#
root@:/opt/prod/cpr# docker run --rm -it cpr:1.2.0 gitea  --version
0.8.0
root@:/opt/prod/cpr#
root@:/opt/prod/cpr# docker run --rm -it cpr:1.2.0 go version
go version go1.19 linux/amd64
root@:/opt/prod/cpr#

but is okay with

docker pull golang:1.18.3

shakibamoshiri avatar Aug 17 '22 10:08 shakibamoshiri

can you try to set explicitly the GOROOT?

ldez avatar Aug 17 '22 11:08 ldez

you can test it with this docker file

first

# golangci-lint does not work with latest version
FROM golang:latest

# use a match version
# FROM golang:1.18.3

ARG GO_LINT_ADDRESS=https://github.com/golangci/golangci-lint/releases/download/v1.46.2/golangci-lint-1.46.2-linux-amd64.tar.gz
ARG LINT_TAR_NAME=golangci-lint-1.46.2-linux-amd64

RUN apt update && \
    wget -c ${GO_LINT_ADDRESS} -O ${LINT_TAR_NAME}.tar.gz && \
    tar xzf ${LINT_TAR_NAME}.tar.gz --strip-components=1  -C /usr/bin/ ${LINT_TAR_NAME}/golangci-lint && \
    chmod +x /usr/bin/golangci-lint


# install golangci-ci by go did not work
# it runs but on runtime throws errors
# go install github.com/golangci/golangci-lint/cmd/[email protected]

CMD golangci-lint

second

docker build --no-cache -t lint:v1 .

third - result for me

Successfully built a04e027eb69d
Successfully tagged lint:v1
root[1][0]docker:/opt/prod/lint# docker run --rm -it lint:v1 
panic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt

goroutine 1 [running]:
github.com/go-critic/go-critic/checkers.init.22()
	github.com/go-critic/[email protected]/checkers/embedded_rules.go:47 +0x4b4

shakibamoshiri avatar Aug 17 '22 11:08 shakibamoshiri

can you add this GOROOT="/usr/local/go" explicitly in your Dockerfile?

(I already tried)

ldez avatar Aug 17 '22 12:08 ldez

closed in favor of #3107

ldez avatar Aug 17 '22 17:08 ldez