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 · 5 comments
Open

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

eliasnaur opened this issue Sep 20, 2021 · 5 comments

Comments

@eliasnaur
Copy link
Contributor

@eliasnaur eliasnaur commented Sep 20, 2021

% 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 this to the Unreleased milestone Sep 20, 2021
@dr2chase
Copy link
Contributor

@dr2chase dr2chase commented Sep 21, 2021

Loading

@stamblerre
Copy link

@stamblerre stamblerre commented Sep 21, 2021

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?

Loading

@eliasnaur
Copy link
Contributor Author

@eliasnaur eliasnaur commented Sep 22, 2021

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

Loading

@stamblerre
Copy link

@stamblerre stamblerre commented Sep 22, 2021

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?

Loading

@stamblerre
Copy link

@stamblerre stamblerre commented Sep 22, 2021

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

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants