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

WebAssembly with Reference Types cannot be parsed #15566

Closed
CryZe opened this issue Mar 20, 2022 · 35 comments · Fixed by #18940
Closed

WebAssembly with Reference Types cannot be parsed #15566

CryZe opened this issue Mar 20, 2022 · 35 comments · Fixed by #18940
Labels

Comments

@CryZe
Copy link

CryZe commented Mar 20, 2022

Reopened #13559

Bug report

WebAssembly modules with enabled Reference Types cannot be loaded. The webpack version I tested this with is 5.38.1

For a minimal reproduction I created this repository: https://github.com/Tarnadas/webpack-5-wasm-reftypes

The error message is the following:

ERROR in ./pkg/webpack_5_wasm_reftypes_bg.wasm
Module parse failed: Internal failure: parseVec could not cast the value
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
Error: Internal failure: parseVec could not cast the value
    at new CompileError (/home/marior/projects/webpack-5-wasm-reftypes/node_modules/@webassemblyjs/helper-api-error/lib/index.js:40:109)
    at parseVec (/home/marior/projects/webpack-5-wasm-reftypes/node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:328:15)
    at parseTypeSection (/home/marior/projects/webpack-5-wasm-reftypes/node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:360:22)
    at parseSection (/home/marior/projects/webpack-5-wasm-reftypes/node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:1363:24)
    at Object.decode (/home/marior/projects/webpack-5-wasm-reftypes/node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:1712:25)
    at decode (/home/marior/projects/webpack-5-wasm-reftypes/node_modules/@webassemblyjs/wasm-parser/lib/index.js:248:21)
    at WebAssemblyParser.parse (/home/marior/projects/webpack-5-wasm-reftypes/node_modules/webpack/lib/wasm-async/AsyncWebAssemblyParser.js:48:19)
    at /home/marior/projects/webpack-5-wasm-reftypes/node_modules/webpack/lib/NormalModule.js:1018:26
    at processResult (/home/marior/projects/webpack-5-wasm-reftypes/node_modules/webpack/lib/NormalModule.js:745:11)
    at /home/marior/projects/webpack-5-wasm-reftypes/node_modules/webpack/lib/NormalModule.js:809:5
 @ ./pkg/webpack_5_wasm_reftypes.js 1:0-58 4:0-21
 @ ./src/index.js 1:0-16

webpack 5.38.1 compiled with 1 error in 572 ms
error Command failed with exit code 1.

I used Rust with wasm-bindgen, which provides a cli option to enable reference types. Instead of directly using wasm-bindgen, I’ve been using wasm-pack, but since it’s unmaintained, you will need my fork to pass the cli option down to wasm-bindgen. You can see my PR for wasm-pack here: rustwasm/wasm-pack#888

By looking at the error message, it seems to be a problem with the wasm parser library. I checked its repository and couldn’t find any mentions about reference types unfortunately, so I’m not sure how to proceed with this.

@vankop
Copy link
Member

vankop commented Mar 21, 2022

I think parser does not support them.. (@webassemblyjs/wasm-parser) . Right now there is no workaround for this.

@vankop vankop added webpack-5 wasm wasm related issues labels Mar 21, 2022
@enomado
Copy link

enomado commented Mar 24, 2022

Why webpack try to parse it? can we just use file-loader?

@vankop
Copy link
Member

vankop commented Mar 24, 2022

Why webpack try to parse it?

There are some exports optimization.. so webpack parsing wasm.

can we just use file-loader?

yes, sure. also in webpack@5 there is asset type.

@sokra
Copy link
Member

sokra commented Apr 7, 2022

@xtuc

@webpack-bot
Copy link
Contributor

This issue had no activity for at least three months.

It's subject to automatic issue closing if there is no activity in the next 15 days.

@CryZe
Copy link
Author

CryZe commented Jul 7, 2022

ping

@webpack-bot
Copy link
Contributor

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

@CryZe
Copy link
Author

CryZe commented Oct 22, 2022

Last time you at least gave a warning, and it's marked as completed?? This has to be the worst bot ever.

@CryZe
Copy link
Author

CryZe commented Feb 6, 2023

Ok dude

@alexander-akait
Copy link
Member

@xtuc Friendly ping

@Havunen
Copy link

Havunen commented Oct 18, 2024

I just updated to 1.82 and now this problem started happening, what is the recommended solution?

for now I downgraded to Rust 1.81, hopefully somebody can fix this properly

@CryZe
Copy link
Author

CryZe commented Oct 18, 2024

This is btw not just a Rust issue, as it should affect C, C++, Zig and co. the same way as it got changed in LLVM to be turned on by default.

@CryZe
Copy link
Author

CryZe commented Oct 18, 2024

Stripping solves the problem:

[profile.release]
strip = true

This isn't great for debug builds however, as you usually want the debug symbols. I believe the reason why stripping works isn't because it does any stripping, but because it likely optimizes the overlong immediates encoding back to how it was before LLVM 19. Running wasm-opt probably behaves the same way.

@bes
Copy link

bes commented Oct 18, 2024

Stripping solves the problem:

[profile.release]
strip = true

This isn't great for debug builds however, as you usually want the debug symbols. I believe the reason why stripping works isn't because it does any stripping, but because it likely optimizes the overlong immediates encoding back to how it was before LLVM 19. Running wasm-opt probably behaves the same way.

@CryZe What problem did you solve by stripping? I tried stripping but I still get the same error:

Can't import the named export '__wbindgen_start' (imported as 'wasm') from default-exporting module (only default export is available)

EDIT; stripping seems to work, but not in combination with the suggested module/rules fix by @alexander-akait above. Stripping has some other issues, but it's better than nothing!

@CryZe
Copy link
Author

CryZe commented Oct 18, 2024

Without the babel-loader workaround that you tried.

@bes
Copy link

bes commented Oct 18, 2024

Sorry, yeah, I just tried that and it worked (edited my reply)

@bes
Copy link

bes commented Oct 18, 2024

Just as a FYI, it's still not possible to actually use reference types (as far as I can tell). Stripping, gets me as far as "my old binary without reference types works".

To get reference types to even pass compile/wasm-pack/wasm-opt I needed to:

//.cargo/config.toml
[target.wasm32-unknown-unknown]
rustflags = [
    "-C", "target-feature=+bulk-memory,+mutable-globals"
]

// Cargo.toml
[package.metadata.wasm-pack.profile.release]
wasm-opt = ['-Oz', '--enable-bulk-memory']

// Shell / Building
  wasm-pack build \
    --release \
    --weak-refs \
    --reference-types \
    --target bundler \
    --scope "myscope" .

But then webpack says

Module parse failed: Internal failure: parseVec could not cast the value
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
Error: Internal failure: parseVec could not cast the value

I don't need reference types, I just wanted to see how far I would get.

@stephanemagnenat
Copy link

I believe the reason why stripping works isn't because it does any stripping, but because it likely optimizes the overlong immediates encoding back to how it was before LLVM 19.

I can confirm this, as switching back to WebAssembly MVP features with:

[package.metadata.wasm-pack.profile.release]
wasm-opt = ['-Os', '-mvp', '--strip-target-features']

does not solve the problem. I did not find a way to use wasm-opt to change the immediate encoding back to something supported by webpack.

@stephanemagnenat
Copy link

Note that in big projects with a workspace having multiple crates, the current webpack limitation is really annoying, because due to a cargo limitation, the strip option must be specified at the workspace level, so that would affect all crates including the ones not compiled for WASM targets. As @CryZe mentioned, that is really a problem for debugging.

@CryZe
Copy link
Author

CryZe commented Oct 21, 2024

On the weekend I tracked down the issue back into wasm-bindgen which apparently uses the target_features section to detect if it should automatically enable its full reference types support. I opened a PR that ensures that you still have to opt into that manually, so that should fix all issues that were caused by Rust 1.82: rustwasm/wasm-bindgen#4213

@alexander-akait
Copy link
Member

@bes can you make a reproducible example so that I can examine it?

@stephanemagnenat

Note that in big projects with a workspace having multiple crates, the current webpack limitation is really annoying, because due to a cargo limitation

This is not our limitation because we simply cannot run the parser, it crashes with an error, this should be fixed in @webassemblyjs/wasm-parser, that's why I ping @xtuc

@bes
Copy link

bes commented Oct 21, 2024

@alexander-akait
Copy link
Member

@bes Yeah, I see the problem, need wait - rustwasm/wasm-bindgen#4211, but anyway, it will not solve - xtuc/webassemblyjs#1146, we need someone to send a pr and solve this issue, unfortunately my knowledge in you is not deep enough to complete this task.

I would say more that this problem is more a question of the node ecosystem, not just webpack

@xtuc
Copy link
Member

xtuc commented Nov 6, 2024

Version 1.13.1 has been published with reference types support. We need to update in webpack.

@alexander-akait
Copy link
Member

@xtuc I tried 1.13.1 update and still get - Error: Internal failure: parseVec could not cast the value, repro - https://github.com/bes/webpack-wasm-reference-types-1.82-poc

@alexander-akait
Copy link
Member

The same - #18940

@xtuc
Copy link
Member

xtuc commented Nov 6, 2024

Thanks for checking @alexander-akait . I'm having a look.

@xtuc
Copy link
Member

xtuc commented Nov 6, 2024

I published 1.14.1. Could you please try again @alexander-akait ?

Sorry I had some issues with my publish setup.

@mateuszJS
Copy link

@xtuc Works for me!

    "@webassemblyjs/ast": "^1.14.1",
    "@webassemblyjs/wasm-edit": "^1.14.1",
    "@webassemblyjs/wasm-parser": "^1.14.1",

https://github.com/mateuszJS/rust-webpack-v5-template/tree/webpack-v5-migration
Thank you for great work 👍

@stephanemagnenat
Copy link

@xtuc works for me as well! Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.