Use when creating custom Bazel rules, toolchains, providers, or aspects. Use when extending Bazel for new languages, build systems, or custom actions. Use when debugging Starlark rule implementations or understanding Bazel build phases.
View on GitHubgaarutyunov/dev-skills
bazel
bazel/skills/developing-bazel-rules/SKILL.md
January 21, 2026
Select agents to install to:
npx add-skill https://github.com/gaarutyunov/dev-skills/blob/main/bazel/skills/developing-bazel-rules/SKILL.md -a claude-code --skill developing-bazel-rulesInstallation paths:
.claude/skills/developing-bazel-rules/# Developing Bazel Rules
## Overview
Bazel rules define how to transform inputs into outputs through actions. A rule has an implementation function executed during analysis phase that registers actions for execution phase.
**Core principle:** Rules don't execute commands directly - they register actions that Bazel executes later based on dependency analysis.
## When to Use
- Creating rules for new languages or tools
- Building custom toolchains
- Implementing providers for dependency propagation
- Creating aspects for cross-cutting concerns
- Wrapping existing tools with Bazel
## Build Phases
| Phase | What Happens | Rule Author's Role |
|-------|--------------|-------------------|
| **Loading** | `BUILD` files evaluated, rules instantiated | Define rule with `rule()`, macros expand |
| **Analysis** | Implementation functions run | Register actions, return providers |
| **Execution** | Actions run (if needed) | Actions produce outputs |
## Quick Start: Minimal Rule
```python
# my_rules.bzl
def _my_compile_impl(ctx):
output = ctx.actions.declare_file(ctx.label.name + ".out")
ctx.actions.run(
mnemonic = "MyCompile",
executable = ctx.executable._compiler,
arguments = ["-o", output.path] + [f.path for f in ctx.files.srcs],
inputs = ctx.files.srcs,
outputs = [output],
)
return [DefaultInfo(files = depset([output]))]
my_compile = rule(
implementation = _my_compile_impl,
attrs = {
"srcs": attr.label_list(allow_files = True),
"_compiler": attr.label(
default = "//tools:compiler",
executable = True,
cfg = "exec",
),
},
)
```
## Core Concepts
### Providers - Passing Data Between Rules
Providers are the mechanism for rules to communicate:
```python
MyInfo = provider(
doc = "Information from my_library targets",
fields = {
"files": "depset of output files",
"transitive_files": "depset of all transitive files",