golangci / golangci-lint

Fast linters runner for Go
https://golangci-lint.run
GNU General Public License v3.0
14.94k stars 1.35k forks source link

go-critic/ruleguard: load embedded ruleguard rules: rules/rules.go:13: can't load fmt #3107

Open ldez opened 1 year ago

ldez commented 1 year ago

The issue is a kind of documentation about this topic.

Description of the problem

$ golangci-lint run
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/go-critic@v0.6.1/checkers/checkers.go:58 +0x4b4
$ golangci-lint run
[ERROR] go-critic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt: setting an explicit GOROOT can fix this problem.

The problem

The error happens only when ruleguard (go-critic) is not able to load rules.

ruleguard (go-critic) uses GOROOT to load rules.

If the value of GOROOT is empty, ruleguard will fallback to the GOROOT from the Go context but this Go context can be wrong.

go-critic issues

Previous issues

The solution?

To avoid this problem you can define explicitly the GOROOT environment variable.

$ go env GOROOT                                            
/usr/local/go

$ export GOROOT=/usr/local/go
# or
export "GOROOT=$(go env GOROOT)"
biggianteye commented 1 year ago
  • Before v1.49.0
  • After v1.49.0

Is this a typo? The latest version at the time of writing this is v1.48.0.

ldez commented 1 year ago

no, I'm talking about the next version (v1.49.0). I didn't use the future tense just to be descriptive.

biggianteye commented 1 year ago

no, I'm talking about the next version (v1.49.0). I didn't use the future tense just to be descriptive.

Ah, I see. Now that I look more closely I can see that your changes are in master, ready for the next version. Makes sense now. Thanks.

cristaloleg commented 1 year ago

