Emurgo / cardano-serialization-lib

This is a library, written in Rust, for serialization & deserialization of data structures used in Cardano's Haskell implementation of Alonzo along with useful utility functions.
Other
231 stars 125 forks source link

__dirname is not sufficient to derive the path to WASM file #624

Closed mPaella closed 1 year ago

mPaella commented 1 year ago

Problem

The compiled lib uses __dirname to resolve the path to cardano_serialization_lib_bg.wasm file, which does not always resolve correctly. Causing Error: ENOENT: no such file or directory, open '/project/sandbox/.next/server/app/foo/cardano_serialization_lib_bg.wasm'

Reproduction

https://codesandbox.io/p/sandbox/gifted-einstein-mo56ss?file=%2Fnext.config.js%3A19%2C7

Fix?

As a hack to see if i could fix it, I tried editing the compiled files for @emurgo/cardano-serialization-lib under node_modules.

OLD - Not working - line ~14053 in the compiled code

const path = require('path').join(__dirname, 'cardano_serialization_lib_bg.wasm');

NEW - Working

const path = require('path').join(process.cwd(), "node_modules", "@emurgo", "cardano-serialization-lib-nodejs", 'cardano_serialization_lib_bg.wasm');
mPaella commented 1 year ago

This appears to be a problem with wasm-pack actually, ill bring it up over there

lisicky commented 1 year ago

Hi @mPaella ! Why did you choose the node csl version and not the browser version ?

mPaella commented 1 year ago

@lisicky I'm using @blockfrost/blockfrost-js which uses the node package.

Which i'm pretty confident is correct, as the code isn't being run in the browser.

If you are familiar with NextJs, in the reproduction you will see that the code is being run inside getServerSideProps (working correctly, pages directory, node env), as well as inside the async server component (NOT working, app directory, still node env)

lisicky commented 1 year ago

Thanks for clarification @mPaella ! It's better to create issue in Wasm-pack or next.js repo. Because it is issue of third-party tool

lisicky commented 1 year ago

Seems it was done by https://github.com/vercel/next.js/issues/49783