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.
Register custom build rules, providers, and toolchains in Starlark. Use when creating rules for new languages, wrapping build tools, or debugging rule implementations during Bazel's analysis phase.
/plugin marketplace add gaarutyunov/dev-skills/plugin install bazel@dev-skillsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
references/api-reference.mdreferences/go-patterns.mdreferences/providers.mdreferences/testing.mdreferences/toolchains.mdBazel 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.
| 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 |
# 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",
),
},
)
Providers are the mechanism for rules to communicate:
MyInfo = provider(
doc = "Information from my_library targets",
fields = {
"files": "depset of output files",
"transitive_files": "depset of all transitive files",
},
)
def _impl(ctx):
# Collect from dependencies
transitive = [dep[MyInfo].transitive_files for dep in ctx.attr.deps]
# Return new provider
return [
DefaultInfo(files = depset([output])),
MyInfo(
files = depset([output]),
transitive_files = depset([output], transitive = transitive),
),
]
Require providers in deps:
"deps": attr.label_list(providers = [MyInfo])
Use depsets to avoid quadratic complexity:
# GOOD: O(1) per target
transitive_srcs = depset(
direct = ctx.files.srcs,
transitive = [dep[MyInfo].srcs for dep in ctx.attr.deps],
)
# BAD: O(n) copying - becomes O(n^2) across graph
transitive_srcs = list(ctx.files.srcs)
for dep in ctx.attr.deps:
transitive_srcs.extend(dep[MyInfo].srcs.to_list())
Only call .to_list() in binary rules, never in libraries.
# Run executable (preferred)
ctx.actions.run(
mnemonic = "Compile",
executable = ctx.executable._tool,
arguments = [args],
inputs = inputs,
outputs = [output],
)
# Use args builder for large inputs
args = ctx.actions.args()
args.add("-o", output)
args.add_all(srcs) # Deferred expansion - efficient!
# Write file content
ctx.actions.write(output, content, is_executable = True)
| Type | Use Case |
|---|---|
attr.label_list | File/target inputs (srcs, deps) |
attr.label | Single target (_compiler) |
attr.string | Config value (importpath) |
attr.bool | Toggle (cgo = True) |
Private attributes (underscore prefix) for implicit deps:
"_stdlib": attr.label(default = "//go:stdlib")
cfg for tools:
"_compiler": attr.label(executable = True, cfg = "exec")
ctx.toolchains["//my:type"]ctx.files.srcs, ctx.attr.depsctx.actions.declare_file()ctx.actions.run()[DefaultInfo(...), MyInfo(...)]| Mistake | Fix |
|---|---|
| Flattening depsets in library | Keep as depset, flatten only in binary |
Missing cfg = "exec" for tools | Add cfg = "exec" to tool attributes |
| Not returning DefaultInfo | Always return DefaultInfo with files |
| Reading files in analysis | Files can only be read by actions |
| Action without outputs | All actions must produce output files |
See references/ for detailed guides:
api-reference.md - Starlark API quick referenceproviders.md - Provider design patternstoolchains.md - Toolchain developmenttesting.md - Testing Bazel rulesgo-patterns.md - Patterns from rules_goThis skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.