Skip to main content
The fuzz command tests a parser by performing random edits and ensuring that undoing these edits results in consistent parse trees.
Aliases: f

How It Works

The fuzzer:
  1. Parses test files from your corpus
  2. Applies random edits to the source code
  3. Re-parses the edited code
  4. Undoes the edits
  5. Verifies the parse tree matches the original
The test fails if:
  • Parse trees are not equal after undoing edits
  • Changed ranges are inconsistent
  • The parser crashes or hangs

Options

Test Selection

names
A list of test names to skip fuzzing. Can be specified multiple times.
regex
Only fuzz corpus test cases whose names match this regex.
regex
Skip corpus test cases whose names match this regex.

Parser Options

path
The path to the directory containing the grammar. Implies --rebuild.
path
The path to the parser’s dynamic library. Used instead of the cached or automatically generated library.
string
If --lib-path is used, the name of the language used to extract the library’s language function.
path
The directory containing the parser. Primarily useful in multi-language repositories.

Fuzzing Parameters

number
default:"3"
The maximum number of edits to perform per fuzz test.Can also be set via the TREE_SITTER_EDITS environment variable.
number
default:"10"
The number of iterations to run per test.Can also be set via the TREE_SITTER_ITERATIONS environment variable.

Debug Options

flag
Output parsing and lexing logs to stderr.
flag
Output logs of the graphs of the stack and parse trees during parsing. Graphs are constructed with graphviz dot, and output is written to log.html.

Build Options

flag
Force a rebuild of the parser before running the fuzzer.

Examples

Basic Fuzzing

Fuzz with More Iterations

Fuzz Specific Tests

Skip Problematic Tests

Fuzz with Debug Logging

Fuzz with Environment Variables

Best Practices

Start Small

Begin with few iterations and edits, then increase:

Focus on Problem Areas

Use --include to focus on specific language constructs:

Regular Fuzzing

Incorporate fuzzing into your development workflow:

CI Integration

Add fuzzing to your CI pipeline:

Interpreting Results

Success

If all tests pass, you’ll see:

Failure

If a test fails, you’ll see:
  • The test name that failed
  • The edit sequence that caused the failure
  • Details about the inconsistency
Use this information to:
  1. Reproduce the issue manually
  2. Add a regression test
  3. Fix the grammar or external scanner

Troubleshooting

Fuzzing Takes Too Long

Reduce iterations or edits:

Tests Hang

Some inputs may cause infinite loops. Use --skip to bypass:

Inconsistent Failures

Fuzzing is random, so failures may not reproduce. To help:
  1. Note the seed value if displayed
  2. Run multiple times to confirm
  3. Add logging to understand the issue