Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang-produced Webassembly execution is broken in vite #3847

Closed
6 tasks done
AskAlice opened this issue Jun 17, 2021 · 2 comments · Fixed by #8219
Closed
6 tasks done

Golang-produced Webassembly execution is broken in vite #3847

AskAlice opened this issue Jun 17, 2021 · 2 comments · Fixed by #8219

Comments

@AskAlice
Copy link

AskAlice commented Jun 17, 2021

Describe the bug

#1330 and potentially even prior implementations breaks the way go WASM execution is handled.

The fix for this would be to return the promise for WebAssembly.instantiateStreaming(fetch(response, opts); in packages/vite/src/node/plugins/wasm.ts on lines 17, 33, and 37.

Destructuring

expecting the returned value of WebAssembly.instantiateStreaming(fetch('src/main.wasm'), go.importObject);
and using wasm_exec.js provided by golang

import './wasm_exec.js'; //required for golang
...


  const go = new window.Go();
  const initWasm = async () => {
    const { instance, module } = await main(go.importObject);
    console.log(instance, module);
    await go.run(instance);
  }

produces this error:

Uncaught (in promise) Error: Go.run: WebAssembly.Instance expected
    at global.Go.run (wasm_exec.js:507)
    at initWasm (App.tsx:63)
    at handleClick (App.tsx:67)
    at HTMLUnknownElement.callCallback2 (react-dom.development.js:3945)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
    at invokeGuardedCallback (react-dom.development.js:4056)
    at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4070)
    at executeDispatch (react-dom.development.js:8243)
    at processDispatchQueueItemsInOrder (react-dom.development.js:8275)
    at processDispatchQueue (react-dom.development.js:8288)

running directly:

  const initWasm = async () => {
    const mod = await main(go.importObject);
    mod.run();
  };

produces this error:

wasm_exec.js:167 Uncaught (in promise) TypeError: Cannot read property 'getUint32' of undefined
    at getInt64 (wasm_exec.js:167)
    at loadSlice (wasm_exec.js:236)
    at runtime.getRandomData (wasm_exec.js:338)
    at runtime.getRandomData (main.wasm:0xd2bf7)
    at runtime.schedinit (main.wasm:0x7900f)
    at runtime.rt0_go (main.wasm:0xcfbeb)
    at wasm_export_run (main.wasm:0xd28f9)
    at initWasm (App.tsx:60)
    at async handleClick (App.tsx:68)

using go.run (provided by wasm_exec.js) on the returned object:

import './wasm_exec.js'; //required for golang
...

  const go = new window.Go();
  const initWasm = async () => {
    const mod = await main(go.importObject);
    go.run(mod);
  }

produces this error:

wasm_exec.js:507 Uncaught (in promise) Error: Go.run: WebAssembly.Instance expected
    at global.Go.run (wasm_exec.js:507)
    at initWasm (App.tsx:60)
    at async handleClick (App.tsx:66)

The only way to do this at the moment is to create a ternary based on the current build mode, then use a fetch to the URL.

Reproduction

https://github.com/AskAlice/moshpit.wasm/tree/master/web

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

 💖❯  npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers

  System:
    OS: Windows 10 10.0.21390
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
    Memory: 11.36 GB / 63.92 GB
  Binaries:
    Node: 14.15.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - C:\Program Files\nodejs\yarn.CMD
    npm: 7.17.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 91.0.4472.106
    Edge: Spartan (44.21390.1.0), Chromium (91.0.864.48)
    Internet Explorer: 11.0.21390.1

Used package manager: NPM

Logs


Before submitting the issue, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Provide a description in this issue that describes the bug.
  • Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
@mukaschultze
Copy link

I'm having the same issue trying to import an npm package created using wasm-pack with Rust code.

@Menci
Copy link
Contributor

Menci commented Mar 5, 2022

image

Is this your expected result?

I'm going to create a PR for Vite.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants