sweet/go-build: set GOROOT correctly

Currently we just use a bare exec.Command and GOROOT may not be set
correctly for the first time we build it. That's an easy fix: use a
common.Go instead.

In the benchmark itself we still need to use a bare exec.Command, but
that's fine. Just set GOROOT in that case explicitly.

Change-Id: I9e00076d3d951ba5b83b63ddabfebcf68b60f73d
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/384654
Reviewed-by: Michael Pratt <mpratt@google.com>
Trust: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/sweet/benchmarks/go-build/main.go b/sweet/benchmarks/go-build/main.go
index 4f652cb..2a77d44 100644
--- a/sweet/benchmarks/go-build/main.go
+++ b/sweet/benchmarks/go-build/main.go
@@ -16,6 +16,7 @@
 	"github.com/google/pprof/profile"
 	"golang.org/x/benchmarks/sweet/benchmarks/internal/cgroups"
 	"golang.org/x/benchmarks/sweet/benchmarks/internal/driver"
+	"golang.org/x/benchmarks/sweet/common"
 )
 
 var (
@@ -80,6 +81,7 @@
 		baseCmd = exec.Command(goTool, cmdArgs...)
 	}
 	baseCmd.Dir = pkgPath
+	baseCmd.Env = common.NewEnvFromEnviron().MustSet("GOROOT=" + filepath.Dir(filepath.Dir(goTool))).Collapse()
 	cmd, err := cgroups.WrapCommand(baseCmd, "test.scope")
 	if err != nil {
 		return err
diff --git a/sweet/harnesses/go-build.go b/sweet/harnesses/go-build.go
index aefd5a3..55982bd 100644
--- a/sweet/harnesses/go-build.go
+++ b/sweet/harnesses/go-build.go
@@ -88,11 +88,9 @@
 		}
 
 		// Build the benchmark once, pulling in any requisite packages.
-		cmd := exec.Command(cfg.GoTool().Tool, "build")
-		cmd.Dir = filepath.Join(bcfg.BinDir, bench.name, bench.pkg)
-		log.TraceCommand(cmd, false)
-		// Call Output here to get an *ExitError with a populated Stderr field.
-		if _, err := cmd.Output(); err != nil {
+		pkgPath := filepath.Join(bcfg.BinDir, bench.name, bench.pkg)
+		dummyBin := filepath.Join(bcfg.BinDir, "dummy")
+		if err := cfg.GoTool().BuildPath(pkgPath, dummyBin); err != nil {
 			return err
 		}
 	}