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

Closed
dmitshur opened this issue Feb 22, 2019 · 3 comments
Labels
FrozenDueToAge 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

@dmitshur
Copy link
Contributor

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 dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 22, 2019
@dmitshur
Copy link
Contributor Author

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

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 12, 2019
@marwan-at-work
Copy link
Contributor

FYI, in Go 1.13 the same error occurs if a go.mod file has a local replace clause but the file path is incorrect.

I think that if packages.Config does not ask for NeedDeps or any LoadMode that requires parsing dependencies, then we should not encounter this error.

Here's my packages Config:

	var c packages.Config
	c.Mode = packages.NeedTypes | packages.NeedSyntax
	pkgs, err := packages.Load(&c, "main.go")

Where main.go is a simple file but has a go.mod with a bad replace clause

Here are the debug logs:

2019/10/07 04:14:06 23.5453ms for GOROOT= GOPATH=/go GO111MODULE= PWD=/my-app go "env" "GOMOD", stderr: <<>>
2019/10/07 04:14:07 430.095ms for GOROOT= GOPATH=/go GO111MODULE= PWD=/my-app go "list" "-m" "-json" "all", stderr: <<go: notexists@v0.0.0-00010101000000-000000000000: parsing /Users/marwansulaiman/notexists/go.mod: open /Users/marwansulaiman/notexists/go.mod: no such file or directory
>>
2019/10/07 04:14:06 23.5453ms for GOROOT= GOPATH=/go GO111MODULE= PWD=/my-app go "env" "GOMOD", stderr: <<>>
2019/10/07 04:14:07 430.095ms for GOROOT= GOPATH=/go GO111MODULE= PWD=/my-app go "list" "-m" "-json" "all", stderr: <<go: marwan.io/notexists@v0.0.0-00010101000000-000000000000: parsing /Users/marwansulaiman/marwan.io/notexists/go.mod: open /Users/marwansulaiman/marwan.io/notexists/go.mod: no such file or directory
>>
2019/10/07 04:14:07 438.4959ms for GOROOT= GOPATH=/go GO111MODULE= PWD=/my-app go "list" "-e" "-json" "-compiled=true" "-test=false" "-export=true" "-deps=false" "-find=false" "--" "api.pb.go", stderr: <<go: marwan.io/notexists@v0.0.0-00010101000000-000000000000: parsing /Users/marwansulaiman/marwan.io/notexists/go.mod: open /Users/marwansulaiman/marwan.io/notexists/go.mod: no such file or directory
>>
run /Users/marwansulaiman/marwan.io/notexists: plugin error: error loading api.pb.go: could not determine GOARCH and Go compiler

@gopherbot
Copy link

Change https://golang.org/cl/204357 mentions this issue: go/internal/packagesdriver: report stderr when failed to determine types.Sizes

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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

3 participants