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

x/tools/go/packages: error during the call to packages.Load with -mod other than empty. #43418

Closed
ldez opened this issue Dec 29, 2020 · 3 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@ldez
Copy link

ldez commented Dec 29, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15.6 linux/amd6

Does this issue reproduce with the latest release?

It's not related to a specific version of Go.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ldez/.cache/go-build"
GOENV="/home/ldez/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ldez/sources/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ldez/sources/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/ldez/.gvm/gos/go1.15.6"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/ldez/.gvm/gos/go1.15.6/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build165529633=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"fmt"

	"golang.org/x/tools/go/packages"
)

func main() {
	conf := &packages.Config{
		BuildFlags: []string{"-mod=readonly"},
	}

	pkgs, err := packages.Load(conf)

	fmt.Println(err, pkgs)
}

The problem is related to the state.getGoVersion() call:

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

This call was introduced in golang/tools@2f4fa18

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

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

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

Related to golangci/golangci-lint#1502

What did you expect to see?

<nil> [.]

What did you see instead?

err: exit status 1: stderr: build flag -mod=readonly only valid when using modules
 []
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Dec 29, 2020
@gopherbot gopherbot added this to the Unreleased milestone Dec 29, 2020
@stamblerre
Copy link
Contributor

Thank you for reporting this! I'm able to reproduce--the issue is that we clear out the invocation's Mod flag, but we don't remove any -mod= flag from the build flags. I'll have a fix soon.

@gopherbot
Copy link

Change https://golang.org/cl/280694 mentions this issue: go/packages: remove -mod, -modfile flags from build flags for go version

@toothrot toothrot added the NeedsFix The path to resolution is known, but the work has not been done. label Jan 5, 2021
gopherbot pushed a commit to golang/tools that referenced this issue Jan 11, 2021
Use provided build flags may include -mod or -modfile, and these should
be removed before any go command calls that don't accept them.

Fixes golang/go#43418

Change-Id: Ie96626bced5093c67fc1890533f1f8cc03d42c80
Reviewed-on: https://go-review.googlesource.com/c/tools/+/280694
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
@fc
Copy link

fc commented Feb 16, 2021

Upgrading to go 1.16 resolved it for me.

@golang golang locked and limited conversation to collaborators Feb 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants