| name | buck2-query-helper |
| description | Runs common Buck2 queries for dependency analysis, target inspection, and build graph exploration. Use when investigating dependencies, finding reverse dependencies, filtering by rule type, or understanding target relationships in the monorepo. |
Buck2 Query Helper
Overview
This skill provides enhanced Buck2 query capabilities with better formatting, common query shortcuts, and pattern matching. It helps you understand the build graph, analyze dependencies, and investigate target relationships without memorizing complex query syntax.
Use this skill when:
- Finding what depends on a target (reverse dependencies)
- Listing all dependencies of a target
- Filtering targets by rule type (all rust_binary, all deno targets, etc.)
- Inspecting target attributes (srcs, deps, visibility)
- Understanding why a target is being built
- Finding cycles in the dependency graph
- Exploring the build graph structure
This skill provides:
scripts/query_helper.py - Enhanced query runner with shortcuts
references/query_patterns.md - Common query examples and patterns
- Better formatted output for large query results
- Shortcuts for frequently used queries
Quick Start
Common queries made simple:
python3 scripts/query_helper.py rdeps //src/lib/mylib
python3 scripts/query_helper.py deps //src/tools/mytool
python3 scripts/query_helper.py kind rust_binary //src/...
python3 scripts/query_helper.py attrs //src/lib/mylib
Common Query Patterns
Finding Dependencies
Direct dependencies:
python3 scripts/query_helper.py deps root//buck/tools/tdutil:tdutil
python3 scripts/query_helper.py deps --show-kind root//buck/tools/tdutil:tdutil
Transitive dependencies:
python3 scripts/query_helper.py deps --transitive root//buck/tools/tdutil:tdutil
python3 scripts/query_helper.py deps --transitive --depth 2 root//buck/tools/tdutil:tdutil
Finding Reverse Dependencies
What depends on this target:
python3 scripts/query_helper.py rdeps //src/lib/common
python3 scripts/query_helper.py rdeps --scope //src/... //src/lib/common
python3 scripts/query_helper.py rdeps --explain //src/lib/common
Filtering by Type
Find targets by rule type:
python3 scripts/query_helper.py kind rust_binary //src/...
python3 scripts/query_helper.py kind rust_library //src/...
python3 scripts/query_helper.py kind deno //src/tools/...
python3 scripts/query_helper.py kind "rust_binary|rust_test" //src/...
Inspecting Attributes
View target configuration:
python3 scripts/query_helper.py attrs //src/lib/mylib
python3 scripts/query_helper.py attrs --fields srcs,deps //src/lib/mylib
python3 scripts/query_helper.py attrs --json //src/lib/mylib
Understanding Buck2 Query Language
Basic Queries
Buck2 queries work on sets of targets:
buck2 query //src/lib:mylib
buck2 query //src/lib:
buck2 query //src/...
buck2 query "//src/lib/...:test"
Query Functions
deps() - Find dependencies:
buck2 query "deps('root//buck/tools/tdutil:tdutil')"
buck2 query "deps('root//buck/tools/tdutil:tdutil', 1)"
buck2 query "deps('root//buck/tools/tdutil:tdutil') ^ third-party//..."
rdeps() - Find reverse dependencies:
buck2 query "rdeps('//src/...', '//src/lib:common')"
buck2 query "rdeps('//src/...', '//src/lib:common', 1)"
kind() - Filter by rule type:
buck2 query "kind('rust_binary', '//src/...')"
buck2 query "kind('rust_.*', '//src/...')"
attrfilter() - Filter by attribute:
buck2 query "attrfilter(visibility, PUBLIC, //src/...)"
buck2 query "attrfilter(srcs, main.rs, //src/...)"
Set Operations
Combine queries with set operators:
buck2 query "//src/lib/... + //src/tools/..."
buck2 query "deps('//src/tools:mytool') ^ //third-party/..."
buck2 query "//src/... - //src/tests/..."
Script Usage Guide
query_helper.py Commands
deps - Show dependencies:
python3 scripts/query_helper.py deps TARGET
--transitive Include all transitive dependencies
--depth N Limit to N levels deep
--show-kind Show rule type for each target
--exclude PATTERN Exclude targets matching pattern
rdeps - Show reverse dependencies:
python3 scripts/query_helper.py rdeps TARGET
--scope PATTERN Search within this scope (default: //...)
--depth N Limit to N levels
--explain Show dependency chain
--show-kind Show rule type
kind - Filter by type:
python3 scripts/query_helper.py kind TYPE_PATTERN SCOPE
python3 scripts/query_helper.py kind rust_binary //src/...
python3 scripts/query_helper.py kind "rust_.*" //src/...
python3 scripts/query_helper.py kind deno //src/tools/...
attrs - Inspect attributes:
python3 scripts/query_helper.py attrs TARGET
--fields F1,F2 Show only specific fields
--json Output as JSON
--raw Show raw Buck2 output
path - Find dependency path:
python3 scripts/query_helper.py path TARGET1 TARGET2
cycles - Detect dependency cycles:
python3 scripts/query_helper.py cycles //src/...
Common Use Cases
Investigating Build Failures
Why is this target being built?
python3 scripts/query_helper.py rdeps --explain --scope //src/... //target
What are all the dependencies?
python3 scripts/query_helper.py deps --transitive --show-kind //target
Refactoring Analysis
What will break if I change this?
python3 scripts/query_helper.py rdeps //src/lib/old-api
python3 scripts/query_helper.py rdeps --scope //src/apps/... //src/lib/old-api
Can I make this internal?
python3 scripts/query_helper.py rdeps //src/lib/maybe-private
Dependency Auditing
Find all third-party dependencies:
python3 scripts/query_helper.py deps --transitive //target | grep third-party
buck2 query "deps('//src/...') ^ //third-party/..."
Find unused dependencies:
python3 scripts/query_helper.py attrs --fields deps //target
Performance Investigation
Find large dependency trees:
python3 scripts/query_helper.py deps --transitive //target | wc -l
for target in $(buck2 targets //src/tools:); do
count=$(python3 scripts/query_helper.py deps --transitive $target | wc -l)
echo "$count $target"
done | sort -rn
Advanced Patterns
Finding Bottlenecks
Targets with many reverse dependencies might be bottlenecks:
for target in $(buck2 targets //src/lib:); do
count=$(python3 scripts/query_helper.py rdeps --scope //src/... $target | wc -l)
echo "$count $target"
done | sort -rn | head -10
Analyzing Test Coverage
python3 scripts/query_helper.py kind ".*_test" //src/...
Understanding Visibility
python3 scripts/query_helper.py attrs --fields visibility //target
buck2 query "attrfilter(visibility, PUBLIC, //src/...)"
Integration with Other Tools
With Target Determination
TARGETS_FILE="$(mktemp "${TMPDIR:-/tmp}/tdutil-targets.XXXXXX")"
trap 'rm -f -- "$TARGETS_FILE"' EXIT
buck2 run root//buck/tools/tdutil:tdutil -- \
--output "$TARGETS_FILE" --universe depot//src/...
while IFS= read -r target; do
python3 scripts/query_helper.py rdeps --scope //src/... "$target" | \
grep _test
done < "$TARGETS_FILE" | sort -u
With jj Version Control
jj diff --stat | cut -f2 | while read file; do
buck2 query "owner('$file')"
done | while read target; do
python3 scripts/query_helper.py rdeps $target | grep test
done | sort -u
Reference Quick Guide
Most Common Queries
buck2 query "deps('//target', 1)"
buck2 query "deps('//target')"
buck2 query "rdeps('//...', '//target')"
buck2 query "kind('rust_binary', '//src/...')"
buck2 query "allpaths('//from', '//to')"
buck2 query "owner('src/lib/foo.rs')"
Output Formats
buck2 query "deps('//target')"
buck2 query "deps('//target')" --json
buck2 query "deps('//target')" --output-attribute srcs,deps
Troubleshooting
Query Takes Too Long
Problem: Query on //... is very slow
Solutions:
- Narrow the scope:
//src/... instead of //...
- Use depth limits:
deps('//target', 2) instead of deps('//target')
- Exclude third-party:
deps('//target') - //third-party/...
No Results Found
Problem: Query returns empty set
Verify:
buck2 targets //path:target
buck2 targets //scope/...
buck2 query "kind('type', '//scope/...')"
Unexpected Results
Problem: Query includes unexpected targets
Debug:
buck2 query "deps('//target')" --output-attribute deps
buck2 query "deps('//target')" --dot | dot -Tpng > deps.png
Tips and Best Practices
- Start narrow, expand if needed - Begin with specific scopes and widen as necessary
- Use depth limits - Especially for rdeps() to avoid massive result sets
- Combine with grep - Filter query results with standard Unix tools
- Save common queries - Create shell aliases for frequently used patterns
- Use JSON output for scripting - More reliable than parsing text output
- Visualize complex graphs - Use
--dot output with Graphviz for visualization
Shell Aliases
Add to your shell rc file:
alias bq='buck2 query'
alias bqdeps='python3 /path/to/scripts/query_helper.py deps'
alias bqrdeps='python3 /path/to/scripts/query_helper.py rdeps'
alias bqkind='python3 /path/to/scripts/query_helper.py kind'