Skip to content

Commit

Permalink
bazel: stage all go source files in a temp dir named after the package
Browse files Browse the repository at this point in the history
This works around an issue where Go source file names as stored in the
`cockroach` binary were truncated (e.g., with the leading
`github.com/cockroachdb/cockroach` prefix, or even the entire package
name prefix, removed). This breaks unit tests (#61913) and some other
internal stuff.

We solve this by staging all Go source files during the build in a
temporary directory named after the package. This incurs an additional
I/O cost, but for now while our codebase isn't able to deal with the
differing file names, we can deal with it.

Fixes #64379
See also #64383

Release note: None
  • Loading branch information
rickystewart committed May 13, 2021
1 parent 5817ba2 commit 1b78c30
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 28 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
# Load go bazel tools. This gives us access to the go bazel SDK/toolchains.
git_repository(
name = "io_bazel_rules_go",
commit = "2fe8a6256c818840cc9a10cf3f366d8410437245",
commit = "a94a425d26532613e71f9c7432c19c36302e1f1d",
remote = "https://github.com/cockroachdb/rules_go",
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/json/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ go_test(
data = glob(["testdata/**"]),
embed = [":json"],
deps = [
"//pkg/build/bazel",
"//pkg/sql/inverted",
"//pkg/sql/pgwire/pgerror",
"//pkg/testutils",
"//pkg/util/encoding",
"//pkg/util/randutil",
"//pkg/util/timeutil",
Expand Down
30 changes: 4 additions & 26 deletions pkg/util/json/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import (
"io/ioutil"
"math/rand"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/cockroachdb/cockroach/pkg/build/bazel"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)

Expand Down Expand Up @@ -63,18 +62,7 @@ func TestJSONRandomEncodeRoundTrip(t *testing.T) {
}

func TestFilesEncode(t *testing.T) {
_, fname, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("couldn't get directory")
}
dir := filepath.Join(filepath.Dir(fname), "testdata", "raw")
if bazel.BuiltWithBazel() {
runfile, err := bazel.Runfile(dir)
if err != nil {
t.Fatal(err)
}
dir = runfile
}
dir := testutils.TestDataPath(t, "raw")
dirContents, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -121,18 +109,8 @@ func TestFilesEncode(t *testing.T) {
// rerun with -rewrite-results-in-testfiles.
t.Run(`explicit encoding`, func(t *testing.T) {
stringifiedEncoding := fmt.Sprintf("%v", encoded)
fixtureFilename := filepath.Join(
filepath.Dir(fname),
"testdata", "encoded",
tc.Name()+".bytes",
)
if bazel.BuiltWithBazel() {
runfile, err := bazel.Runfile(fixtureFilename)
if err != nil {
t.Fatal(err)
}
fixtureFilename = runfile
}
fixtureFilename := testutils.TestDataPath(
t, "encoded", tc.Name()+".bytes")

if *rewriteResultsInTestfiles {
err := ioutil.WriteFile(fixtureFilename, []byte(stringifiedEncoding), 0644)
Expand Down

0 comments on commit 1b78c30

Please sign in to comment.