]> Cypherpunks repositories - gostls13.git/commit
cmd/go: run tests when cmd/go is cross-compiled
authorBryan C. Mills <bcmills@google.com>
Fri, 15 Oct 2021 15:35:25 +0000 (11:35 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 18 Aug 2022 14:52:30 +0000 (14:52 +0000)
commite64c87157d1e8fbc512a670b8c0af8abc3afa7c1
tree7db458aa825bad56d005d1ca5c69b8509b19af98
parentd8f90ce0f8119bf593efb6fb91825de5b61fcda7
cmd/go: run tests when cmd/go is cross-compiled

When the GOOS or GOARCH of the cmd/go test binary does not match the
GOOS or GOARCH of the installed 'go' binary itself, the test currently
attempts to trick 'go test' into thinking that there were no test
functions to run.

That makes it very difficult to discover how to actually run the
tests, which in turn makes it difficult to diagnose and fix
regressions in, say, the linux-386-longtest builders. (We have had a
few of those lately, and they shouldn't be as much of an ordeal to fix
as they currently are.)

There are three underlying problems:

1. cmd/go uses its own GOOS and GOARCH to figure out which variant of
   other tools to use, and the cache keys for all installed tools and
   libraries include the IDs of the tools used to build them. So when
   cmd/go's GOARCH changes, all installed tools and binaries appear
   stale *even if* they were just installed by invoking the native
   cmd/go with the appropriate GOARCH value set.

2. The "go/build" library used by cmd/go toggles its default
   CGO_ENABLED behavior depending on whether the GOOS and GOARCH being
   imported match runtime.GOOS and runtime.GOARCH.

3. A handful of cmd/go tests explicitly use gccgo, but the user's
   installed gccgo binary cannot necessarily cross-compile to the same
   platforms as cmd/go.

To address the cache-invalidation problem, we modify the test variant
of cmd/go to use the host's native toolchain (as indicated by the new
TESTGO_GOHOSTOS and TESTGO_GOHOSTARCH environment variables) instead
of the toolchain matching the test binary itself. That allows a test
cmd/go binary compiled with GOARCH=386 to use libraries and tools
cross-compiled by the native toolchain, so that

$ GOARCH=386 go install std cmd

suffices to make the packages in std and cmd non-stale in the
tests.

To address the CGO_ENABLED mismatch, we set CGO_ENABLED explicitly in
the test's environment whenever it may differ from the default. Since
script tests that use cgo are already expected to use a [cgo]
condition, setting the environment to match that condition fixes the
cgo-specific tests.

To address the gccgo-specific cross-compilation failures, we add a new
script condition, [cross], which evaluates to true whenever the
platform of the test binary differs from that of the native toolchain.
We can then use that condition to explicitly skip the handful of gccgo
tests that fail under cross-compilation.

Fixes #53936.

Change-Id: I8633944f674eb5941ccc95df928991660e7e8137
Reviewed-on: https://go-review.googlesource.com/c/go/+/356611
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
17 files changed:
src/cmd/go/go_test.go
src/cmd/go/internal/base/tool.go
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/clean/clean.go
src/cmd/go/internal/envcmd/env.go
src/cmd/go/internal/fmtcmd/fmt.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/test/flagdefs_test.go
src/cmd/go/internal/tool/tool.go
src/cmd/go/internal/work/exec.go
src/cmd/go/script_test.go
src/cmd/go/testdata/script/build_overlay.txt
src/cmd/go/testdata/script/build_trimpath.txt
src/cmd/go/testdata/script/gccgo_m.txt
src/cmd/go/testdata/script/install_cmd_gobin.txt
src/cmd/go/testdata/script/mod_outside.txt
src/cmd/go/testdata/script/vendor_complex.txt