TExcelTAD
Home › Monorepo Builds: Bazel (+ Buck2 & Pants) › Field Notes: .bazelrc & BUILD Hygiene

Field Notes: .bazelrc & BUILD Hygiene

Bazel repositories rarely fail in dramatic ways. They decay: a .bazelrc nobody understands, BUILD files with glob(["**"]), a local = True someone added under deadline pressure, visibility set to public "for now". These notes are the habits that keep a Bazel repository healthy, each with the failure it prevents.

1. Structure .bazelrc in layers

text
# .bazelrc: committed, shared by everyone
common --enable_platform_specific_config
common --lockfile_mode=update
build  --incompatible_strict_action_env          # explicit, even where it's the default
build  --cxxopt=-std=c++20
test   --test_output=errors
test   --test_summary=terse

build:linux --copt=-fdiagnostics-color=never     # applied automatically on Linux

# bazel build --config=ci //...
common:ci --lockfile_mode=error
build:ci  --keep_going
build:ci  --remote_cache=grpcs://cache.example.com
build:ci  --remote_upload_local_results=true
build:ci  --build_event_json_file=bep.json

# bazel build --config=release
build:release --compilation_mode=opt --stamp

try-import %workspace%/user.bazelrc               # personal, git-ignored

Rules that keep it understandable:

Habit Because
use common for flags that change the configuration build-only flags make query/cquery/test see a different configuration and discard the analysis cache when you switch commands
one named config per environment (ci, release, asan, remote) --config=ci is self-documenting; a wall of flags in CI YAML isn't
a comment per non-obvious flag in a year nobody remembers why --noexperimental_check_output_files is there
personal flags only in user.bazelrc / ~/.bazelrc --disk_cache paths and --jobs differ per machine
bazel build --announce_rc when two machines disagree prints exactly which rc lines applied

2. Globs: precise, and never empty

bash · try it yourself
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir -p lib/src lib/test other/src
cat > lib/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(name = "everything", srcs = glob(["**/*.cc"]))              # too broad
cc_library(name = "precise", srcs = glob(["src/*.cc"]))
EOF
cat > other/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(name = "typo", srcs = glob(["source/*.cc"]))                # matches nothing
EOF
echo 'int other() { return 2; }' > other/src/other.cc
echo 'int helper() { return 1; }' > lib/src/helper.cc
echo 'int main() { return 0; }' > lib/test/helper_test.cc
echo "== what each glob matched"
bazel query 'labels(srcs, //lib:everything)' 2>/dev/null
bazel query 'labels(srcs, //lib:precise)' 2>/dev/null
echo "== the typo"
bazel build //other:typo 2>&1 | grep -o "glob pattern 'source/\*.cc' didn't match anything.*" | cut -c1-120
== what each glob matched
//lib:src/helper.cc
//lib:test/helper_test.cc
//lib:src/helper.cc
== the typo
glob pattern 'source/*.cc' didn't match anything, but allow_empty is set to False (the default value of allow_empty can
  • **/*.cc swept the test's main() into the library. Recursive globs at the top of a large package also make every new file anywhere below change the target, and slow down loading on big trees.
  • An empty glob is an error in Bazel 8+ (--incompatible_disallow_empty_glob), which catches typos and moved directories. The error is raised while loading the package, so every target in that package fails with it, which is why the typo lives in its own package here. Pass allow_empty = True only when emptiness is legitimate.
  • Prefer explicit srcs lists for small targets and narrow, per-directory globs for large ones; never glob(["**"]).

3. local = True and no-sandbox need a reason

Every local = True, tags = ["no-sandbox"], "no-remote" or "no-cache" removes a guarantee (Sandboxing showed the stale output it allows). Require a comment and an owner:

python
genrule(
    name = "keychain_export",
    outs = ["certs.pem"],
    cmd = "security find-certificate -a -p > $@",
    # no-sandbox: macOS `security` needs the user's keychain. Owner: @platform-team. TODO(INFRA-123)
    tags = ["no-sandbox", "no-remote"],
)

A bazel query 'attr(tags, "no-sandbox|no-remote|local", //...)' in CI that compares against an allow-list keeps the number from creeping up.

4. BUILD files: small targets, narrow visibility

Habit Because
one target per directory or per concept, not one per repository finer targets = more cache hits, smaller test fan-out, enforceable layering
package(default_visibility = ["//visibility:private"]) (the default) and explicit exports public-by-default makes every internal helper an API forever
implementation_deps for C++, exports sparingly for Java stops header/class leaks through transitive deps
testonly = True on test helpers fakes never reach production binaries
no logic in BUILD files: macros in .bzl files with docstrings BUILD files stay greppable and tool-editable (buildozer, Gazelle)
run buildifier --lint=warn in CI consistent formatting makes diffs reviewable

5. Pin everything, upgrade deliberately

.bazelversion, MODULE.bazel + MODULE.bazel.lock, toolchain versions (go_sdk.download(version = …), python.toolchain(python_version = …)), maven_install.json, pip lock files. Then upgrade on purpose: one Bazel minor version at a time with bazelisk --bisect ready, rule sets in their own PRs, and the release notes' incompatible flag list read before flipping anything. Bazel announces breaking changes as --incompatible_* flags one release ahead; turning them on early in CI (build:next --incompatible_…) avoids surprises at upgrade time.

Test yourself

Why prefer common over build in .bazelrc for flags like --enable_platform_specific_config?

  1. It's shorter
  2. query/cquery/test see the same options, so switching commands doesn't discard the analysis cache
  3. build lines are ignored
  4. common lines are faster to parse

What happens in Bazel 9 when a glob matches no files?

  1. Nothing
  2. It's an error unless allow_empty = True
  3. A warning only
  4. The target is skipped

What should accompany every tags = ["no-sandbox"]?

  1. Nothing
  2. A comment with the reason and an owner, ideally tracked by a CI allow-list
  3. A second target
  4. --spawn_strategy=local globally