npx skills add ...
npx skills add facebook/buck2 --skill buck2-rule-basics
npx skills add facebook/buck2 --skill buck2-rule-basics
Guide users through writing their first Buck2 rule to learn fundamental concepts including rules, actions, targets, configurations, analysis, and select(). Use this skill when users want to learn Buck2 basics hands-on or need help understanding rule writing.
@nolint
This is an interactive, step-by-step tutorial that teaches Buck2 fundamentals through hands-on practice. You'll guide users through writing a simple text processing rule that converts text to uppercase, explaining core concepts as they encounter them.
This skill includes additional reference documentation that you can use to answer deeper questions:
references/concepts.md - Deep dive into Buck2 core concepts including:
references/advanced_patterns.md - Production-ready patterns including:
When to use these references:
concepts.mdadvanced_patterns.mdAlways read from these files when users ask questions that go beyond the basic tutorial content.
DO NOT dump all content at once! This is an interactive tutorial. Follow these rules:
When the skill launches, FIRST check what the user has already done:
After completing each major step (1-8), do NOT simply ask "ready to continue?". Verify the user actually understood the step before advancing by posing a comprehension check and having them answer in their own words.
Ask the question as plain text, then stop and wait for the user's
typed reply. Do NOT use AskUserQuestion or offer answer choices — the
user should compose a free-response answer, not pick from a list. Having
to articulate the idea themselves, with no options to recognize, is what
reveals whether they actually understood it.
Generate the question yourself — do not hardcode it. Look at the concepts you just explained and the code you just wrote together, then craft a question that probes the "why," not just the "what." A good question asks the user to predict, explain, or apply ("what would happen if...", "why did X occur", "which line is responsible for Y") rather than recite a definition.
Act on the answer:
Keep it to one or two questions per step — a comprehension check, not an exam. The user can always ask their own question instead, or say they want to skip ahead; honor that.
Illustrative only — the shape to aim for, not a script to copy:
We built
:helloand it built successfully but wrote no output file. Why do you think that happened?
Then wait for their answer. What you're listening for here: the impl ran
during analysis and returned an empty DefaultInfo, so no action was
ever registered to produce a file.
Use TodoWrite to show:
This tutorial uses the system buck2 command, NOT ./buck2.py.
buck2 build, buck2 test, buck2 cquery, etc../buck2.py (that's for Buck2 development/self-bootstrap)This ensures the tutorial works for all users with Buck2 installed.
The tutorial has 8 progressive steps:
Create a new directory for the tutorial and navigate into it:
Run this:
All following steps will be done in this directory.
Step 1: Create Minimal Rule Stub - Returns empty DefaultInfo() Step 2: Add Source File Attribute - Accept input files Step 3: Declare Output Artifact - Promise to produce output (will error) Step 4: Create an Action - Actually produce the output Step 5: Understanding Targets - Unconfigured vs Configured Step 6: Add Configuration Support - Use select() for platform-specific behavior Step 7: Add Dependencies - Make rules compose Step 8: Rules vs Macros - Understand the difference
Create todo list:
Check existing state:
Ask user:
Goal: Get the simplest possible Buck2 rule working.
What to do:
uppercase.bzl with minimal implementationBUCK file with target definitionbuck2 buildCode to create:
uppercase.bzl:
BUCK:
Testing:
Key concepts to explain AFTER successful build:
rule() functionAnalysisContext, returns Provider listBefore moving on — Socratic check:
Run a comprehension check (see "Use the Socratic Method Between Major Steps"), generating the question from what this step covered. Aim it at whether they grasp that the impl runs during the analysis phase, and why the build succeeded despite producing no output. Advance only once they show they understand.
Goal: Make the rule accept an input file.
What to do:
uppercase.bzl to add src attributeBUCK to pass a source fileinput.txt test fileUpdate uppercase.bzl:
Update BUCK:
Create input.txt:
Testing:
Key concepts to explain:
attrs={}, accessed via ctx.attrsBefore moving on — Socratic check:
Run a comprehension check (see "Use the Socratic Method Between Major
Steps"), generating the question from what this step covered. Aim it at
the gap between declaring an attribute and producing output — why adding
src still yields no output, and what attrs.source() actually does.
Advance only once they show they understand.
Goal: Declare that we'll produce an output (will cause expected error).
What to do:
Update uppercase.bzl implementation:
Testing:
Explain the error: This error is expected and good! We declared an output but haven't created an action to produce it. Buck2 is telling us: "You promised an output, but didn't say how to make it!"
Key concepts to explain:
Before moving on — Socratic check:
Run a comprehension check (see "Use the Socratic Method Between Major
Steps"), generating the question from what this step covered. Aim it at
why the build errored — that declare_output() only promises an artifact
while nothing yet produces it (declaration vs production). Advance only
once they show they understand.
Goal: Actually produce the output file by running a command.
What to do:
ctx.actions.run()Update uppercase.bzl implementation:
Testing:
Key concepts to explain:
> in shell scripts (Buck2 doesn't support
redirect_stdout)Before moving on — Socratic check:
Run a comprehension check (see "Use the Socratic Method Between Major
Steps"), generating the question from what this step covered. Aim it at
what as_output() and ctx.actions.run() do, and when the action
actually runs (lazy execution) — i.e. why adding the action fixed the
error. Advance only once they show they understand.
Goal: Understand target names, unconfigured vs configured targets.
What to do:
buck2 targetsuquery)cquery)Run commands:
Explain target name anatomy:
A full Buck2 target name has three parts:
Examples with explanations:
fbcode//buck2/buck2-tutorial:hello
//buck2/buck2-tutorial:hello
// is shorthand for fbcode//:hello
buck2 build :hello from the buck2-tutorial directory, Buck2 knows you mean fbcode//buck2/buck2-tutorial:helloNow query the targets:
Key concepts to explain:
Unconfigured Target (//path:name):
select() expressions not yet resolvedConfigured Target (//path:name (cfg:...)):
select() resolved based on platform (linux/windows/mac)Show the difference:
fbcode//buck2/buck2-tutorial:hellofbcode//buck2/buck2-tutorial:hello (cfg:dev-linux-x86_64-...)Notice the configuration suffix (cfg:...) is added when Buck2 applies platform-specific settings.
Before moving on — Socratic check:
Run a comprehension check (see "Use the Socratic Method Between Major
Steps"), generating the question from what this step covered. Aim it at
the difference between unconfigured and configured targets — what the
(cfg:...) suffix means, or the parts of a target name. Advance only
once they show they understand.
Goal: Make the rule behave differently on different platforms.
What to do:
output_name attribute to the ruleselect() in TARGETS to choose different names per platformUpdate rule in uppercase.bzl:
Update BUCK to use select():
Testing:
Key concepts to explain:
Before moving on — Socratic check:
Run a comprehension check (see "Use the Socratic Method Between Major
Steps"), generating the question from what this step covered. Aim it at
when and how select() resolves (the configuration phase), and what
differs between the uquery and cquery output for output_name.
Advance only once they show they understand.
Goal: Make rules that depend on other rules.
What to do:
deps attribute to the ruleUpdate rule in uppercase.bzl:
Add to BUCK:
Create input2.txt:
Testing:
Key concepts to explain:
dep[ProviderType]Before moving on — Socratic check:
Run a comprehension check (see "Use the Socratic Method Between Major
Steps"), generating the question from what this step covered. Aim it at
how Buck2 knows to build :hello before :goodbye, and how the rule
reaches a dependency's output via dep[DefaultInfo]. Advance only once
they show they understand.
Goal: Understand when to use rules vs macros.
What to do:
Add macro to uppercase.bzl:
Add to BUCK:
Testing:
Key concepts to explain:
Rules:
rule()buck2 cquery/uqueryMacros:
When to use macros:
Before wrapping up — Socratic check:
Run a comprehension check (see "Use the Socratic Method Between Major
Steps"), generating the question from what this step covered. Aim it at
the rule-vs-macro distinction — which phase each runs in, and why the
macro itself doesn't appear in buck2 cquery/uquery. Wrap up only once
they show they understand.
Final wrap-up:
Here's how a typical interactive session should flow:
User: "I want to learn Buck2"
Claude: "Great! I'll guide you through writing your first Buck2 rule. We'll build a simple text converter that uppercases files. I'll guide you step-by-step - there are 8 steps total, and we'll test after each one.
Let me check if you have any existing tutorial files..."
[Checks for files]
"Looks like you're starting fresh! I'll create a todo list to track our progress."
[Creates todo list with 8 steps]
"Ready to start with Step 1: Creating a minimal rule?"
[Waits for confirmation]
User: "Yes"
Claude: "Perfect! Step 1 is about creating the simplest possible Buck2 rule - one that does nothing but is valid.
I'll create two files:
uppercase.bzl - The rule definitionBUCK - How to use the rule[Creates files and builds]
"Success! The build worked (with a warning about no outputs, which is expected).
Let me explain what just happened:
rule() functionNow, before we move on, let me check one thing..."
[Asks an open-ended Socratic comprehension question as plain text, generated from what Step 1 taught — e.g. why the build succeeded with no output — then waits for the user to answer in their own words. If the answer is right, affirm and go to Step 2; if not, re-explain and ask again.]
[And continues this pattern through all 8 steps]
uppercase.bzl with the rule definition and updated BUCK to use it."uppercase.bzl to add the src attribute."uppercase.bzl in your editor to see the full implementation."references/concepts.md - Deep dive into Buck2 core concepts (build model, targets, artifacts, actions, providers, configurations, build graph)references/advanced_patterns.md - Production-ready patterns (custom providers, transitive sets, toolchains, multiple outputs, testing rules)fbcode/buck2/prelude/Pro tip: After completing the tutorial, ask questions like "how do providers work in detail?" or "what are transitive sets?" and I'll reference the appropriate documentation to give you deeper explanations.
User is stuck: Offer to review previous concepts or try simpler examples
User wants to skip ahead: Allow it, but ensure prerequisites are met
User found a bug: Help debug and explain what went wrong
User wants to experiment: Encourage it! Help them try variations
User is confused: Go back a step, provide more examples, or explain differently
AskUserQuestion:
- "Start from scratch (will backup existing files)"
- "Continue from where I left off"
- "Review a specific step"# uppercase.bzl
def _uppercase_impl(ctx: AnalysisContext) -> list[Provider]:
"""Rule implementation function - called during analysis phase."""
return [DefaultInfo()]
uppercase = rule(
impl = _uppercase_impl,
attrs = {},
)load(":uppercase.bzl", "uppercase")
uppercase(name = "hello")buck2 build :hello
# Expected: SUCCESS with warning "target does not have any outputs"def _uppercase_impl(ctx: AnalysisContext) -> list[Provider]:
# Access the source file attribute
src = ctx.attrs.src # This is an Artifact
return [DefaultInfo()]
uppercase = rule(
impl = _uppercase_impl,
attrs = {
"src": attrs.source(), # Declares this rule accepts a source file
},
)load(":uppercase.bzl", "uppercase")
uppercase(
name = "hello",
src = "input.txt",
)hello worldbuck2 build :hello
# Expected: SUCCESS (still no outputs, but accepts input now)def _uppercase_impl(ctx: AnalysisContext) -> list[Provider]:
src = ctx.attrs.src
# Declare an output artifact
output = ctx.actions.declare_output("result.txt")
# Return it as the default output
return [DefaultInfo(default_output = output)]buck2 build :hello
# Expected: ERROR - "Artifact must be bound by now"def _uppercase_impl(ctx: AnalysisContext) -> list[Provider]:
src = ctx.attrs.src
output = ctx.actions.declare_output("result.txt")
# Create a shell script that will run
# Use Python to uppercase the file and write to output
script = """
python3 -c "import sys; print(open(sys.argv[1]).read().upper(), end='')" "$1" > "$2"
"""
# Register the action with Buck2
ctx.actions.run(
cmd_args([
"/bin/bash",
"-c",
script,
"--",
src, # Input artifact
output.as_output(), # Output artifact
]),
category = "uppercase", # Shows in buck2 output
)
return [DefaultInfo(default_output = output)]buck2 build :hello --show-full-output
# Expected: SUCCESS with output path shown
# Check the content
cat <output-path>
# Expected: HELLO WORLD# List all targets in the current package
buck2 targets :
# Expected output shows targets like:
# fbcode//buck2/buck2-tutorial:hello
# fbcode//buck2/buck2-tutorial:goodbyecell//package/path:target_name
└─┬┘ └─────┬──────┘ └────┬────┘
│ │ └─ Target name (from 'name' attribute)
│ └─────────────── Package path (directory containing BUCK file)
└──────────────────────── Cell name (repository root)# Unconfigured - shows raw attributes
buck2 uquery :hello --output-attribute=src
# Configured - shows with platform config applied
buck2 cquery :hello --output-attribute=srcdef _uppercase_impl(ctx: AnalysisContext) -> list[Provider]:
src = ctx.attrs.src
# Output filename can vary by platform
output_name = ctx.attrs.output_name
output = ctx.actions.declare_output(output_name)
script = """
python3 -c "import sys; print(open(sys.argv[1]).read().upper(), end='')" "$1" > "$2"
"""
ctx.actions.run(
cmd_args([
"/bin/bash",
"-c",
script,
"--",
src,
output.as_output(),
]),
category = "uppercase",
)
return [DefaultInfo(default_output = output)]
uppercase = rule(
impl = _uppercase_impl,
attrs = {
"src": attrs.source(),
"output_name": attrs.string(default = "result.txt"),
},
)load(":uppercase.bzl", "uppercase")
uppercase(
name = "hello",
src = "input.txt",
output_name = select({
"DEFAULT": "result.txt",
"ovr_config//os:windows": "result_windows.txt",
"ovr_config//os:linux": "result_linux.txt",
"ovr_config//os:macos": "result_macos.txt",
}),
)# See the raw select() expression
buck2 uquery :hello --output-attribute=output_name
# See the resolved value for your platform
buck2 cquery :hello --output-attribute=output_name
# Build with resolved configuration
buck2 build :hello --show-full-output
# Notice the output filename matches your OS!def _uppercase_impl(ctx: AnalysisContext) -> list[Provider]:
src = ctx.attrs.src
output_name = ctx.attrs.output_name
output = ctx.actions.declare_output(output_name)
# Collect outputs from dependencies
dep_files = []
for dep in ctx.attrs.deps:
dep_output = dep[DefaultInfo].default_outputs
dep_files.extend(dep_output)
# Build a script that concatenates dep outputs with src, then uppercases
# This demonstrates actually USING the dependency outputs!
# Script expects: output_path dep_file1 dep_file2 ... src_file
# Simple Python script to concatenate files with newlines and uppercase
script = """
python3 -c "
import sys
contents = []
for path in sys.argv[2:]: # Skip output path, read all input files
with open(path) as f:
contents.append(f.read())
with open(sys.argv[1], 'w') as out:
out.write('\\n'.join(contents).upper())
" "$@"
"""
# Build the command: bash -c script -- output dep1 dep2 ... src
cmd = cmd_args([
"/bin/bash",
"-c",
script,
"--",
output.as_output(),
])
# Add all dependency outputs as inputs
for dep_file in dep_files:
cmd.add(dep_file)
# Add our source file last
cmd.add(src)
ctx.actions.run(cmd, category = "uppercase")
return [DefaultInfo(default_output = output)]
uppercase = rule(
impl = _uppercase_impl,
attrs = {
"src": attrs.source(),
"output_name": attrs.string(default = "result.txt"),
"deps": attrs.list(attrs.dep(), default = []),
},
)# Create input2.txt first
# (You'll need to create this file)
uppercase(
name = "goodbye",
src = "input2.txt",
output_name = "result2.txt",
deps = [":hello"], # Depends on the first target
)goodbye world# Build goodbye - will also build hello automatically
buck2 build :goodbye --show-full-output
# Check the content - it should contain BOTH files uppercased with newline between!
cat <output-path-for-goodbye>
# Expected:
# HELLO WORLD
# GOODBYE WORLD
# (concatenated hello's output + goodbye's input, separated by newline, all uppercased)
# See the dependency graph
buck2 cquery "deps(:goodbye)"# This is a MACRO - just a function that wraps the rule
def uppercase_macro(name, src, prefix = "UPPER", **kwargs):
"""
A convenience macro that adds a prefix to the output filename.
Macros run during the loading phase, before analysis.
"""
uppercase(
name = name,
src = src,
output_name = prefix + "_" + src, # Add prefix
**kwargs
)uppercase_macro(
name = "hello_macro",
src = "input.txt",
prefix = "PREFIXED",
)buck2 build :hello_macro --show-full-output
# Output filename will be: PREFIXED_input.txtAskUserQuestion:
question: "Congratulations! You've completed the tutorial. What would you like to do?"
options:
- "Review a specific concept"
- "Try building something custom"
- "Clean up tutorial files"
- "See next steps for learning more"