Labsco
wshobson logo

bazel-build-optimization

โ˜… 37,559

by wshobson ยท part of wshobson/agents

Optimize Bazel builds for large-scale monorepos. Use when configuring Bazel, implementing remote execution, or optimizing build performance for enterprise codebases.

๐Ÿงฉ One of 7 skills in the wshobson/agents package โ€” works on its own, and pairs well with its siblings.

This is the playbook your agent receives when the skill activates โ€” you don't need to read it to use the skill, but it's here to audit before installing.

Bazel Build Optimization

Production patterns for Bazel in large-scale monorepos.

When to Use This Skill

  • Setting up Bazel for monorepos
  • Configuring remote caching/execution
  • Optimizing build times
  • Writing custom Bazel rules
  • Debugging build issues
  • Migrating to Bazel

Core Concepts

1. Bazel Architecture

workspace/
โ”œโ”€โ”€ WORKSPACE.bazel       # External dependencies
โ”œโ”€โ”€ .bazelrc              # Build configurations
โ”œโ”€โ”€ .bazelversion         # Bazel version
โ”œโ”€โ”€ BUILD.bazel           # Root build file
โ”œโ”€โ”€ apps/
โ”‚   โ””โ”€โ”€ web/
โ”‚       โ””โ”€โ”€ BUILD.bazel
โ”œโ”€โ”€ libs/
โ”‚   โ””โ”€โ”€ utils/
โ”‚       โ””โ”€โ”€ BUILD.bazel
โ””โ”€โ”€ tools/
    โ””โ”€โ”€ bazel/
        โ””โ”€โ”€ rules/

2. Key Concepts

ConceptDescription
TargetBuildable unit (library, binary, test)
PackageDirectory with BUILD file
LabelTarget identifier //path/to:target
RuleDefines how to build a target
AspectCross-cutting build behavior

Templates and detailed worked examples

Full template library and detailed worked examples live in references/details.md. Read that file when you need the concrete templates.

Best Practices

Do's

  • Use fine-grained targets - Better caching
  • Pin dependencies - Reproducible builds
  • Enable remote caching - Share build artifacts
  • Use visibility wisely - Enforce architecture
  • Write BUILD files per directory - Standard convention

Don'ts

  • Don't use glob for deps - Explicit is better
  • Don't commit bazel-* dirs - Add to .gitignore
  • Don't skip WORKSPACE setup - Foundation of build
  • Don't ignore build warnings - Technical debt