diff --git a/BUILD.bazel b/BUILD.bazel index 5a64b2365..0a1d10355 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -260,6 +260,7 @@ filegroup( srcs = glob( ["**/*.go"], allow_empty = True, + exclude = ["bazel-*/**"], ), visibility = ["//:__subpackages__"], ) diff --git a/scripts/bazel_policy_test.go b/scripts/bazel_policy_test.go index cf5f1e31f..46246acac 100644 --- a/scripts/bazel_policy_test.go +++ b/scripts/bazel_policy_test.go @@ -351,6 +351,29 @@ func diffStringSets(want, got []string) (missing, extra []string) { return missing, extra } +// rootGoSrcsGlobRe matches the glob of the root package's go_srcs filegroup. +var rootGoSrcsGlobRe = regexp.MustCompile(`(?s)filegroup\(\s*name\s*=\s*"go_srcs",\s*srcs\s*=\s*glob\((.*?)\),\s*visibility`) + +// Bazel's convenience symlinks (bazel-bin, bazel-out, bazel-testlogs, +// bazel-beads) sit at the repository root, and a glob follows them into the +// output tree. CI's test step is the first build in a fresh checkout, but in +// any checkout that has built before (every agent worktree) an unexcluded +// root glob(["**/*.go"]) puts the output base's .go files into //:go_srcs and +// so into every test that reads it (//cmd/bd's test targets): their inputs +// change on every build, they never come from the cache, and loading them +// takes ~36 s instead of ~3 s (433,026 such files after a few runs, measured +// 2026-10-19, ga-ylpbsy.24). +func TestBazelRootGoSrcsSkipsConvenienceSymlinks(t *testing.T) { + root := sourceRepoRoot(t) + m := rootGoSrcsGlobRe.FindStringSubmatch(stripStarlarkComments(readPolicyFile(t, root, "BUILD.bazel"))) + if m == nil { + t.Fatal("BUILD.bazel has no root go_srcs glob: the block tools/bazel/go_srcs.py writes changed shape; update rootGoSrcsGlobRe") + } + if !strings.Contains(m[1], `"bazel-*/**"`) { + t.Errorf("the root //:go_srcs glob does not exclude \"bazel-*/**\", so it follows Bazel's convenience symlinks into the output tree; fix tools/bazel/go_srcs.py and run `make bazel-sync`") + } +} + // goSrcsTrees are the tools/bazel/go_srcs.py TREES roots. A test that walks // one of these trees through its tree_go_srcs sees only the packages listed // there, so an unlisted package makes that walk pass vacuously. diff --git a/tools/bazel/go_srcs.py b/tools/bazel/go_srcs.py index db584cf99..5b5da55bb 100755 --- a/tools/bazel/go_srcs.py +++ b/tools/bazel/go_srcs.py @@ -199,15 +199,23 @@ def packages_under(root: str) -> list[str]: return sorted(found) +# Bazel's convenience symlinks (bazel-bin, bazel-out, bazel-testlogs, +# bazel-beads) sit at the repository root, and a glob follows them into the +# output tree: unexcluded, the root go_srcs would hold the output base's .go +# files in any checkout that has built before, so every test that reads it +# would re-run (and reload) on each build (scripts/bazel_policy_test.go +# TestBazelRootGoSrcsSkipsConvenienceSymlinks). +ROOT_GO_SRCS_EXCLUDE = ["bazel-*/**"] + + def block(pkg: str, tree_members: list[str] | None) -> str: + glob = _partition_glob(("**/*.go",), ROOT_GO_SRCS_EXCLUDE if pkg == "." else [], allow_empty=True) lines = [ BEGIN, "", "filegroup(", ' name = "go_srcs",', - ' srcs = glob(', - ' ["**/*.go"],', - " allow_empty = True,", + *glob, " ),", ' visibility = ["//:__subpackages__"],', ")",