unjs / unwasm

🇼 WebAssembly tools for JavaScript
MIT License
178 stars 6 forks source link

prisma compatibility #21

Open pi0 opened 8 months ago

pi0 commented 8 months ago

Yhere are probably easier ways to include a repro, but I ran in to the same problem with Prismas new database adapters.

Here's a repro-link codesandbox.io.

If you try yourself with prisma, it does require you to have generated the prisma client before building:

node_modules/.bin/prisma generate

Originally posted by @Zn4rK in https://github.com/unjs/unwasm/issues/19#issuecomment-1984137271

Zn4rK commented 8 months ago

FWIW;

That reproduction uses Prismas dev version 5.11.0-dev.5.

When patched with mukundshahs patch, it did build, but it did not work when previewed with Wrangler.

It looks like Prisma 5.11.0 will be released very soon, and should probably have a stabilised wasm path.

When I tried the latest version of 5.11.0 (5.11.0-dev.52), with the patch, I got the following error when building instead:

[nitro 11:29:45 AM]  ERROR  Error: Could not load /SOME_PATH/node_modules/.pnpm/@prisma+client@5.11.0-dev.52_prisma@5.11.0-dev.52/node_modules/.prisma/client/query_engine_bg.wasm?module (imported by node_modules/.pnpm/@prisma+client@5.11.0-dev.52_prisma@5.11.0-dev.52/node_modules/.prisma/client/wasm-edge-light-loader.js): ENOENT: no such file or directory, open '/SOME_PATH/node_modules/.pnpm/@prisma+client@5.11.0-dev.52_prisma@5.11.0-dev.52/node_modules/.prisma/client/query_engine_bg.wasm?module'

The path

/SOME_PATH/node_modules/.pnpm/@prisma+client@5.11.0-dev.52_prisma@5.11.0-dev.52/node_modules/.prisma/client/query_engine_bg.wasm

does exists. If I manually remove the querystring (?module), and apply the patch, it does work.

Zn4rK commented 8 months ago

The prisma team has some integration tests here that might be helpful: https://github.com/prisma/ecosystem-tests/tree/dev/driver-adapters-wasm

pi0 commented 8 months ago

Thanks for the updates and info dear @Zn4rK ❤️

SevInf commented 7 months ago

Hey everyone, Sehii from Prisma team here. We would really like to support Nitro and Nuxt edge deployments and I did my own short investigation into the problem. You can find basic reporduction of the problem here. Offending code seems to be here - it assumes that import modules are valid JS identifiers which is not what wasm-bindgen does by default - idetifier need to be escaped.

Hope it helps!

pi0 commented 7 months ago

Thanks for the pointer dear @SevInf. I'm going to work on prisma driver integration soon (congrats on last release!) and will investigate this one as well.

(mainly for here we need a minimal reproduction or regression test. if you are able to provide it before me feel free to directly make PR 👍🏼 )

pi0 commented 7 months ago

https://github.com/unjs/unwasm/pull/24 landed with a relevant fix. Keeping this issue open until making sure prisma is fully supported.

SevInf commented 7 months ago

@pi0 thanks for the fix! I've updated by reproduction to the latest unwasm version. Now we are getting different error:

[unwasm] Failed to parse WASM module wasm/query_engine_bg-5343c5664d9b411c.wasm: Error: [unwasm] Failed to parse wasm/query_engine_bg-5343c5664d9b411c.wasm: Error: Unexpected instruction: 0xc1
    at parseWasm (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:6479:11)
    at parse (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/plugin.mjs:195:22)
    at Object.transform (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/plugin.mjs:272:73)
    at /Users/serhii/projects/unwasm-bug/node_modules/rollup/dist/shared/rollup.js:998:40 {
  [cause]: CompileError2: Unexpected instruction: 0xc1
      at parseInstructionBlock (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5351:19)
      at parseInstructionBlock (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5424:13)
      at parseInstructionBlock (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5424:13)
      at parseInstructionBlock (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5424:13)
      at parseInstructionBlock (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5424:13)
      at parseInstructionBlock (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5424:13)
      at parseInstructionBlock (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5424:13)
      at parseInstructionBlock (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5424:13)
      at parseInstructionBlock (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5424:13)
      at parseCodeSection (file:///Users/serhii/projects/unwasm-bug/node_modules/unwasm/dist/tools.mjs:5328:11)

0xc1 is a valid wasm instruction, so It looks like @webassmpblyjs/parser is not up to date with a spec.

pi0 commented 7 months ago

yes i guess i need to patch it again 😅 In the meantime, you can use ?module as workaround it will skip parsing.