| name | buck2-build-troubleshoot |
| description | Debugs Buck2 build failures systematically by analyzing error logs, checking common issues (cache, visibility, cycles), and suggesting fixes. Use when builds fail, tests won't run, or Buck2 reports errors. |
Buck2 Build Troubleshoot
Overview
This skill provides systematic debugging for Buck2 build failures. It analyzes error logs, identifies common issues, and suggests concrete fixes. Instead of manually parsing verbose build output, use the build doctor script to diagnose problems quickly.
Use this skill when:
- Buck2 build fails with cryptic errors
- Targets won't compile or link
- Tests fail to run
- Visibility errors prevent building
- Dependency cycles are suspected
- Cache issues cause inconsistent builds
- Remote execution fails
This skill provides:
scripts/build_doctor.py - Automated build diagnostics
references/common_errors.md - Error patterns and solutions
- Systematic debugging workflow
- Quick fixes for common issues
Quick Start
When a build fails:
python3 scripts/build_doctor.py
python3 scripts/build_doctor.py //src/tools:mytool
python3 scripts/build_doctor.py --verbose //src/tools:mytool
The script will:
- Check what failed recently
- Analyze error messages
- Check common issues (cache, visibility, cycles)
- Suggest specific fixes
Common Build Failures
Compilation Errors
Rust compilation failure:
Error: rustc failed with exit code 1
Diagnosis:
python3 scripts/build_doctor.py --show-logs //target
buck2 query "//target" --output-attribute srcs
buck2 query "deps('//target', 1)"
Common fixes:
- Fix syntax errors in source code
- Add missing dependencies to BUILD file
- Check Rust edition compatibility
- Verify feature flags
Dependency Errors
Missing dependency:
Error: unresolved import `foo::bar`
Fix:
deps = [
"//path/to:foo",
"third-party//crate:crate",
]
Circular dependency:
Error: cycle detected in dependency graph
Diagnosis:
python3 scripts/build_doctor.py --check-cycles //src/...
buck2 query "allpaths('//target/a', '//target/b')"
buck2 query "allpaths('//target/b', '//target/a')"
Fix: Break the cycle by:
- Extracting shared code to new library
- Removing unnecessary dependency
- Using dependency injection instead of direct import
Visibility Errors
Cannot access target:
Error: //src/app:app cannot depend on //src/lib:internal (not visible)
Diagnosis:
buck2 query "//src/lib:internal" --output-attribute visibility
python3 scripts/build_doctor.py --check-visibility //src/lib:internal
Fix:
depot.rust_library(
name = "internal",
visibility = [
"//src/app/...",
"PUBLIC",
],
)
Linking Errors
Undefined reference:
Error: undefined reference to `symbol`
Common causes:
- Missing library dependency
- Wrong link order
- ABI mismatch between libraries
Fix:
buck2 query "deps('//target', 1)" --output-attribute deps
Test Failures
Tests won't run:
Error: No tests found
Diagnosis:
buck2 targets //path:test
buck2 query "//path:test" --output-attribute buck.type
Fix:
depot.rust_test(
name = "test",
srcs = glob(["src/**/*.rs"]),
)
Systematic Debugging Workflow
Step 1: Identify What Failed
buck2 log what-failed
python3 scripts/build_doctor.py
Step 2: Read the Error
buck2 log last
buck2 build //target -v 2
Step 3: Check Common Issues
python3 scripts/build_doctor.py --all-checks //target
This checks:
- Cache corruption
- Visibility issues
- Circular dependencies
- Missing dependencies
- Common configuration errors
Step 4: Verify Target Configuration
buck2 query "//target" --json | jq
buck2 query "deps('//target', 1)"
buck2 query "//target" --output-attribute srcs
Step 5: Test in Isolation
buck2 build //target
buck2 clean && buck2 build //target
buck2 build //target --no-remote-cache
Build Doctor Script Usage
Basic Diagnostics
python3 scripts/build_doctor.py
python3 scripts/build_doctor.py //src/tools:mytool
python3 scripts/build_doctor.py //src/tools:tool1 //src/lib:lib2
Check Options
python3 scripts/build_doctor.py --check-cache
python3 scripts/build_doctor.py --check-visibility //target
python3 scripts/build_doctor.py --check-cycles //src/...
python3 scripts/build_doctor.py --all-checks //target
Output Options
python3 scripts/build_doctor.py --verbose //target
python3 scripts/build_doctor.py --show-logs //target
python3 scripts/build_doctor.py --json //target
Common Error Patterns
Pattern: "No such target"
Error:
Error: No targets found matching //src/tools:missing
Causes:
- Typo in target name
- Target not defined in BUILD
- Wrong package path
Fix:
buck2 targets //src/tools:
ls -la src/tools/BUILD
buck2 targets //... | grep missing
Pattern: "Glob matched no files"
Error:
Warning: glob(["src/**/*.rs"]) matched no files
Causes:
- Wrong glob pattern
- Files in wrong location
- Missing source directory
Fix:
ls -la src/
Pattern: "Cannot find module"
Error (Rust):
Error: cannot find module `foo` in crate root
Fix:
- Add module declaration:
mod foo; in lib.rs/main.rs
- Or add file to same directory
- Or add as dependency in BUILD
Pattern: "Feature not enabled"
Error:
Error: feature `foo` is required
Fix:
deps = [
"third-party//crate:crate[foo]",
]
Pattern: "Output already used"
Error:
Error: output directory already in use
Causes:
- Multiple builds running
- Stale lock files
- Buck daemon issues
Fix:
buck2 kill
buck2 clean
buck2 build //target
Advanced Troubleshooting
Cache Issues
Symptoms:
- Inconsistent builds
- Changes not reflected
- Stale outputs
Diagnosis:
buck2 build --no-remote-cache //target
buck2 audit config cache
Fix:
buck2 clean
cat .buckconfig | grep -A5 cache
Remote Execution Issues
Symptoms:
- Builds fail in CI but work locally
- Platform-specific errors
- Missing dependencies in remote environment
Diagnosis:
buck2 build --no-remote-execution //target
buck2 audit config re
Fix:
- Verify all dependencies are in BUILD
- Check platform compatibility
- Ensure no local-only paths
Incremental Build Issues
Symptoms:
- Buck rebuilds everything
- No caching benefits
- Slow builds
Diagnosis:
buck2 explain //target
buck2 build //target
buck2 build //target
Fix:
- Ensure reproducible builds
- Avoid generated source in working copy
- Check for timestamp dependencies
Integration with Other Tools
With jj Version Control
jj diff --stat | cut -f2 | while read file; do
buck2 query "owner('$file')"
done | while read target; do
python3 scripts/build_doctor.py $target
done
With CI/CD
if ! buck2 build @targets; then
python3 scripts/build_doctor.py --json > diagnosis.json
cat diagnosis.json
exit 1
fi
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/...
buck2 build "@$TARGETS_FILE" || {
python3 scripts/build_doctor.py --show-logs
}
Quick Reference
Must-Know Commands
buck2 log what-failed
buck2 log last
buck2 build //target -v 2
buck2 clean
buck2 kill
buck2 explain //target
Common Quick Fixes
buck2 clean && buck2 build //target
buck2 kill && buck2 build //target
buck2 build --no-remote-cache //target
buck2 build //target -v 2 2>&1 | tee build.log
buck2 test //... --keep-going
Troubleshooting Checklist
When builds fail, check:
Tips and Best Practices
- Read the full error - Often the actual error is near the top, not bottom
- Build with -v 2 - Verbose mode shows what's actually running
- Test incrementally - Fix one error at a time
- Use clean builds - When in doubt,
buck2 clean
- Check recent changes - Use
jj log to see what changed
- Isolate the problem - Build specific targets, not everything
- Use build doctor - Automated checks save time
- Keep BUILD files simple - Complex BUILD files are hard to debug
Getting Help
If stuck after trying these steps:
- Run build doctor with verbose output
- Collect full error logs:
buck2 build //target -v 2 2>&1 > error.log
- Check target configuration:
buck2 query //target --json
- Search for similar errors in documentation
- Ask for help with specific error message and context