I see v0.6.1 in your log but AFAIR v0.6.4 fixed everything (https://github.com/go-critic/go-critic/releases/tag/v0.6.4) isn't it?

ldez commented 1 year ago

The logs are related to golangci-lint before v1.49.0.

andig commented 1 year ago

@ldez I had expected this to be resolved in v1.49.0 due to upgrade of go-critic, but it seems it isn't and still crashes on startup:

brew info golangci-lint
==> golangci-lint: stable 1.49.0 (bottled), HEAD
Fast linters runner for Go
https://golangci-lint.run/
/opt/homebrew/Cellar/golangci-lint/1.49.0 (9 files, 28.7MB) *
  Poured from bottle on 2022-08-24 at 18:41:07
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/golangci-lint.rb
License: GPL-3.0-only
==> Dependencies
Required: go ✔
==> Options
--HEAD
    Install HEAD version
==> Caveats
zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions
==> Analytics
install: 19,317 (30 days), 39,846 (90 days), 117,291 (365 days)
install-on-request: 19,309 (30 days), 39,814 (90 days), 117,186 (365 days)
build-error: 3 (30 days)
 evcc master*​ ≡ ❯
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()
    /Users/andig/go/pkg/mod/github.com/go-critic/go-critic@v0.6.3/checkers/embedded_rules.go:47 +0x494

I'm wondering why it sill points to go-critic@v0.6.3 though?

ldez commented 1 year ago

/Users/andig/go/pkg/mod/github.com/go-critic/go-critic@v0.6.3/checkers/embedded_rules.go:47 +0x494

@andig v1.49.0 uses go-critic@v0.6.4, so you are using a mysterious v1.49.0

andig commented 1 year ago

Fixed for me, sorry for the confusion! Had a stray v1.48.0 in the path.

psydvl commented 1 year ago

go-critic issue closed 2 days ago https://github.com/go-critic/go-critic/issues/1253 v0.6.5 released 2 days ago

vfaronov commented 1 year ago

This error isn’t fixed for me neither by 1.49.0 nor by explicitly setting GOROOT:

$ golangci-lint version
golangci-lint has version 1.49.0 built from cc2d97f3 on 2022-08-24T10:24:37Z

$ export GOROOT=$(go env GOROOT)

$ head -1 $GOROOT/src/fmt/doc.go
// Copyright 2009 The Go Authors. All rights reserved.

$ golangci-lint run --disable-all -E gocritic
ERRO [linter] gocritic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt: setting an explicit GOROOT can fix this problem. 
lucastheisen commented 1 year ago

@vfaronov , i had the same issue as you (go 1.19 and golangci-lint 1.49.0 with/without GOROOT) and switching to 1.50.1 resolved this for me.

fatelei commented 1 year ago

upgrade golangci-lint to 1.5x

phillbaker commented 1 year ago

I saw this when moving from x86_64 to arm64 (m1 machine). I found this helpful to uninstall the old golangci installed via homebrew: https://github.com/orgs/Homebrew/discussions/3437

pawel-slusarczyk commented 1 year ago

Still getting this error with GOROOT pointing to my Go 1.20.4 SDK and with golangci-lint v1.52.2.

➜  project git:(master) ✗ ./bin/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/go-critic@v0.6.3/checkers/embedded_rules.go:47 +0x4b4

Okay, actually it helped when I rebuild the binary of the vendored golangci-lint package. This step must be obvious for more frequent Go users, but I forgot about it.

Liooo commented 1 year ago

I'm seeing this error even when $GOROOT is set, and seems like 8f8c68ab8896d1317813081f20ff47831503e167 with go version 1.20 is somehow causing this on my machine.

.pre-commit-config.yml

repos:
  - repo: https://github.com/golangci/golangci-lint
#   rev: commit                                       go 1.20.4     go 1.19.8
#  ---------------------------------------------------------------------------------
#   rev: 8bbb6555e3d9348e320674fc0952eb8d0e5f6daf #     #3107        passes      <= latest master
#   ...
#   rev: 8f8c68ab8896d1317813081f20ff47831503e167 #     #3107         #3711      <= the ruleguard error happens from here
#   rev: 252ae9f2c92c1fed287d8721e1efdd0b72ca80ed #     #3711         #3711
#   ...
#   rev: da04413a8a1eefb8c10161c9f2b558138d01815c #     #3711         #3711       <= v1.52.2
    hooks:
    - id: golangci-lint
      language_version: 1.19.8 # or 1.20.4

Originally I've been trying to solve #3711, which I could confirm being fixed in the latest master (by https://github.com/golangci/golangci-lint/pull/3835). But from 8f8c68ab8896d1317813081f20ff47831503e167 with go 1.20, I get the ruleguard error described in this issue.


UPDATE

This was pre-commit thing. Inside pre-commit somehow the GOROOT is overwritten, and the workaround is:

$ export PRECOMMIT_GOROOT=$(go env GOROOT)

then,

repos:
  - repo: https://github.com/golangci/golangci-lint
     hooks:
     - id: golangci-lint
       language_version: 1.20.4
       entry: bash -c 'GOROOT=$PRECOMMIT_GOROOT golangci-lint run'
haroongelato commented 9 months ago

I fixed this issue by upgrading my golangci-lint version to 1.54.2.

dviljoen commented 7 months ago

I just ran into this when upgrading from go1.18.1 to go1.20.11. I also upgraded my golangci-lint to 1.55.2. As described above, I tried setting my GOROOT. No luck.

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/go-critic@v0.6.3/checkers/embedded_rules.go:47 +0x4b4
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/go-critic@v0.6.3/checkers/embedded_rules.go:47 +0x4b4
ns-saurabh commented 6 months ago

I ran into this issue with go1.21 and golangci-lint 1.51. Moving golangci-lint to 1.55.2 fixed the issue.

rmasclef commented 5 months ago

hey there I do have this issue even with the latest version

Adding export GOROOT=$(go env GOROOT) to my .zshrc file doesn't seem to fix the issue.


golangci-lint version
golangci-lint has version 1.55.2 built with go1.21.3 from e3c2265 on 2023-11-02T21:40:02Z
golangci-lint run ./...
ERRO [linters_context] gocritic: load embedded ruleguard rules: rules/rules.go:13: can't load fmt: setting an explicit GOROOT can fix this problem.
go version
go version go1.21.6 darwin/arm64
go env
GO111MODULE='auto'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/rmasclef/Library/Caches/go-build'
GOENV='/Users/rmasclef/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/rmasclef/go/pkg/mod'
GONOPROXY='github.com/reezogit'
GONOSUMDB='github.com/reezogit'
GOOS='darwin'
GOPATH='/Users/rmasclef/go'
GOPRIVATE='github.com/reezogit'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.21.6/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.21.6/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.6'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/rmasclef/Documents/work/reezocar/repositories/backend/reezoback/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/km/qbnnqyrs6g92mrrqstg2gk8r0000gn/T/go-build1707879524=/tmp/go-build -gno-record-gcc-switches -fno-common'