{
  "schema": "https://ai-atoms.com/schemas/skill-v1.json",
  "type": "skill",
  "id": "skill/bazel-build-optimization",
  "version": "1.0.0",
  "name": "Bazel Build Optimization",
  "description": "Optimize Bazel builds for large-scale monorepos. Use when configuring Bazel, implementing remote execution, or optimizing build performance for enterprise codebases.",
  "system_prompt_fragment": "# Bazel Build Optimization\n\nProduction patterns for Bazel in large-scale monorepos.\n\n## Do not use this skill when\n\n- The task is unrelated to bazel build optimization\n- You need a different domain or tool outside this scope\n\n## Instructions\n\n- Clarify goals, constraints, and required inputs.\n- Apply relevant best practices and validate outcomes.\n- Provide actionable steps and verification.\n- If detailed examples are required, open `resources/implementation-playbook.md`.\n\n## Use this skill when\n\n- Setting up Bazel for monorepos\n- Configuring remote caching/execution\n- Optimizing build times\n- Writing custom Bazel rules\n- Debugging build issues\n- Migrating to Bazel\n\n## Core Concepts\n\n### 1. Bazel Architecture\n\n```\nworkspace/\n├── WORKSPACE.bazel       # External dependencies\n├── .bazelrc              # Build configurations\n├── .bazelversion         # Bazel version\n├── BUILD.bazel           # Root build file\n├── apps/\n│   └── web/\n│       └── BUILD.bazel\n├── libs/\n│   └── utils/\n│       └── BUILD.bazel\n└── tools/\n    └── bazel/\n        └── rules/\n```\n\n### 2. Key Concepts\n\n| Concept | Description |\n|---------|-------------|\n| **Target** | Buildable unit (library, binary, test) |\n| **Package** | Directory with BUILD file |\n| **Label** | Target identifier `//path/to:target` |\n| **Rule** | Defines how to build a target |\n| **Aspect** | Cross-cutting build behavior |\n\n## Templates\n\n### Template 1: WORKSPACE Configuration\n\n```python\n# WORKSPACE.bazel\nworkspace(name = \"myproject\")\n\nload(\"@bazel_tools//tools/build_defs/repo:http.bzl\", \"http_archive\")\n\n# Rules for JavaScript/TypeScript\nhttp_archive(\n    name = \"aspect_rules_js\",\n    sha256 = \"...\",\n    strip_prefix = \"rules_js-1.34.0\",\n    url = \"https://github.com/aspect-build/rules_js/releases/download/v1.34.0/rules_js-v1.34.0.tar.gz\",\n)\n\nload(\"@aspect_rules_js//js:repositories.bzl\", \"rules_js_dependencies\")\nrules_js_dependencies()\n\nload(\"@rules_nodejs//nodejs:repositories.bzl\", \"nodejs_register_toolchains\")\nnodejs_register_toolchains(\n    name = \"nodejs\",\n    node_version = \"20.9.0\",\n)\n\nload(\"@aspect_rules_js//npm:repositories.bzl\", \"npm_translate_lock\")\nnpm_translate_lock(\n    name = \"npm\",\n    pnpm_lock = \"//:pnpm-lock.yaml\",\n    verify_node_modules_ignored = \"//:.bazelignore\",\n)\n\nload(\"@npm//:repositories.bzl\", \"npm_repositories\")\nnpm_repositories()\n\n# Rules for Python\nhttp_archive(\n    name = \"rules_python\",\n    sha256 = \"...\",\n    strip_prefix = \"rules_python-0.27.0\",\n    url = \"https://github.com/bazelbuild/rules_python/releases/download/0.27.0/rules_python-0.27.0.tar.gz\",\n)\n\nload(\"@rules_python//python:repositories.bzl\", \"py_repositories\")\npy_repositories()\n```\n\n### Template 2: .bazelrc Configuration\n\n```bash\n# .bazelrc\n\n# Build settings\nbuild --enable_platform_specific_config\nbuild --incompatible_enable_cc_toolchain_resolution\nbuild --experimental_strict_conflict_checks\n\n# Performance\nbuild --jobs=auto\nbuild --local_cpu_resources=HOST_CPUS*.75\nbuild --local_ram_resources=HOST_RAM*.75\n\n# Caching\nbuild --disk_cache=~/.cache/bazel-disk\nbuild --repository_cache=~/.cache/bazel-repo\n\n# Remote caching (optional)\nbuild:remote-cache --remote_cache=grpcs://cache.example.com\nbuild:remote-cache --remote_upload_local_results=true\nbuild:remote-cache --remote_timeout=3600\n\n# Remote execution (optional)\nbuild:remote-exec --remote_executor=grpcs://remote.example.com\nbuild:remote-exec --remote_instance_name=projects/myproject/instances/default\nbuild:remote-exec --jobs=500\n\n# Platform configurations\nbuild:linux --platforms=//platforms:linux_x86_64\nbuild:macos --platforms=//platforms:macos_arm64\n\n# CI configuration\nbuild:ci --config=remote-cache\nbuild:ci --build_metadata=ROLE=CI\nbuild:ci --bes_results_url=https://results.example.com/invocation/\nbuild:ci --bes_backend=grpcs://bes.example.com\n\n# Test settings\ntest --test_output=errors\ntest --test_summary=detailed\n\n# Coverage\ncoverage --combined_report=lcov\ncoverage --instrumentation_filter=\"//...\"\n\n# Convenience aliases\nbuild:opt --compilation_mode=opt\nbuild:dbg --compilation_mode=dbg\n\n# Import user settings\ntry-import %workspace%/user.bazelrc\n```\n\n### Template 3: TypeScript Library BUILD\n\n```python\n# libs/utils/BUILD.bazel\nload(\"@aspect_rules_ts//ts:defs.bzl\", \"ts_project\")\nload(\"@aspect_rules_js//js:defs.bzl\", \"js_library\")\nload(\"@npm//:defs.bzl\", \"npm_link_all_packages\")\n\nnpm_link_all_packages(name = \"node_modules\")\n\nts_project(\n    name = \"utils_ts\",\n    srcs = glob([\"src/**/*.ts\"]),\n    declaration = True,\n    source_map = True,\n    tsconfig = \"//:tsconfig.json\",\n    deps = [\n        \":node_modules/@types/node\",\n    ],\n)\n\njs_library(\n    name = \"utils\",\n    srcs = [\":utils_ts\"],\n    visibility = [\"//visibility:public\"],\n)\n\n# Tests\nload(\"@aspect_rules_jest//jest:defs.bzl\", \"jest_test\")\n\njest_test(\n    name = \"utils_test\",\n    config = \"//:jest.config.js\",\n    data = [\n        \":utils\",\n        \"//:node_modules/jest\",\n    ],\n    node_modules = \"//:node_modules\",\n)\n```\n\n### Template 4: Python Library BUILD\n\n```python\n# libs/ml/BUILD.bazel\nload(\"@rules_python//python:defs.bzl\", \"py_library\", \"py_test\", \"py_binary\")\nload(\"@pip//:requirements.bzl\", \"requirement\")\n\npy_library(\n    name = \"ml\",\n    srcs = glob([\"src/**/*.py\"]),\n    deps = [\n        requirement(\"numpy\"),\n        requirement(\"pandas\"),\n        requirement(\"scikit-learn\"),\n        \"//libs/utils:utils_py\",\n    ],\n    visibility = [\"//visibility:public\"],\n)\n\npy_test(\n    name = \"ml_test\",\n    srcs = glob([\"tests/**/*.py\"]),\n    deps = [\n        \":ml\",\n        requirement(\"pytest\"),\n    ],\n    size = \"medium\",\n    timeout = \"moderate\",\n)\n\npy_binary(\n    name = \"train\",\n    srcs = [\"train.py\"],\n    deps = [\":ml\"],\n    data = [\"//data:training_data\"],\n)\n```\n\n### Template 5: Custom Rule for Docker\n\n```python\n# tools/bazel/rules/docker.bzl\ndef _docker_image_impl(ctx):\n    dockerfile = ctx.file.dockerfile\n    base_image = ctx.attr.base_image\n    layers = ctx.files.layers\n\n    # Build the image\n    output = ctx.actions.declare_file(ctx.attr.name + \".tar\")\n\n    args = ctx.actions.args()\n    args.add(\"--dockerfile\", dockerfile)\n    args.add(\"--output\", output)\n    args.add(\"--base\", base_image)\n    args.add_all(\"--layer\", layers)\n\n    ctx.actions.run(\n        inputs = [dockerfile] + layers,\n        outputs = [output],\n        executable = ctx.executable._builder,\n        arguments = [args],\n        mnemonic = \"DockerBuild\",\n        progress_message = \"Building Docker image %s\" % ctx.label,\n    )\n\n    return [DefaultInfo(files = depset([output]))]\n\ndocker_image = rule(\n    implementation = _docker_image_impl,\n    attrs = {\n        \"dockerfile\": attr.label(\n            allow_single_file = [\".dockerfile\", \"Dockerfile\"],\n            mandatory = True,\n        ),\n        \"base_image\": attr.string(mandatory = True),\n        \"layers\": attr.label_list(allow_files = True),\n        \"_builder\": attr.label(\n            default = \"//tools/docker:builder\",\n            executable = True,\n            cfg = \"exec\",\n        ),\n    },\n)\n```\n\n### Template 6: Query and Dependency Analysis\n\n```bash\n# Find all dependencies of a target\nbazel query \"deps(//apps/web:web)\"\n\n# Find reverse dependencies (what depends on this)\nbazel query \"rdeps(//..., //libs/utils:utils)\"\n\n# Find all targets in a package\nbazel query \"//libs/...\"\n\n# Find changed targets since commit\nbazel query \"rdeps(//..., set($(git diff --name-only HEAD~1 | sed 's/.*/\"&\"/' | tr '\\n' ' ')))\"\n\n# Generate dependency graph\nbazel query \"deps(//apps/web:web)\" --output=graph | dot -Tpng > deps.png\n\n# Find all test targets\nbazel query \"kind('.*_test', //...)\"\n\n# Find targets with specific tag\nbazel query \"attr(tags, 'integration', //...)\"\n\n# Compute build graph size\nbazel query \"deps(//...)\" --output=package | wc -l\n```\n\n### Template 7: Remote Execution Setup\n\n```python\n# platforms/BUILD.bazel\nplatform(\n    name = \"linux_x86_64\",\n    constraint_values = [\n        \"@platforms//os:linux\",\n        \"@platforms//cpu:x86_64\",\n    ],\n    exec_properties = {\n        \"container-image\": \"docker://gcr.io/myproject/bazel-worker:latest\",\n        \"OSFamily\": \"Linux\",\n    },\n)\n\nplatform(\n    name = \"remote_linux\",\n    parents = [\":linux_x86_64\"],\n    exec_properties = {\n        \"Pool\": \"default\",\n        \"dockerNetwork\": \"standard\",\n    },\n)\n\n# toolchains/BUILD.bazel\ntoolchain(\n    name = \"cc_toolchain_linux\",\n    exec_compatible_with = [\n        \"@platforms//os:linux\",\n        \"@platforms//cpu:x86_64\",\n    ],\n    target_compatible_with = [\n        \"@platforms//os:linux\",\n        \"@platforms//cpu:x86_64\",\n    ],\n    toolchain = \"@remotejdk11_linux//:jdk\",\n    toolchain_type = \"@bazel_tools//tools/jdk:runtime_toolchain_type\",\n)\n```\n\n## Performance Optimization\n\n```bash\n# Profile build\nbazel build //... --profile=profile.json\nbazel analyze-profile profile.json\n\n# Identify slow actions\nbazel build //... --execution_log_json_file=exec_log.json\n\n# Memory profiling\nbazel build //... --memory_profile=memory.json\n\n# Skip analysis cache\nbazel build //... --notrack_incremental_state\n```\n\n## Best Practices\n\n### Do's\n- **Use fine-grained targets** - Better caching\n- **Pin dependencies** - Reproducible builds\n- **Enable remote caching** - Share build artifacts\n- **Use visibility wisely** - Enforce architecture\n- **Write BUILD files per directory** - Standard convention\n\n### Don'ts\n- **Don't use glob for deps** - Explicit is better\n- **Don't commit bazel-* dirs** - Add to .gitignore\n- **Don't skip WORKSPACE setup** - Foundation of build\n- **Don't ignore build warnings** - Technical debt\n\n## Resources\n\n- [Bazel Documentation](https://bazel.build/docs)\n- [Bazel Remote Execution](https://bazel.build/docs/remote-execution)\n- [rules_js](https://github.com/aspect-build/rules_js)",
  "applicable_domains": [
    "frontend"
  ],
  "category": "frontend",
  "invocation": [
    "/bazel-build-optimization"
  ],
  "authored_by": "claudeskills.in community",
  "source_url": "https://claudeskills.in/skill/bazel-build-optimization",
  "provenance": {
    "source": "claudeskills.in",
    "source_url": "https://claudeskills.in/skill/bazel-build-optimization",
    "license": "unknown",
    "imported_at": "2026-09-03",
    "notes": "Aggregated by claudeskills.in from community GitHub lists."
  },
  "tags": [
    "claudeskills",
    "frontend"
  ],
  "lifecycle": "draft"
}