1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Package experiment_toolid_test verifies that GOEXPERIMENT settings built
8 // into the toolchain influence tool ids in the Go command.
9 // This test requires bootstrapping the toolchain twice, so it's very expensive.
10 // It must be run explicitly with -tags=explicit.
11 // Verifies golang.org/issue/33091.
23 func TestExperimentToolID(t *testing.T) {
25 goroot, err := os.MkdirTemp("", "experiment-goroot")
29 defer os.RemoveAll(goroot)
31 gorootSrc := filepath.Join(goroot, "src")
32 if err := overlayDir(gorootSrc, filepath.Join(runtime.GOROOT(), "src")); err != nil {
36 if err := os.WriteFile(filepath.Join(goroot, "VERSION"), []byte("go1.999"), 0666); err != nil {
39 env := append(os.Environ(), "GOROOT=", "GOROOT_BOOTSTRAP="+runtime.GOROOT())
42 gocache, err := os.MkdirTemp("", "experiment-gocache")
46 defer os.RemoveAll(gocache)
47 env = append(env, "GOCACHE="+gocache)
49 // Build the toolchain without GOEXPERIMENT.
53 makeScript = "make.bat"
55 makeScript = "make.rc"
57 makeScript = "make.bash"
59 makeScriptPath := filepath.Join(runtime.GOROOT(), "src", makeScript)
60 runCmd(t, gorootSrc, env, makeScriptPath)
62 // Verify compiler version string.
63 goCmdPath := filepath.Join(goroot, "bin", "go")
64 if runtime.GOOS == "windows" {
67 gotVersion := bytes.TrimSpace(runCmd(t, gorootSrc, env, goCmdPath, "tool", "compile", "-V=full"))
68 wantVersion := []byte(`compile version go1.999`)
69 if !bytes.Equal(gotVersion, wantVersion) {
70 t.Errorf("compile version without experiment: got %q, want %q", gotVersion, wantVersion)
73 // Build a package in a mode not handled by the make script.
74 runCmd(t, gorootSrc, env, goCmdPath, "build", "-race", "archive/tar")
76 // Rebuild the toolchain with GOEXPERIMENT.
77 env = append(env, "GOEXPERIMENT=fieldtrack")
78 runCmd(t, gorootSrc, env, makeScriptPath)
80 // Verify compiler version string.
81 gotVersion = bytes.TrimSpace(runCmd(t, gorootSrc, env, goCmdPath, "tool", "compile", "-V=full"))
82 wantVersion = []byte(`compile version go1.999 X:fieldtrack,framepointer`)
83 if !bytes.Equal(gotVersion, wantVersion) {
84 t.Errorf("compile version with experiment: got %q, want %q", gotVersion, wantVersion)
87 // Build the same package. We should not get a cache conflict.
88 runCmd(t, gorootSrc, env, goCmdPath, "build", "-race", "archive/tar")
91 func runCmd(t *testing.T, dir string, env []string, path string, args ...string) []byte {
92 cmd := exec.Command(path, args...)
95 out, err := cmd.Output()