Skip to content

Diagnose no-cgo parse errors with tree-sitter compiled to wasm - #303

Merged
vito merged 2 commits into
pitfall-diagnosticsfrom
nocgo-wasm-treesitter
Nov 12, 2026
Merged

vito merged 2 commits into
pitfall-diagnosticsfrom
nocgo-wasm-treesitter

Conversation

@vito

@vito vito commented Nov 12, 2026

Copy link
Copy Markdown
Owner

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 with CGO_ENABLED=0.

let x = (1 + 2
Error: syntax error: missing closing ')'      # without cgo, was: unexpected end of file
  --> main.dang:1:15

How it works

  • pkg/dang/tswasm: zig compiles the C runtime, treesitter/src/parser.c, scanner.c and a small export shim (csrc/shim.c) to wasm32-wasi. The result, dang.wasm (322 KB, 75 KB gzipped), is checked in, embedded with go: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.
    • Parse returns the tree as Go structs. Error reporting only reads subtrees that contain errors, so it can prune the rest.
    • The module is compiled once per process, lazily, on the first parse error.
    • Instances are pooled, so concurrent parses are safe. An instance whose parse was stopped by its context is closed instead of reused. So is one whose memory grew past 16 MB, because wasm memory never shrinks.
  • pkg/dang/parse_errors*.go:
  • Generation: the new treesitter-wasm Dagger generator builds the module with a pinned, checksummed zig 0.15.2 (x86_64 or aarch64). Nobody needs zig installed.
    • It compiles the parser.c that ./hack/generate produces, so a single generate run covers a grammar change.
    • Its treesitter-wasm/stale check catches a grammar change that skipped it.
    • The build is reproducible: regenerating gives the same bytes.

Tests

  • TestParityWithCgo (pkg/dang/tswasm, cgo) parses every .dang file 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.
  • Unit tests cover pruning, empty input, cancellation and deadlines mid-parse (the instance is discarded and the pool stays healthy), 64 concurrent parses, and the pool's memory cap.
  • New test/nocgo check runs go test ./pkg/dang/... ./tests and go test -short ./pkg/lsp with CGO_ENABLED=0.
    • -short skips the Neovim tests. They drive a separately built dang binary (cgo), not the code under test.
    • They also fail in this container whether cgo is on or off: Neovim sends an empty root URI.
  • Full go test ./pkg/... ./tests/... ./cmd/dang/... passes with cgo on and off. The GOOS=js playground build and -tags dang_nowasm link no wazero.

Size and latency

Stripped (-s -w), go1.26, linux/amd64, against #302's head (385a331).

Build Before After Δ
dang, cgo 15,930,952 15,938,952 +8 KB
dang, CGO_ENABLED=0 15,290,530 18,030,754 +2.74 MB
dang, CGO_ENABLED=0 -tags dang_nowasm 15,290,530 15,290,530 0
importer of pkg/dang + analysis, CGO_ENABLED=0 11,530,402 14,323,874 +2.79 MB
same, already linking wazero (like the engine) 13,967,522 14,323,874 +356 KB

Most of the +2.7 MB is wazero itself; the engine already pays for it.

ParseWithRecovery on a 25-byte broken file cgo wasm
First parse error in a process (compile + instantiate + parse) 0.3–0.6 ms 34–88 ms
Steady state 0.17–0.39 ms 1.2–1.9 ms

The 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)

  • Completion (CompletionContextAt) and REPL highlighting on the wasm path. They still need cgo; the wasm module would need exports for queries and cursor classification.
  • The playground (GOOS=js) keeps the token message. wazero doesn't run there; the docs site's web-tree-sitter could.
  • The wasm parse is slower on large inputs than expected. zig's -O level 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.

vito added 2 commits November 12, 2026 18:16
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]>
@vito
vito added this pull request to stack #304 November 12, 2026 18:19
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

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

View logs

@vito
vito merged commit 1313b09 into main Nov 12, 2026
11 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant