skyquery
skyquery
Section titled “skyquery”A query tool for analyzing Starlark source code. Find function definitions, load statements, and function calls across your codebase using a simple query language.
Installation
Section titled “Installation”go install github.com/albertocavalcante/sky/cmd/skyquery@latest# List all function definitionsskyquery 'defs(//...)'
# List all load statements in a directoryskyquery 'loads(//internal/...)'
# Find functions matching a patternskyquery 'filter("^test_", defs(//...))'
# Output as JSONskyquery --output=json 'defs(//...)'
# Show file locationsskyquery --output=location 'defs(//...)'
# Count results onlyskyquery --output=count 'files(//...)'| Flag | Description |
|---|---|
--output | Output format: name, location, json, count (default: name) |
--workspace | Workspace root directory (default: .) |
--keep_going | Continue on parse errors |
--version | Print version and exit |
Query Language
Section titled “Query Language”skyquery uses a simple functional query language to find elements in Starlark source files.
Target Patterns
Section titled “Target Patterns”Target patterns specify which files to analyze:
| Pattern | Description |
|---|---|
//... | All files in workspace recursively |
//path/... | All files under path/ recursively |
//path:file.star | Specific file |
Query Functions
Section titled “Query Functions”defs(pattern)
Section titled “defs(pattern)”Find function definitions:
# All function definitionsskyquery 'defs(//...)'
# Functions in a specific directoryskyquery 'defs(//lib/...)'loads(pattern)
Section titled “loads(pattern)”Find load statements:
# All load statementsskyquery 'loads(//...)'
# Loads in BUILD filesskyquery 'loads(//...)'calls(function, pattern)
Section titled “calls(function, pattern)”Find function calls:
# Find all calls to 'load'skyquery 'calls(load, //...)'
# Find all calls to 'rule'skyquery 'calls(rule, //...)'files(pattern)
Section titled “files(pattern)”List matching files:
# Count all Starlark filesskyquery --output=count 'files(//...)'
# List all files in a directoryskyquery 'files(//internal/...)'filter(regex, query)
Section titled “filter(regex, query)”Filter query results by name:
# Find test functionsskyquery 'filter("^test_", defs(//...))'
# Find private functionsskyquery 'filter("^_", defs(//...))'
# Find functions ending with _implskyquery 'filter("_impl$", defs(//...))'Output Formats
Section titled “Output Formats”name (Default)
Section titled “name (Default)”Outputs just the names, one per line:
$ skyquery 'defs(//lib/...)'create_targetvalidate_inputshelper_functionlocation
Section titled “location”Outputs file:line: name format:
$ skyquery --output=location 'defs(//lib/...)'lib/targets.star:10: create_targetlib/targets.star:25: validate_inputslib/utils.star:5: helper_functionFull JSON output with all details:
$ skyquery --output=json 'defs(//lib/...)'{ "query": "defs(//lib/...)", "items": [ { "type": "def", "name": "create_target", "file": "lib/targets.star", "line": 10, "params": ["name", "srcs", "deps"], "docstring": "Create a build target." }, { "type": "def", "name": "validate_inputs", "file": "lib/targets.star", "line": 25, "params": ["inputs"], "docstring": "" } ]}Outputs only the count of results:
$ skyquery --output=count 'defs(//...)'42
$ skyquery --output=count 'files(//...)'15Examples
Section titled “Examples”Find All Public Functions
Section titled “Find All Public Functions”# Functions not starting with underscoreskyquery 'filter("^[^_]", defs(//...))'Analyze Dependencies
Section titled “Analyze Dependencies”# Find all external loadsskyquery --output=json 'loads(//...)' | jq '.items[] | select(.module | startswith("@"))'Count Functions per Directory
Section titled “Count Functions per Directory”# Count functions in each major directoryfor dir in lib internal rules; do count=$(skyquery --output=count "defs(//${dir}/...)") echo "${dir}: ${count} functions"doneFind Undocumented Functions
Section titled “Find Undocumented Functions”# Get JSON and filter for empty docstringsskyquery --output=json 'defs(//...)' | \ jq '.items[] | select(.docstring == "") | .name'Search for Specific Patterns
Section titled “Search for Specific Patterns”# Find all rule implementationsskyquery 'filter("_impl$", defs(//...))'
# Find all test functionsskyquery 'filter("^test_", defs(//...))'
# Find all setup functionsskyquery 'filter("^setup", defs(//...))'Generate Function Index
Section titled “Generate Function Index”# Create a markdown index of all functionsecho "# Function Index"echo ""skyquery --output=location 'defs(//...)' | while read line; do file=$(echo "$line" | cut -d: -f1) linenum=$(echo "$line" | cut -d: -f2) name=$(echo "$line" | cut -d: -f3 | tr -d ' ') echo "- [\`${name}\`](${file}#L${linenum})"doneCI Integration
Section titled “CI Integration”Check for New Functions
Section titled “Check for New Functions”# In CI, compare against baselineskyquery --output=count 'defs(//...)' > current_count.txtdiff baseline_count.txt current_count.txt || echo "Function count changed"Enforce Naming Conventions
Section titled “Enforce Naming Conventions”# Fail if any public function doesn't follow naming conventionbad_names=$(skyquery 'filter("^[A-Z]", defs(//...))' | wc -l)if [ "$bad_names" -gt 0 ]; then echo "Error: Found $bad_names functions starting with uppercase" skyquery 'filter("^[A-Z]", defs(//...))' exit 1fiGenerate Documentation Index
Section titled “Generate Documentation Index”- name: Generate function index run: | skyquery --output=json 'defs(//...)' > docs/function-index.jsonWorking with Large Codebases
Section titled “Working with Large Codebases”Use Specific Patterns
Section titled “Use Specific Patterns”# Instead of scanning everything:skyquery 'defs(//...)'
# Scan specific directories:skyquery 'defs(//lib/...)'skyquery 'defs(//rules/...)'Continue on Errors
Section titled “Continue on Errors”# Don't fail on parse errors in some filesskyquery --keep_going 'defs(//...)'Specify Workspace Root
Section titled “Specify Workspace Root”# Run from a different directoryskyquery --workspace=/path/to/project 'defs(//...)'Exit Codes
Section titled “Exit Codes”| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Error (invalid query, no files found, etc.) |
Query Language Grammar
Section titled “Query Language Grammar”query = function "(" args ")"function = "defs" | "loads" | "calls" | "files" | "filter"args = arg ("," arg)*arg = pattern | string | querypattern = "//" path "..."string = "\"" chars "\""Comparison with Bazel Query
Section titled “Comparison with Bazel Query”| Feature | skyquery | bazel query |
|---|---|---|
| Scope | Starlark source files | Build graph |
| Speed | Fast (no build) | Requires loading |
| Function defs | Yes | No |
| Load statements | Yes | Limited |
| Dependencies | No | Yes |
| Targets | No | Yes |
skyquery is designed for source code analysis, not build graph queries. Use Bazel’s query for dependency analysis and skyquery for source-level analysis.