Skip to content

Commit

Permalink
Suppress goroot path in tool builds
Browse files Browse the repository at this point in the history
The goroot is encoded in the output binary, but the goroot is different
in different workspaces. As a result tool binaries (such as builder) are
not reproducible. Bazel will have cache misses for build involving
builder even if the resulting output from the build is repoducible.

Similar go_binary builds, suppress the goroot path.
  • Loading branch information
adobke committed Sep 15, 2021
1 parent 70b8365 commit 6b9b36c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def _go_tool_binary_impl(ctx):
arguments = [largs],
inputs = sdk.libs + sdk.headers + sdk.tools + [cout],
outputs = [out],
env = {"GOROOT_FINAL": "GOROOT"}, # Suppress go root paths to keep the output reproducible.
mnemonic = "GoToolchainBinary",
)

Expand Down
18 changes: 18 additions & 0 deletions tests/integration/reproducibility/reproducibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -215,6 +216,23 @@ func Test(t *testing.T) {
if err := compareHashes(dirHashes[0], dirHashes[2]); err == nil {
t.Fatalf("dir0 and dir2 are the same)", len(dirHashes[0]))
}

// Check that the go_sdk path doesn't appear in the builder binary. This path is different
// nominally different per workspace (but in these tests, the go_sdk paths are all set to the same
// path in WORKSPACE) -- so if this path is in the builder binary, then builds between workspaces
// would be partially non cacheable.
builder_file, err := os.Open(filepath.Join(dirs[0], "bazel-out", "host", "bin", "external", "go_sdk", "builder"))
if err != nil {
t.Fatal(err);
}
defer builder_file.Close()
builder_data, err := ioutil.ReadAll(builder_file)
if err != nil {
t.Fatal(err)
}
if bytes.Index(builder_data, []byte("go_sdk")) != -1 {
t.Fatalf("Found go_sdk path in builder binary, builder tool won't be reproducible")
}
}

func copyTree(dstRoot, srcRoot string) error {
Expand Down

0 comments on commit 6b9b36c

Please sign in to comment.