Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

depguard is enabled and fails on v1.53.0 #3862

Closed
4 tasks done
adamdecaf opened this issue Jun 1, 2023 · 49 comments · Fixed by #3866 or RedHatInsights/insights-results-aggregator#1757
Closed
4 tasks done

depguard is enabled and fails on v1.53.0 #3862

adamdecaf opened this issue Jun 1, 2023 · 49 comments · Fixed by #3866 or RedHatInsights/insights-results-aggregator#1757
Labels
bug Something isn't working dependencies Relates to an upstream dependency

Comments

@adamdecaf
Copy link

adamdecaf commented Jun 1, 2023

Welcome

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

Description of the problem

Running v1.53.0 of golangci-lint seems to enable depguard and fails due to a config error.

Version of golangci-lint

$ golangci-lint --version

v1.53.0

Configuration file

Go environment

$ go version && go env

go version go1.20.4 darwin/amd64

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v

golangci/golangci-lint info checking GitHub for tag 'v1.53.0'
golangci/golangci-lint info found version: 1.53.0 for v1.53.0/darwin/amd64
golangci/golangci-lint info installed ./bin/golangci-lint
STARTING golangci-lint checks
ERRO [linter] depguard: create analyzer: couldn't expand $gostd: could not read GOROOT directory: open src: no such file or directory
must have an Allow and/or Deny package list 

Code example or link to a public repository

Updating golangci-lint to v1.53.0 in https://github.com/moov-io/infra/blob/master/go/lint-project.sh

CI run: https://github.com/moov-io/infra/actions/runs/5149020788/jobs/9271486191

@adamdecaf adamdecaf added the bug Something isn't working label Jun 1, 2023
@gandarez
Copy link
Contributor

gandarez commented Jun 1, 2023

It's happening with latest version released v1.53.0.

@gandarez
Copy link
Contributor

gandarez commented Jun 1, 2023

There's an open issue to track it but the maintainer hasn't replied yet OpenPeeDeeP/depguard#46

@adamdecaf
Copy link
Author

Does the upgrade to v2 need to be reverted? #3795

@ldez
Copy link
Member

ldez commented Jun 1, 2023

The configuration has changed and it's expected.

#3727

We will not revert the upgrade.

@ldez ldez added question Further information is requested and removed bug Something isn't working labels Jun 1, 2023
@gandarez
Copy link
Contributor

gandarez commented Jun 1, 2023

It bumped from v1.52.2 to v1.53.0 so no major change was implicitly made. It's breaking for everyone who was using previous version and you assumed that's expected?? How to fix it without reverting the upgrade?

note: I have no depguard enabled on my side.

@ldez
Copy link
Member

ldez commented Jun 1, 2023

It bumped from v1.52.2 to v1.53.0 so no major change was implicitly made

We follow semver but we cannot follow the major version of each linter, then changes inside linter configuration are not considered as breaking.

@ldez
Copy link
Member

ldez commented Jun 1, 2023

I will try to reproduce the problem locally and find a fix.

@ldez
Copy link
Member

ldez commented Jun 1, 2023

Can you provide the output of go env?

@jhheider
Copy link

jhheider commented Jun 1, 2023

It fails something as simple as a golangci-lint help linters in an empty directory, where this was a working behavior in the previous version:

https://github.com/teaxyz/pantry/actions/runs/5148610773/jobs/9271272674

@adamdecaf
Copy link
Author

I don't have depguard enabled either.

@ldez
Copy link
Member

ldez commented Jun 1, 2023

I need a reproducible context, then I need information.

$ golangci-lint version

$ go env

$ cat .golangci.yml

And a public repo.

@adamdecaf
Copy link
Author

v1.53.0 of golangci-lint fails locally for me inside a Go project. There's no golangci.yml file in the project.

$ ./bin/golangci-lint version
ERRO [linter] depguard: create analyzer: couldn't expand $gostd: could not read GOROOT directory: open src: no such file or directory
must have an Allow and/or Deny package list 

@ldez
Copy link
Member

ldez commented Jun 1, 2023

I need a reproducible context, then I need information.

$ golangci-lint version

$ go env

$ cat .golangci.yml

And a public repo.

@jhheider
Copy link

jhheider commented Jun 1, 2023

$ mkdir foo
$ cd foo
$ golangci-lint version
ERRO [linter] depguard: create analyzer: couldn't expand $gostd: could not read GOROOT directory: open src: no such file or directory
must have an Allow and/or Deny package list
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/jacob/Library/Caches/go-build"
GOENV="/Users/jacob/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/jacob/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jacob/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/jacob/.tea/go.dev/v1.20.4"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/jacob/.tea/go.dev/v1.20.4/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -fdebug-prefix-map=/var/folders/t5/180fzzln0k7_0cgpzmkx2_w80000gn/T/go-build2213119517=/tmp/go-build -gno-record-gcc-switches -fno-common"
$ cat .golangci.yml
cat: .golangci.yml: No such file or directory

edited to add:

even just version has stopped working. Simplest quick test:

$ tea +golangci-lint.run=1.52.2 golangci-lint version
installed: ~/.tea/golangci-lint.run/v1.52.2
golangci-lint has version 1.52.2 built with go1.20.2 from da04413a on 2023-03-25T18:11:28Z
$ tea +golangci-lint.run=1.53.0 golangci-lint version
ERRO [linter] depguard: create analyzer: couldn't expand $gostd: could not read GOROOT directory: open src: no such file or directory
must have an Allow and/or Deny package list

@ldez
Copy link
Member

ldez commented Jun 1, 2023

I think I know the root of the problem: https://github.com/OpenPeeDeeP/depguard/blob/84475ef6d579f85ae86343f549bd9c267d3b7993/internal/utils/variables.go#L48

The use of build.Default.GOROOT cannot work because it's the GOROOT of the build.

@ldez
Copy link
Member

ldez commented Jun 1, 2023

I'm working on a fix, give me some time.

@JohnRoesler
Copy link

Thank you @ldez if you need an example on a public repo - I've got it failing here https://github.com/go-co-op/gocron/actions/runs/5149340859/jobs/9272212188

@adamdecaf
Copy link
Author

I have a public repo which has the failure as well moov-io/infra#271

@ldez
Copy link
Member

ldez commented Jun 1, 2023

I am still not able to reproduce it locally.

I created a simple app just to display build.Default.GOROOT and use 2 different GOROOT (one for the build, one for the run) and it works (I'm surprised but it works).

package main

import (
	"fmt"
	"go/build"
)

func main() {
	fmt.Println(build.Default.GOROOT)
}
$ ./sandbox
/usr/lib/go
$ ./sandbox 
/home/ldez/.gvm/gos/go1.20.4
$ GOROOT=fofo ./sandbox                                                                                                              
fofo
2023/06/02 00:01:38 open fofo: no such file or directory

@ldez
Copy link
Member

ldez commented Jun 1, 2023

What I'm sure about:

ERRO [linter] depguard: create analyzer: couldn't expand $gostd: could not read GOROOT directory: open src: no such file or directory

the message open src: no such file or directory means that the directory based on GOROOT is src.

This directory (src) is not related to your GOROOT (GOROOT="/Users/jacob/.tea/go.dev/v1.20.4")

And 2 users have the same error message with this directory (src).

Based on jobs/9271272674 it fails on all OS.

jobs/9272212188 and jobs/9271486191 failed on liunx amd64.

@adamdecaf
Copy link
Author

I'm on macOS right now and Go is installed via homebrew.

go env | grep ROOT
GOROOT="/usr/local/Cellar/go/1.20.4/libexec"

@ldez
Copy link
Member

ldez commented Jun 1, 2023

$ golangci-lint version              
golangci-lint has version 1.53.0 built with go1.20.4 from 9f2528a3 on 2023-06-01T19:43:45Z
$ git clone [email protected]:moov-io/infra.git  
Cloning into 'infra'...
remote: Enumerating objects: 9277, done.
remote: Counting objects: 100% (96/96), done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 9277 (delta 42), reused 86 (delta 36), pack-reused 9181
Receiving objects: 100% (9277/9277), 3.93 MiB | 1.45 MiB/s, done.
Resolving deltas: 100% (5751/5751), done.
$ cd infra      
$ golangci-lint run  
$
$ git clone [email protected]:go-co-op/gocron.git                  
Cloning into 'gocron'...
remote: Enumerating objects: 1746, done.
remote: Counting objects: 100% (314/314), done.
remote: Compressing objects: 100% (161/161), done.
remote: Total 1746 (delta 218), reused 234 (delta 149), pack-reused 1432
Receiving objects: 100% (1746/1746), 592.91 KiB | 1.54 MiB/s, done.
Resolving deltas: 100% (1158/1158), done.
$ cd gocron
$ golangci-lint run
scheduler.go:1178:11: superfluous-else: if block ends with a break statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
} else {
repeatMap[dayOfMonth]++
}
scheduler_test.go:26:24: unused-parameter: parameter 'i' seems to be unused, consider removing or renaming it as _ (revive)
func (f fakeTime) Unix(i int64, i2 int64) time.Time {
^
scheduler_test.go:2356:54: unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
func TestScheduler_CheckSetBehaviourBeforeJobCreated(t *testing.T) {
^
$

I'm on Linux amd64.

@ldez
Copy link
Member

ldez commented Jun 1, 2023

can someone try this command:

GOROOT=<YOUR REAL GOROOT> golangci-lint version

@adamdecaf
Copy link
Author

adamdecaf commented Jun 1, 2023

I can reproduce this with the following Dockerfile.

FROM golang:1.20

RUN wget https://github.com/golangci/golangci-lint/releases/download/v1.53.0/golangci-lint-1.53.0-linux-amd64.tar.gz
RUN tar xf golangci-lint-1.53.0-linux-amd64.tar.gz

RUN go env
RUN /go/golangci-lint-1.53.0-linux-amd64/golangci-lint version

Building that fails with the following.

 => ERROR [5/5] RUN /go/golangci-lint-1.53.0-linux-amd64/golangci-lint version                                                         0.5s 
------                                                                                                                                      
 > [5/5] RUN /go/golangci-lint-1.53.0-linux-amd64/golangci-lint version:                                                                    
#0 0.468 level=error msg="[linter] depguard: create analyzer: must have an Allow and/or Deny package list"                                  
------
Dockerfile:8
--------------------
   6 |     RUN go env
   7 |     
   8 | >>> RUN /go/golangci-lint-1.53.0-linux-amd64/golangci-lint version
   9 |     
  10 |     # WORKDIR /go/src/
--------------------
ERROR: failed to solve: process "/bin/sh -c /go/golangci-lint-1.53.0-linux-amd64/golangci-lint version" did not complete successfully: exit code: 3

Edit: If I setup a new GOROOT which matches the v1.53.0 builder release path the issue goes away, so it does appear to be a mismatch of the build's GOROOT and the runtime GOROOT.

Add the following steps to the Dockerfile allows docker build -t golangci-issue . to complete.

RUN mkdir -p /opt/hostedtoolcache/go/1.20.4/x64
RUN ln -s /usr/local/go/* /opt/hostedtoolcache/go/1.20.4/x64/
ENV GOROOT=/opt/hostedtoolcache/go/1.20.4/x64/

Jumping into the completed Docker image shows a working GOROOT.

docker run -it --entrypoint /bin/bash golangci-issue 
root@bb48f4e764af:/go# go env | grep ROOT
GOROOT="/opt/hostedtoolcache/go/1.20.4/x64"

@ldez
Copy link
Member

ldez commented Jun 1, 2023

Thank you, I can reproduce it now.

$ docker run --rm -it golang:1.20-alpine sh                                                                  
/go # wget https://github.com/golangci/golangci-lint/releases/download/v1.53.0/golangci-lint-1.53.0-linux-amd64.tar.gz
Connecting to github.com (140.82.121.4:443)
Connecting to objects.githubusercontent.com (185.199.109.133:443)
saving to 'golangci-lint-1.53.0-linux-amd64.tar.gz'
golangci-lint-1.53.0 100% |********************************************************************************************************************************************************************************************| 9760k  0:00:00 ETA
'golangci-lint-1.53.0-linux-amd64.tar.gz' saved
/go # tar xf golangci-lint-1.53.0-linux-amd64.tar.gz
/go # go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/dev/null"
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 -m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3482902365=/tmp/go-build -gno-record-gcc-switches"
/go # /go/golangci-lint-1.53.0-linux-amd64/golangci-lint version
ERRO [linter] depguard: create analyzer: must have an Allow and/or Deny package list 
/go # GOROOT=$(go env GOROOT) /go/golangci-lint-1.53.0-linux-amd64/golangci-lint version
golangci-lint has version 1.53.0 built with go1.20.4 from 9f2528a3 on 2023-06-01T19:43:45Z

If I set the GOROOT explicitly it works 🤔

@ldez
Copy link
Member

ldez commented Jun 1, 2023

More information: the problem appears even if you don't use depguard because it is related to a global variable.

Another example of why I hate global variables 😢

rissson added a commit to goauthentik/authentik that referenced this issue Jun 2, 2023
JanDeDobbeleer added a commit to JanDeDobbeleer/oh-my-posh that referenced this issue Jun 2, 2023
JanDeDobbeleer added a commit to JanDeDobbeleer/oh-my-posh that referenced this issue Jun 2, 2023
rissson added a commit to goauthentik/authentik that referenced this issue Jun 2, 2023
@ldez ldez closed this as completed in #3866 Jun 2, 2023
rissson added a commit to goauthentik/authentik that referenced this issue Jun 2, 2023
@JohnRoesler
Copy link

Thanks for the quick resolution folks! 👏

@deitch
Copy link

deitch commented Jun 7, 2024

I know this is a year old, but picking it up now.

If I understand correctly, after this fix, if you do not have depguard enabled, everything should work. And, indeed, running golangci-lint run ./... --disable depguard works.

If you use depguard it remains broken? I had thought depguard was now built in with a fix in a fork based on this comment, and so it should be fixed for depguard-enabled as well? Or is that fork just to fix the "breaks everything else even if not enabled"?

@ldez
Copy link
Member

ldez commented Jun 7, 2024

depguard has been updated since that issue.

We don't use a fork now and you don't need to disable it.

You should update your configuration: https://golangci-lint.run/usage/linters/#depguard

@deitch
Copy link

deitch commented Jun 7, 2024

Thanks @ldez .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependencies Relates to an upstream dependency
Projects
None yet
6 participants