Commit 48ec5122 authored by Daniel Martí's avatar Daniel Martí

cmd/go: move GOOS/GOARCH and tags checks to Init

They were in Do, which is the method that actually starts the build.
However, we can already run these checks in Init, since we already have
all the information necessary to do the checks.

More importantly, some work happens between Init and Do, namely package
loading. That may exit with an error, meaning that in some cases the
user gets a confusing error instead of the correct one.

For example, using a GOOS typo, before showed:

	$ GOOS=windwos go build
	can't load package: package p: build constraints exclude all Go files in ...

And now:

	$ GOOS=windwos go build
	cmd/go: unsupported GOOS/GOARCH pair windwos/amd64

Also had to tweak TestGoEnv to modify GOOS as well as GOARCH. Otherwise,
on windows this would result in the invalid GOOS/GOARCH pair
windows/arm, which would error given that we now check that in non-build
commands such as "go env".

Fixes #21999.

Change-Id: Iff9890dea472bff0179a9d703d6f698a0e3187c1
Reviewed-on: https://go-review.googlesource.com/65656
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
parent 9f7fd893
...@@ -3820,6 +3820,7 @@ func TestGoEnv(t *testing.T) { ...@@ -3820,6 +3820,7 @@ func TestGoEnv(t *testing.T) {
tg := testgo(t) tg := testgo(t)
tg.parallel() tg.parallel()
defer tg.cleanup() defer tg.cleanup()
tg.setenv("GOOS", "freebsd") // to avoid invalid pair errors
tg.setenv("GOARCH", "arm") tg.setenv("GOARCH", "arm")
tg.run("env", "GOARCH") tg.run("env", "GOARCH")
tg.grepStdout("^arm$", "GOARCH not honored") tg.grepStdout("^arm$", "GOARCH not honored")
...@@ -4589,3 +4590,12 @@ func TestParallelNumber(t *testing.T) { ...@@ -4589,3 +4590,12 @@ func TestParallelNumber(t *testing.T) {
}) })
} }
} }
func TestWrongGOOSErrorBeforeLoadError(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
tg.setenv("GOOS", "windwos")
tg.runFail("build", "exclude")
tg.grepStderr("unsupported GOOS/GOARCH pair", "GOOS=windwos go build exclude did not report 'unsupported GOOS/GOARCH pair'")
}
...@@ -733,6 +733,17 @@ func (b *Builder) Init() { ...@@ -733,6 +733,17 @@ func (b *Builder) Init() {
base.AtExit(func() { os.RemoveAll(workdir) }) base.AtExit(func() { os.RemoveAll(workdir) })
} }
} }
if _, ok := cfg.OSArchSupportsCgo[cfg.Goos+"/"+cfg.Goarch]; !ok && cfg.BuildContext.Compiler == "gc" {
fmt.Fprintf(os.Stderr, "cmd/go: unsupported GOOS/GOARCH pair %s/%s\n", cfg.Goos, cfg.Goarch)
os.Exit(2)
}
for _, tag := range cfg.BuildContext.BuildTags {
if strings.Contains(tag, ",") {
fmt.Fprintf(os.Stderr, "cmd/go: -tags space-separated list contains comma\n")
os.Exit(2)
}
}
} }
// readpkglist returns the list of packages that were built into the shared library // readpkglist returns the list of packages that were built into the shared library
...@@ -1104,17 +1115,6 @@ func allArchiveActions(root *Action) []*Action { ...@@ -1104,17 +1115,6 @@ func allArchiveActions(root *Action) []*Action {
// do runs the action graph rooted at root. // do runs the action graph rooted at root.
func (b *Builder) Do(root *Action) { func (b *Builder) Do(root *Action) {
if _, ok := cfg.OSArchSupportsCgo[cfg.Goos+"/"+cfg.Goarch]; !ok && cfg.BuildContext.Compiler == "gc" {
fmt.Fprintf(os.Stderr, "cmd/go: unsupported GOOS/GOARCH pair %s/%s\n", cfg.Goos, cfg.Goarch)
os.Exit(2)
}
for _, tag := range cfg.BuildContext.BuildTags {
if strings.Contains(tag, ",") {
fmt.Fprintf(os.Stderr, "cmd/go: -tags space-separated list contains comma\n")
os.Exit(2)
}
}
// Build list of all actions, assigning depth-first post-order priority. // Build list of all actions, assigning depth-first post-order priority.
// The original implementation here was a true queue // The original implementation here was a true queue
// (using a channel) but it had the effect of getting // (using a channel) but it had the effect of getting
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment