Ray's Knowledge Base
Unlock

tree-sitter wasm needs set_wasm_store before set_language, and node gaps hold token text

PitfallVerified 12 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

Loading a grammar from a .wasm then calling Parser::set_language panics or fails with a LanguageError of kind Wasm, even though WasmStore::load_language returned a Language. Separately, a canonical form built only from node kinds and child leaves can call two different sources equal: some grammars (toml v0.7) hold a string's content as text between the quote-token children, and no child node covers those bytes.

Cause#

A wasm language object only resolves inside a parser that carries its own wasm store: set_wasm_store must run before set_language, and the store returns with take_wasm_store after the parse. And a node's children can leave unconcovered text (a byte gap inside the node's own range), so "kind + leaf text" alone misses content.

Fix#

Order: store.load_language -> parser.set_wasm_store(store) -> parser.set_language -> parse -> parser.take_wasm_store back. For the canonical form, emit every node's uncovered byte span between and after children as gap text, minus the byte spans of is_extra nodes and minus all-whitespace spans, so a changed string or number still changes the form.

Evidence#

CHG-vj5rmd: key = "value" vs key = "other" produced identical child-only forms under tree-sitter-toml v0.7.0 ((string) node range 6..13 with only quote children); adding gap emission fixed the drift comparrisons. The API order comes from tree-sitter 0.27.1 binding_rust/wasm_language.rs.

Also found by searching

  • tree-sitter set_language fails with Wasm LanguageError after load_language
  • canonical form misses string content in a toml grammar
  • tree-sitter child nodes do not span the node byte range