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: "could not determine GOARCH and Go compiler" error text is not sufficient to explain what what went wrong #30355

Open
dmitshur opened this issue Feb 22, 2019 · 1 comment

Comments

@dmitshur
Copy link
Member

commented Feb 22, 2019

This happens when using Go 1.11 only. Go 1.12 or newer is not affected.

If the user writes the following Go program that uses packages.Load, and sets Config.Dir to a directory that doesn't contain a module:

package main

import (
	"fmt"
	"log"

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

func main() {
	cfg := &packages.Config{
		Mode: packages.LoadSyntax,
		Dir:  "/tmp/empty",
	}
	pkgs, err := packages.Load(cfg, "foobar/...")
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("loaded %d packages\n", len(pkgs))
}

Then when running said program in module mode by setting GO111MODULE=on, they get an error that is not very helpful at figuring out what went wrong:

$ GO111MODULE=on go run .
2019/02/20 17:10:21 could not determine GOARCH and Go compiler

This isn't sufficient information for the user to find out why that error was returned, other than to look into the source code of packages and debug it themselves.

If Config.Mode is left at its default LoadFiles value, the error is better at hinting what went wrong:

$ GO111MODULE=on go run .
2019/02/20 17:10:08 go [list -e -json -compiled -test=false -export=false -deps=false -find=true -- foobar/...]: exit status 1: go: cannot find main module; see 'go help modules'

exit status 1

Cause

In Go 1.12, it's possible to run GO111MODULE=on go list unsafe in any directory successfully. In Go 1.11, running it outside of a module gives the following error:

$ GO111MODULE=on go list unsafe
go: cannot find main module; see 'go help modules'

One of the actions packages.Load performs is invoking the go list -f "{{context.GOARCH}} {{context.Compiler}}" -- unsafe command in order to determine the current GOARCH and Go compiler name. If it fails, it returns an error that makes sense in the local context:

https://github.com/golang/tools/blob/83362c3779f5f48611068d488a03ea7bbaddc81e/go/internal/packagesdriver/sizes.go#L90

But not when it's returned to the caller of packages.Load.

Perhaps it can be augmented to provide more details, such as:

could not determine GOARCH and Go compiler: cannot find main module at /tmp/empty

Given that this affects only Go 1.11 and not newer, and happens only when the Dir is incorrectly specified, the value of fixing this is not very high.

/cc @julieqiu @matloob

@gopherbot gopherbot added this to the Unreleased milestone Feb 22, 2019

@dmitshur

This comment has been minimized.

Copy link
Member Author

commented Feb 22, 2019

I've been made aware of two debug env vars. Using them makes it possible to get more information about what went wrong:

issue30355 $ GO111MODULE=on go run main.go
2019/02/22 14:53:05 could not determine GOARCH and Go compiler
exit status 1

issue30355 $ GOPACKAGESDEBUG=1 GO111MODULE=on go run main.go
2019/02/22 14:53:12 29.409141ms for GOROOT= GOPATH= GO111MODULE=on PWD=/tmp/issue30355 go [list -e -json -compiled -test=false -export=true -deps=true -find=false -- foobar/...]
2019/02/22 14:53:12 could not determine GOARCH and Go compiler
exit status 1

issue30355 $ GOPACKAGESPRINTGOLISTERRORS=1 GO111MODULE=on go run main.go
GOROOT= GOPATH= GO111MODULE=on PWD=/tmp/issue30355 go [list -e -json -compiled -test=false -export=true -deps=true -find=false -- foobar/...] stderr: <<go: cannot find main module; see 'go help modules'
>>
GOROOT= GOPATH= GO111MODULE=on PWD=/tmp/issue30355 go [list -f {{context.GOARCH}} {{context.Compiler}} -- unsafe] stderr: <<go: cannot find main module; see 'go help modules'
>>
2019/02/22 14:53:20 could not determine GOARCH and Go compiler
exit status 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.