Skip to main content 首页 创作者 beko2210 firstbrain bazel-build-optimization
bazel-build-optimization Optimize Bazel builds for large-scale monorepos. Use when configuring Bazel, implementing remote execution, or optimizing build performance for enterprise codebases.
跳到安装 Skills Marketplace 发现并探索由社区构建的 Agent Skills
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/BEKO2210/Firstbrain --skill bazel-build-optimization命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
下载 Zip 下载中... 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. type skill created 2026-02-27T00:00:00.000Z domain software-development category devtools risk unknown source community tags ["skill","software-development","devtools","bazel","build","optimization"]
Bazel Build Optimization
Production patterns for Bazel in large-scale monorepos.
Do not use this skill when
The task is unrelated to bazel build optimization
You need a different domain or tool outside this scope
Instructions
Clarify goals, constraints, and required inputs.
Apply relevant best practices and validate outcomes.
Provide actionable steps and verification.
If detailed examples are required, open resources/implementation-playbook.md.
Use this skill when
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
Concept Description Target Buildable unit (library, binary, test) Package Directory with BUILD file Label Target identifier //path/to:target Rule Defines how to build a target Aspect Cross-cutting build behavior
Templates
Template 1: WORKSPACE Configuration
workspace(name = "myproject" )
load("@bazel_tools//tools/build_defs/repo:http.bzl" , "http_archive" )
http_archive(
name = "aspect_rules_js" ,
sha256 = "..." ,
strip_prefix = "rules_js-1.34.0" ,
url = "https://github.com/aspect-build/rules_js/releases/download/v1.34.0/rules_js-v1.34.0.tar.gz" ,
)
load( , )
rules_js_dependencies()
load( , )
nodejs_register_toolchains(
name = ,
node_version = ,
)
load( , )
npm_translate_lock(
name = ,
pnpm_lock = ,
verify_node_modules_ignored = ,
)
load( , )
npm_repositories()
http_archive(
name = ,
sha256 = ,
strip_prefix = ,
url = ,
)
load( , )
py_repositories()
"@aspect_rules_js//js:repositories.bzl"
"rules_js_dependencies"
"@rules_nodejs//nodejs:repositories.bzl"
"nodejs_register_toolchains"
"nodejs"
"20.9.0"
"@aspect_rules_js//npm:repositories.bzl"
"npm_translate_lock"
"npm"
"//:pnpm-lock.yaml"
"//:.bazelignore"
"@npm//:repositories.bzl"
"npm_repositories"
"rules_python"
"..."
"rules_python-0.27.0"
"https://github.com/bazelbuild/rules_python/releases/download/0.27.0/rules_python-0.27.0.tar.gz"
"@rules_python//python:repositories.bzl"
"py_repositories"
Template 2: .bazelrc Configuration
build --enable_platform_specific_config
build --incompatible_enable_cc_toolchain_resolution
build --experimental_strict_conflict_checks
build --jobs =auto
build --local_cpu_resources=HOST_CPUS*.75
build --local_ram_resources=HOST_RAM*.75
build --disk_cache=~/.cache/bazel-disk
build --repository_cache=~/.cache/bazel-repo
build:remote-cache --remote_cache=grpcs://cache.example.com
build:remote-cache --remote_upload_local_results=true
build:remote-cache --remote_timeout=3600
build:remote-exec --remote_executor=grpcs://remote.example.com
build:remote-exec --remote_instance_name=projects/myproject/instances/default
build:remote-exec --jobs =500
build:linux --platforms=//platforms:linux_x86_64
build:macos --platforms=//platforms:macos_arm64
build:ci --config=remote-cache
build:ci --build_metadata=ROLE=CI
build:ci --bes_results_url=https://results.example.com/invocation/
build:ci --bes_backend=grpcs://bes.example.com
test --test_output=errors
test --test_summary=detailed
coverage --combined_report=lcov
coverage --instrumentation_filter="//..."
build:opt --compilation_mode=opt
build:dbg --compilation_mode=dbg
try-import %workspace%/user.bazelrc
Template 3: TypeScript Library BUILD
load("@aspect_rules_ts//ts:defs.bzl" , "ts_project" )
load("@aspect_rules_js//js:defs.bzl" , "js_library" )
load("@npm//:defs.bzl" , "npm_link_all_packages" )
npm_link_all_packages(name = "node_modules" )
ts_project(
name = "utils_ts" ,
srcs = glob(["src/**/*.ts" ]),
declaration = True ,
source_map = True ,
tsconfig = "//:tsconfig.json" ,
deps = [
":node_modules/@types/node" ,
],
)
js_library(
name = "utils" ,
srcs = [":utils_ts" ],
visibility = ["//visibility:public" ],
)
load("@aspect_rules_jest//jest:defs.bzl" , "jest_test" )
jest_test(
name = "utils_test" ,
config = "//:jest.config.js" ,
data = [
":utils" ,
"//:node_modules/jest" ,
],
node_modules = "//:node_modules" ,
)
Template 4: Python Library BUILD
load("@rules_python//python:defs.bzl" , "py_library" , "py_test" , "py_binary" )
load("@pip//:requirements.bzl" , "requirement" )
py_library(
name = "ml" ,
srcs = glob(["src/**/*.py" ]),
deps = [
requirement("numpy" ),
requirement("pandas" ),
requirement("scikit-learn" ),
"//libs/utils:utils_py" ,
],
visibility = ["//visibility:public" ],
)
py_test(
name = "ml_test" ,
srcs = glob(["tests/**/*.py" ]),
deps = [
":ml" ,
requirement("pytest" ),
],
size = "medium" ,
timeout = "moderate" ,
)
py_binary(
name = "train" ,
srcs = ["train.py" ],
deps = [":ml" ],
data = ["//data:training_data" ],
)
Template 5: Custom Rule for Docker
def _docker_image_impl (ctx ):
dockerfile = ctx.file.dockerfile
base_image = ctx.attr.base_image
layers = ctx.files.layers
output = ctx.actions.declare_file(ctx.attr.name + ".tar" )
args = ctx.actions.args()
args.add("--dockerfile" , dockerfile)
args.add("--output" , output)
args.add("--base" , base_image)
args.add_all("--layer" , layers)
ctx.actions.run(
inputs = [dockerfile] + layers,
outputs = [output],
executable = ctx.executable._builder,
arguments = [args],
mnemonic = "DockerBuild" ,
progress_message = "Building Docker image %s" % ctx.label,
)
return [DefaultInfo(files = depset([output]))]
docker_image = rule(
implementation = _docker_image_impl,
attrs = {
"dockerfile" : attr.label(
allow_single_file = [".dockerfile" , "Dockerfile" ],
mandatory = True ,
),
"base_image" : attr.string(mandatory = True ),
"layers" : attr.label_list(allow_files = True ),
"_builder" : attr.label(
default = "//tools/docker:builder" ,
executable = True ,
cfg = "exec" ,
),
},
)
Template 6: Query and Dependency Analysis
bazel query "deps(//apps/web:web)"
bazel query "rdeps(//..., //libs/utils:utils)"
bazel query "//libs/..."
bazel query "rdeps(//..., set($(git diff --name-only HEAD~1 | sed 's/.*/"&" /' | tr '\n' ' ') ))"
bazel query "deps(//apps/web:web)" --output=graph | dot -Tpng > deps.png
bazel query "kind('.*_test', //...)"
bazel query "attr(tags, 'integration', //...)"
bazel query "deps(//...)" --output=package | wc -l
Template 7: Remote Execution Setup
platform(
name = "linux_x86_64" ,
constraint_values = [
"@platforms//os:linux" ,
"@platforms//cpu:x86_64" ,
],
exec_properties = {
"container-image" : "docker://gcr.io/myproject/bazel-worker:latest" ,
"OSFamily" : "Linux" ,
},
)
platform(
name = "remote_linux" ,
parents = [":linux_x86_64" ],
exec_properties = {
"Pool" : "default" ,
"dockerNetwork" : "standard" ,
},
)
toolchain(
name = "cc_toolchain_linux" ,
exec_compatible_with = [
"@platforms//os:linux" ,
"@platforms//cpu:x86_64" ,
],
target_compatible_with = [
"@platforms//os:linux" ,
"@platforms//cpu:x86_64" ,
],
toolchain = "@remotejdk11_linux//:jdk" ,
toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type" ,
)
Performance Optimization
bazel build //... --profile=profile.json
bazel analyze-profile profile.json
bazel build //... --execution_log_json_file=exec_log.json
bazel build //... --memory_profile=memory.json
bazel build //... --notrack_incremental_state
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
Resources
Connections
Domain: [[Software Entwicklung]]
Kategorie: [[Entwickler-Tools]]
Navigation: [[Skills Uebersicht]], [[Home]]