assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Fix CLI metadata startup loading in WebContainers

Open culpen90 opened this issue 5 months ago • 3 comments

Fixes #3027.

Summary

  • defer compiler and Binaryen initialization until after metadata-only CLI exits
  • avoid a static Binaryen import in the built asc bundle
  • install an instantiateStreaming fallback while loading compiler startup dependencies, so environments with broken streaming support can still compile
  • add browser test coverage for the version path, bundle shape, and first compile with broken instantiateStreaming

Validation

  • corepack npm run build
  • corepack npm run lint
  • corepack npm run test:browser
  • corepack npm run test:cli
  • corepack npm run test:transform:esm
  • corepack npm run test:transform:cjs

culpen90 avatar Jun 13 '26 20:06 culpen90

Wouldn't the underlying problem still be present when you try to compile something?

Also, from my testing, I think the issue is that StackBlitz doesn't properly support WebAssembly.instantiateStreaming(), at least in the form WebAssembly.instantiateStreaming(fetch("data:application/wasm;base64,...")).

CountBleck avatar Jun 13 '26 21:06 CountBleck

@CountBleck The first version only avoided loading the compiler/Binaryen for metadata-only exits, so yes, an actual compile could still hit the same broken startup wasm path.

I pushed a follow-up in 0ab3f456 that covers the compile path too. The CLI now installs a temporary WebAssembly.instantiateStreaming fallback while loading the compiler startup dependencies and Binaryen: it still tries native streaming first, but if that fails it falls back to WebAssembly.instantiate(await response.arrayBuffer(), imports).

I also added browser regression coverage that patches WebAssembly.instantiateStreaming to throw and verifies asc.compileString(...) still succeeds, in addition to the existing asc --version check.

culpen90 avatar Jun 13 '26 22:06 culpen90

install an instantiateStreaming fallback while loading compiler startup dependencies, so environments with broken streaming support can still compile

Hmm, I'm not sure we should provide compile streaming fallback from our side. importWithInstantiateStreamingFallback add extra async abstructions for compile streaming for most of browsers which support streaming.

It's much better to have the environment load the polyfill if it really required this by itself. For example use this one: https://www.npmjs.com/package/wasm-instantiate-streaming

MaxGraey avatar Jun 14 '26 08:06 MaxGraey