Golangci-lint: Error: build flag -mod=readonly only valid when using modules

Created on 10 Nov 2020  路  10Comments  路  Source: golangci/golangci-lint

Thank you for creating the issue!

  • [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).

Please include the following information:

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version v1.32.2 built from (unknown, mod sum: "h1:CgIeFWTLJ3Nt1w/WU1RO351j/CjN6LIVjppbJfI9nMk=") on (unknown)

Config file

$ cat .golangci.yml
run:
  modules-download-mode: readonly
  skip-dirs:
    - .git
    - .github
    - .vscode
    - bin
    - dist
    - docs
    - man
    - tools

linters:
  disable-all: true
  enable:
    - bodyclose
    - deadcode
    - dogsled
    - dupl
    - errcheck
    - exhaustive
    - exportloopref
    - goconst
    - gofmt
    - goimports
    - golint
    - gosec
    - gosimple
    - govet
    - ineffassign
    - misspell
    - nolintlint
    - prealloc
    - staticcheck
    - structcheck
    - stylecheck
    - typecheck
    - unconvert
    - unparam
    - unused
    - varcheck
    - whitespace

linters-settings:
  gofmt:
    simplify: true
  goimports:
    local-prefixes: github.com/lukasmalkmus/xxx
  govet:
    check-shadowing: true

Go environment

$ go version && go env
go version go1.15.4 darwin/amd64
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/lukasmalkmus/Library/Caches/go-build"
GOENV="/Users/lukasmalkmus/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/lukasmalkmus/Code/Go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/lukasmalkmus/Code/Go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.4/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.4/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/lukasmalkmus/Code/Private/xxx/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 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dy/s6dd70892l35f0444cwt96t40000gn/T/go-build856306592=/tmp/go-build -gno-record-gcc-switches -fno-common"

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /Users/lukasmalkmus/Code/Private/xxx /Users/lukasmalkmus/Code/Private /Users/lukasmalkmus/Code /Users/lukasmalkmus /Users /] 
INFO [config_reader] Used config file .golangci.yml 
INFO [lintersdb] Active 27 linters: [bodyclose deadcode dogsled dupl errcheck exhaustive exportloopref goconst gofmt goimports golint gosec gosimple govet ineffassign misspell nolintlint prealloc staticcheck structcheck stylecheck typecheck unconvert unparam unused varcheck whitespace] 
INFO [loader] Go packages loading at mode 575 (deps|exports_file|imports|name|types_sizes|compiled_files|files) took 12.275422ms 
ERRO Running error: context loading failed: failed to load packages: failed to load with go/packages: err: exit status 1: stderr: build flag -mod=readonly only valid when using modules 
INFO Memory: 2 samples, avg is 72.1MB, max is 72.1MB 
INFO Execution took 25.264712ms  

Build details above should be sufficient enough. TL;DR: latest macOS Catalina, golangci-lint 1.32.2

The error only occurs when using golang.org/x/tools v0.0.0-20201109182053-3db8fd265862. When downgrading to v0.0.0-20201022035929-9cf592e881e9 it works just fine.

bug

Most helpful comment

Same here.

go version go1.15.6 windows/amd64
golangci-lint has version 1.34.0 built from 260dc80 on 2020-12-28T16:20:11Z
GO111MODULE=on

Error:

level=error msg="Running error: context loading failed: failed to load packages: failed to load with go/packages: err: exit status 1: stderr: build flag -mod=readonly only valid when using modules

I have this option for .golangci.yml

modules-download-mode: readonly

If remove modules-download-mode from config error is disappeared

All 10 comments

Hey, thank you for opening your first Issue ! 馃檪 If you would like to contribute we have a guide for contributors.

Reproduced in v1.33.1 and not in v1.33.0

Modules with vendor folder are used, modules-download-mode: vendor in the configuration

Could someone explain what is wrong? Is it golanci-lint or Go tooling behavior, because I noticed that Go is upgraded from 1.15.5->1.15.6 in golangci-lint:v1.33.1

I guess something wrong with golangci-lint because go list -mod=vendor ./... works without errors

FYI the v1.33.1 has been replaced by v1.33.2 (based on v1.33.0) and v1.34.0 (based on master)

Hey guys - we can confirm we are seeing this same error for v1.34.0 as well in our builds. We've been able to resolve for now by pinning to v1.33.2 馃憤馃徎. Output from local lint here:

golangci/golangci-lint info checking GitHub for latest tag
golangci/golangci-lint info found version: 1.34.0 for v1.34.0/linux/amd64
golangci/golangci-lint info installed /go/bin/golangci-lint
level=info msg="[config_reader] Config search paths: [./ /code / /root]"
level=info msg="[config_reader] Used config file .golangci.yml"
level=info msg="[lintersdb] Active 17 linters: [deadcode errcheck gocritic gocyclo golint gosimple govet ineffassign maligned misspell staticcheck structcheck typecheck unconvert unparam unused varcheck]"
level=info msg="[loader] Go packages loading at mode 575 (compiled_files|deps|files|types_sizes|exports_file|imports|name) took 7.036ms"
level=error msg="Running error: context loading failed: failed to load packages: failed to load with go/packages: err: exit status 1: stderr: build flag -mod=readonly only valid when using modules\n"
level=info msg="Memory: 2 samples, avg is 71.6MB, max is 71.6MB"
level=info msg="Execution took 18.4588ms"
make: *** [lint] Error 3

Same here.

go version go1.15.6 windows/amd64
golangci-lint has version 1.34.0 built from 260dc80 on 2020-12-28T16:20:11Z
GO111MODULE=on

Error:

level=error msg="Running error: context loading failed: failed to load packages: failed to load with go/packages: err: exit status 1: stderr: build flag -mod=readonly only valid when using modules

I have this option for .golangci.yml

modules-download-mode: readonly

If remove modules-download-mode from config error is disappeared

The problem is related to a call to go list -mod=readonly -e -f {{context.ReleaseTags}}

https://github.com/golang/tools/blob/929a8494cf60267d89035bc211b797a67b65a6b9/go/packages/golist.go#L848-L852

This was introduced because of go1.16:

https://github.com/golang/tools/commit/2f4fa188d925fdee3467708b69aa4761a429ae9d

I'm currently trying to find if it's a real bug in golang.org/x/tools or if we need to set up something.

I think that the call creates this problem because of the hard-coded GO111MODULE=off

https://github.com/golang/tools/blob/929a8494cf60267d89035bc211b797a67b65a6b9/internal/gocommand/version.go#L18

$ GO111MODULE=off go list -mod=readonly -e -f {{context.ReleaseTags}}
build flag -mod=readonly only valid when using modules
$ go list -mod=readonly -e -f {{context.ReleaseTags}}
[go1.1 go1.2 go1.3 go1.4 go1.5 go1.6 go1.7 go1.8 go1.9 go1.10 go1.11 go1.12 go1.13 go1.14 go1.15]

I find a fix, I don't really know if it's a hack or just the expected behavior.

To understand, we have to look into the go command:

    case "readonly", "vendor", "mod":
        if !cfg.ModulesEnabled && !base.InGOFLAGS("-mod") {
            base.Fatalf("build flag -mod=%s only valid when using modules", cfg.BuildMod)
        }

https://github.com/golang/go/blob/4fd94558820100129b98f284e21b19fc27a99926/src/cmd/go/internal/work/init.go#L271-L274

Because of the hard-coded GO111MODULE=off the value of cfg.ModulesEnabled is false.

The second clause (!base.InGOFLAGS("-mod")) checks in the GOFLAGS env var to find a flag that contains -mod.
Currently, in the majority of the cases, the GOFLAGS env var is empty.

So the fix consists of adding the value -mod=%s into the env var GOFLAGS.

I opened an issue in the Go repository: https://github.com/golang/go/issues/43418

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cyriltovena picture cyriltovena  路  5Comments

kpeu3i picture kpeu3i  路  3Comments

grongor picture grongor  路  4Comments

simonpasquier picture simonpasquier  路  4Comments

ferhatelmas picture ferhatelmas  路  4Comments