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: Config.Env doesn't support -modfile #48482

Open
eliasnaur opened this issue Sep 20, 2021 · 6 comments
Open

x/tools/go/packages: Config.Env doesn't support -modfile #48482

eliasnaur opened this issue Sep 20, 2021 · 6 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@eliasnaur
Copy link
Contributor

% go version
go version go1.17 darwin/amd64
% cat main.go go.mod
package main

import (
	"log"
	"os"

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

func main() {
	_, err := packages.Load(&packages.Config{
		Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps,
		Env:  os.Environ(),
	}, ".")
	if err != nil {
		log.Fatal(err)
	}
}
module example.com

go 1.17

require golang.org/x/tools v0.1.6

require (
	golang.org/x/mod v0.4.2 // indirect
	golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
	golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
% go run .
% GOFLAGS=-modfile=go.mod go run .
2021/09/20 08:48:46 err: exit status 1: stderr: build flag -modfile only valid when using modules
exit status 1

I'd like to support GOFLAGS=-modfile=... from tools that use packages.Load, but perhaps more concerning is that the error message indicates some go tool commands are run in non-module mode.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 20, 2021
@gopherbot gopherbot added this to the Unreleased milestone Sep 20, 2021
@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 21, 2021
@dr2chase
Copy link
Contributor

@golang/tools-team

@stamblerre
Copy link
Contributor

go/packages runs a number of Go command invocations, not just one, so the -modfile flag won't work when set through an environment variable. We can't clear out the value of GOFLAGS when running the first go list to get the Go version, which is why you get this error. Can you set -modfile through the BuildFlags field in the config instead?

@eliasnaur
Copy link
Contributor Author

go/packages runs a number of Go command invocations, not just one, so the -modfile flag won't work when set through an environment variable. We can't clear out the value of GOFLAGS when running the first go list to get the Go version, which is why you get this error.

Isn't the point of GOFLAGS to affect all go tool invocations, and be ignored for the irrelevant? From the error message (build flag -modfile only valid when using modules) it seems the problem is that x/packages run non-module commands. If that's unavoidable, perhaps the go tool should ignore GOFLAGS=-modfile instead of complaining. I found a similar case where GOFLAGS=-modfile is ignored: https://go-review.googlesource.com/c/go/+/258297.

Can you set -modfile through the BuildFlags field in the config instead?

Not for my use case, where I want a command line tool (gogio) to act as a wrapper for the go tool:

$ GOFLAGS=-modfile=... go build gioui.org/example/kitchen                  # regular build
$ GOFLAGS=-modfile=... gogio -target android gioui.org/example/kitchen     # wrapped build

@stamblerre
Copy link
Contributor

The issue is that https://pkg.go.dev/golang.org/x/tools/internal/gocommand#GoVersion runs with GO111MODULE=off. I'm not totally sure why--I believe that @heschi wrote this code, so maybe he knows?

@stamblerre
Copy link
Contributor

@hyangah pointed out that #34273 is likely the reason for this. We will consider ignoring GOFLAGS for this go list invocation.

@oscarciurana
Copy link

Any progress on this issue?
Same problem running golangci-lint using GOFLAGS=-modfile=...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. 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