Optimize Bazel Builds for Large Monorepos
Production Bazel patterns for large monorepos: WORKSPACE/BUILD templates, remote caching, and query-based dependency analysis.
Why it matters
Streamline your large-scale monorepo development by optimizing Bazel build performance. This skill provides production-ready patterns for efficient build configurations, remote caching, and execution.
Outcomes
What it gets done
Configure Bazel for monorepo environments.
Implement remote caching and execution strategies.
Optimize build times and reduce resource consumption.
Debug and resolve common Bazel build issues.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-bazel-build-optimization | bash Overview
Bazel Build Optimization
Production Bazel patterns for large monorepos: WORKSPACE and BUILD.bazel templates for JS/TS and Python, .bazelrc performance and remote caching/execution configuration, a custom Docker build rule, and bazel query patterns for dependency analysis. Use when setting up, optimizing, or migrating a monorepo's Bazel build; not needed for small projects where a simpler build tool suffices.
What it does
Production patterns for Bazel in large-scale monorepos, covering workspace architecture, remote caching and execution, build performance optimization, custom Bazel rules, build debugging, and Bazel migration. Core concepts: Targets (buildable units), Packages (directories with a BUILD file), Labels (//path/to:target), Rules (how to build a target), and Aspects (cross-cutting build behavior).
When to use - and when NOT to
Use this when setting up Bazel for a monorepo, configuring remote caching or execution, optimizing build times, writing custom Bazel rules, debugging build issues, or migrating an existing build to Bazel.
Not needed for small single-language projects where a simpler build tool is sufficient, or for tasks unrelated to Bazel build optimization.
Inputs and outputs
Provides seven ready-to-adapt templates: a WORKSPACE.bazel configuration registering JS/TS (aspect_rules_js, a Node toolchain, npm_translate_lock) and Python (rules_python) dependencies; a .bazelrc covering performance flags like --jobs=auto and CPU/RAM resource limits, disk and repository caching, optional remote caching and remote execution configs, platform-specific and CI build configurations, and test/coverage settings; TypeScript and Python library BUILD.bazel files (ts_project/js_library/jest_test, and py_library/py_test/py_binary with pip requirements); a custom Bazel rule implementing a docker_image build action; bazel query patterns for dependency analysis - deps, reverse deps, changed-targets-since-commit, dependency graph generation, tag-based filtering; and a remote-execution platform and toolchain configuration, including exec_properties for a Docker-based worker container and a Linux/x86_64 cc_toolchain definition. The custom Docker rule's implementation declares a .tar output file, assembles dockerfile/base-image/layer arguments, and runs a builder executable via ctx.actions.run, returning a DefaultInfo provider with the built image.
### Find all dependencies of a target
bazel query "deps(//apps/web:web)"
### Find reverse dependencies (what depends on this)
bazel query "rdeps(//..., //libs/utils:utils)"
Integrations
Performance-optimization commands profile builds (--profile=profile.json plus bazel analyze-profile), identify slow actions (--execution_log_json_file), profile memory (--memory_profile), and skip the analysis cache when needed. Best practices: use fine-grained targets for better caching, pin dependencies for reproducible builds, enable remote caching to share artifacts, use visibility deliberately to enforce architecture boundaries, and write one BUILD file per directory. Anti-patterns to avoid: using glob for dependencies instead of explicit lists, committing bazel-* output directories, skipping WORKSPACE setup, and ignoring build warnings. References Bazel's own documentation, its remote-execution docs, and the rules_js project.
Who it's for
Build and platform engineers setting up, optimizing, or migrating a large monorepo's Bazel build, who need concrete, copy-adaptable WORKSPACE, BUILD, and .bazelrc templates and query patterns rather than generic Bazel concepts explained from first principles.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.