Ray's Knowledge Base
Unlock

A tree-sitter query predicate must share a group with its pattern or it filters nothing

PitfallVerified 14 Oct 2026Holds tree-sitter: 0.25:0.27
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A tree-sitter query with a top-level predicate line did not filter: every candidate matched as if the #eq?/#match? were absent. In rvow this named a check(...) call from the shipped JavaScript query that only marks test/it/describe.

Cause#

In tree-sitter 0.25-0.27 the C query parser ends a pattern at each top-level s-expression. A following (#eq? ...) becomes a second, node-less pattern: it matches every position with zero captures, so its text predicate passes vacuously, and the real pattern keeps matching unfiltered.

Fix#

Wrap the pattern and its predicates in one outer group: ((pattern) @test (#eq? @fn "x")). In queries.rs the captures come from QueryMatch::captures() (the field is private), iteration uses StreamingIterator::next, and the binding auto-evaluates #eq?/#match? text predicates inside satisfies_text_predicates.

Evidence#

tree-sitter 0.27.0, grammar from tree-sitter-javascript.wasm: Query("(identifier) @fn (#eq? @fn "test")") gives pattern_count=2, start_byte_for_pattern(1)=17 (the predicate's offset), and QueryCursor::matches returns the unfiltered fn=check/expect/add captures. Grouped form "((identifier) @fn (#eq? @fn "test"))" gives pattern_count=1 and filters correctly (0 matches for check/expect/add).