Repository navigation
Diagnose no-cgo parse errors with tree-sitter compiled to wasm - #303
Merged
Merged
Conversation
Builds without cgo, like the Dagger engine's, have had no tree-sitter. This package gives them the real one: the C runtime (the version go-tree-sitter vendors, which cgo builds link), parser.c and scanner.c, compiled by zig into the checked-in dang.wasm (about 310 KB) and run by wazero v1.11.0, the version the engine already links. The new treesitter-wasm generator builds the module with a pinned, checksummed zig, from the parser.c ./hack/generate produces, so it is reproducible and one generate run covers a grammar change; its stale check catches a change that skipped it. The module is compiled once per process on first use. Instances are pooled, so concurrent parses are safe; one cut short by its context is thrown away, and so is one whose memory grew past 16 MB, since wasm memory never shrinks. A parse can prune error-free subtrees, which is all error reporting reads. TestParityWithCgo parses every .dang file in the repo and every tree-sitter corpus input both ways and requires identical trees, the 39 malformed inputs included. Signed-off-by: Alex Suraci <[email protected]>
Without cgo, a parse error that isn't a pitfall named only the token the parser failed at. Now tree-sitter, compiled to WebAssembly (pkg/dang/tswasm), diagnoses it the same as the cgo build does: the message builder reads the tree through a small node interface that both runtimes implement, and since both run the same C code, the tests/ syntax goldens now pass with CGO_ENABLED=0 too. Pitfalls still come first. A recovery parse is bounded by a timeout, past which the token message stands in. It is there to stop a runaway parse, not a slow one, so it grows with the source: 20µs a byte, five times the parser's measured pace, with a one-second floor. GOOS=js (the playground) keeps the token message, and so does -tags dang_nowasm, for a no-cgo build that would rather not link wazero (about 2.7 MB). The new test/nocgo check runs pkg/dang, pkg/lsp and the tests without cgo, so the engine's build is covered by CI. Signed-off-by: Alex Suraci <[email protected]>
Deploying dang with
|
| Latest commit: |
3edd353
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://f2091ec7.dang-3kk.pages.dev |
| Branch Preview URL: | https://nocgo-wasm-treesitter.dang-3kk.pages.dev |
vito
added a commit
to dagger/dagger
that referenced
this pull request
Nov 13, 2026
Pulls in pitfall diagnostics (vito/dang#302) and tree-sitter compiled to WebAssembly and run by wazero (vito/dang#303, #307): Dang no longer needs cgo anywhere, and its highlighter (dang.Highlight) is usable from any binary. Signed-off-by: Alex Suraci <[email protected]>
vito
added a commit
to dagger/dagger
that referenced
this pull request
Nov 13, 2026
Pulls in pitfall diagnostics (vito/dang#302), tree-sitter compiled to WebAssembly and run by wazero (vito/dang#303, #307): Dang no longer needs cgo anywhere, and its highlighter (dang.Highlight) is usable from any binary. Also checker diagnostics fixes (vito/dang#309): GraphQL object list hints suggest `value.{{id}}`, and a failed declaration no longer cascades "not found" errors through its uses. Signed-off-by: Alex Suraci <[email protected]>
vito
added a commit
to dagger/dagger
that referenced
this pull request
Nov 13, 2026
Pulls in pitfall diagnostics (vito/dang#302), tree-sitter compiled to WebAssembly and run by wazero (vito/dang#303, #307): Dang no longer needs cgo anywhere, and its highlighter (dang.Highlight) is usable from any binary. Also checker diagnostics fixes (vito/dang#309): GraphQL object list hints suggest `value.{{id}}`, and a failed declaration no longer cascades "not found" errors through its uses. Signed-off-by: Alex Suraci <[email protected]>
vito
added a commit
to dagger/dagger
that referenced
this pull request
Nov 13, 2026
Pulls in pitfall diagnostics (vito/dang#302), tree-sitter compiled to WebAssembly and run by wazero (vito/dang#303, #307): Dang no longer needs cgo anywhere, and its highlighter (dang.Highlight) is usable from any binary. Also checker diagnostics fixes (vito/dang#309): GraphQL object list hints suggest `value.{{id}}`, and a failed declaration no longer cascades "not found" errors through its uses. Signed-off-by: Alex Suraci <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Without cgo, Dang had no tree-sitter. That is how the Dagger engine builds it, so a syntax error that isn't a pitfall (#302) named only the token the parser stopped at. This PR runs the real C tree-sitter in those builds by compiling it to WebAssembly. Their parse errors now read exactly like the cgo build's: the
tests/syntax goldens pass withCGO_ENABLED=0.How it works
pkg/dang/tswasm: zig compiles the C runtime,treesitter/src/parser.c,scanner.cand a small export shim (csrc/shim.c) towasm32-wasi. The result,dang.wasm(322 KB, 75 KB gzipped), is checked in, embedded withgo:embed, and run by wazero v1.11.0, the version the engine already links. The C runtime is the version go-tree-sitter vendors, so it's the same code cgo builds link.Parsereturns the tree as Go structs. Error reporting only reads subtrees that contain errors, so it can prune the rest.pkg/dang/parse_errors*.go:The message builder, including Interpolate in double-quoted strings (Dang 2.5) #300's "error nearest the PEG failure" choice, now reads the tree through a small
syntaxNodeinterface. The cgo runtime and the wasm one both implement it.Build selection:
!cgoGOOS=js(playground)-tags dang_nowasmPitfalls still come first.
A wasm recovery parse has a timeout, after which the token message is used instead. The timeout grows with the source (20 µs/byte, five times the measured pace, with a one-second floor), so it stops runaway parses, not merely slow ones.
treesitter-wasmDagger generator builds the module with a pinned, checksummed zig 0.15.2 (x86_64 or aarch64). Nobody needs zig installed.parser.cthat./hack/generateproduces, so a single generate run covers a grammar change.treesitter-wasm/stalecheck catches a grammar change that skipped it.Tests
TestParityWithCgo(pkg/dang/tswasm, cgo) parses every.dangfile in the repo and every tree-sitter corpus input (923 inputs) with both runtimes. It requires identical trees, node for node, including the 39 inputs with syntax errors.test/nocgocheck runsgo test ./pkg/dang/... ./testsandgo test -short ./pkg/lspwithCGO_ENABLED=0.-shortskips the Neovim tests. They drive a separately builtdangbinary (cgo), not the code under test.go test ./pkg/... ./tests/... ./cmd/dang/...passes with cgo on and off. TheGOOS=jsplayground build and-tags dang_nowasmlink no wazero.Size and latency
Stripped (
-s -w), go1.26, linux/amd64, against #302's head (385a331).dang, cgodang,CGO_ENABLED=0dang,CGO_ENABLED=0 -tags dang_nowasmpkg/dang+analysis,CGO_ENABLED=0Most of the +2.7 MB is wazero itself; the engine already pays for it.
ParseWithRecoveryon a 25-byte broken fileThe ranges span two runs on a shared, noisy runner. On large files the wasm parser runs at about 4 µs/byte, roughly 9× slower than cgo: a 96 KB file takes about 0.4 s. That only costs time on the error path.
Follow-ups (not in this PR)
CompletionContextAt) and REPL highlighting on the wasm path. They still need cgo; the wasm module would need exports for queries and cursor classification.GOOS=js) keeps the token message. wazero doesn't run there; the docs site's web-tree-sitter could.-Olevel made no difference, and error recovery is the expensive part (a 15-byte broken input takes about 1 ms against 30 µs for a valid one). Worth profiling if large broken files turn out to matter.