Skip to content

fix(wasm): resolve playground "env" module error#39

Merged
JuanMarchetto merged 1 commit into
mainfrom
fix/wasm-playground-env
Feb 26, 2026
Merged

fix(wasm): resolve playground "env" module error#39
JuanMarchetto merged 1 commit into
mainfrom
fix/wasm-playground-env

Conversation

@jeanclawdbotdamn

Copy link
Copy Markdown
Collaborator

Summary

  • The WASM playground fails on load with: Uncaught TypeError: Failed to resolve module specifier "env"
  • Root cause: wasm-bindgen generates import * from "env" for 12 C stdlib functions tree-sitter needs (malloc, free, calloc, realloc, abort, fprintf, snprintf, vsnprintf, fclose, clock, fwrite, fputc). Browsers cannot resolve bare module specifiers.
  • Fix: add an import map to index.html mapping "env"./pkg/env.js, and provide a shim with no-op stubs for all 12 functions
  • The Rust allocator handles actual memory management; these stubs only satisfy WebAssembly.instantiate

Files changed

File Change
playground/env.js New — C stdlib stubs for WASM env imports
playground/index.html Add <script type="importmap">
.github/workflows/pages.yml Copy env.js to _site/pkg/

Test plan

🤖 Generated with Claude Code

The wasm-bindgen generated JS contains `import * from "env"` statements
for 12 C stdlib functions that tree-sitter needs (malloc, free, fprintf,
etc.). Browsers cannot resolve bare module specifiers, causing the
playground to fail on load.

Fix: add an import map to index.html that maps "env" to a local env.js
shim providing no-op stubs. The Rust allocator handles actual memory
management via wasm-bindgen; these C stubs exist only to satisfy
WebAssembly.instantiate.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov

codecov Bot commented Feb 26, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@JuanMarchetto JuanMarchetto merged commit ee4fc67 into main Feb 26, 2026
10 checks passed
jeanclawdbotdamn added a commit that referenced this pull request Feb 26, 2026
tree-sitter's C code references malloc/free/fprintf/etc. which become
unresolved "env" imports in the WASM binary. Browsers cannot resolve the
bare "env" module specifier, causing the playground to fail.

Previous fix (PR #39) added JS shims via import map, but malloc returning
NULL caused tree-sitter to call abort() at runtime.

This fix provides real #[no_mangle] implementations in Rust:
- malloc/calloc/realloc/free: route to Rust's global allocator with an
  8-byte size header so free/realloc can reconstruct the Layout
- fprintf/snprintf/vsnprintf/fwrite/fputc/fclose: no-op stubs (tree-sitter
  declares but never calls these in the validation path)
- clock: returns 0
- abort: triggers wasm32::unreachable trap

The linker resolves these at build time, eliminating all "env" imports
from the generated JS. Removes the now-unnecessary env.js shim and
import map.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@JuanMarchetto JuanMarchetto deleted the fix/wasm-playground-env branch February 26, 2026 18:51
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.

2 participants