fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
3.61k stars 254 forks source link

Build for Web fails: error: cannot import from modules (`env`) with `--no-modules` #1915

Closed flowhorn closed 2 weeks ago

flowhorn commented 2 weeks ago

Describe the bug

I try to integrate the zstd library into the rust code. When doing this, I get this to compile for all targets except for wasm/web.

This is the error message I received:

error: cannot import from modules (`env`) with `--no-modules`
Error: Running the wasm-bindgen CLI
Caused by: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit status: 1

My cargo.toml includes these two dependencies:

[dependencies]
flutter_rust_bridge = "=2.0.0-dev.32"
zstd = { git = "https://github.com/jbms/zstd-rs.git", branch = "fix-wasm32-unknown-unknown" }

I got zstd to compile to the wasm32-unknown-unknown target, but I didn't got this to work with the no-modules restriction, do you have any solution for this? As there is no support for zstd in dart, I thought that this would be useful to use the rust_bridge for, and on other targets this worked perfectly.

Best regards.

Steps to reproduce

Add these dependencies:

[dependencies] flutter_rust_bridge = "=2.0.0-dev.32" zstd = { git = "https://github.com/jbms/zstd-rs.git", branch = "fix-wasm32-unknown-unknown" }

utils.rs (+ adding this in mod.rs):

use zstd::stream::{decode_all, encode_all};

#[flutter_rust_bridge::frb(sync)] // Synchronous mode for simplicity of the demo
pub fn compress_zstd(data: Vec<u8>, compression_level: i32) -> Vec<u8> {
    encode_all(data.as_slice(), compression_level).unwrap()
}

#[flutter_rust_bridge::frb(sync)] // Synchronous mode for simplicity of the demo
pub fn decompress_zstd(data: Vec<u8>) -> Vec<u8> {
    decode_all(data.as_slice()).unwrap()
}

Running flutter_rust_bridge_codegen build-web

Logs

error: cannot import from modules (`env`) with `--no-modules`
Error: Running the wasm-bindgen CLI
Caused by: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit status: 1

Command: wasm-pack build -t no-modules -d /[...]/my_app/web/pkg --no-typescript --out-name rust_lib_my_app --dev rust -- -Z build-std=std,panic_abort
#0      runCommand (package:flutter_rust_bridge/src/cli/run_command.dart:67:5)
<asynchronous suspension>
#1      _executeWasmPack (package:flutter_rust_bridge/src/cli/build_web/executor.dart:143:3)

Expected behavior

No response

Generated binding code

No response

OS

MacOS

Version of flutter_rust_bridge_codegen

2.0.0-dev.32

Flutter info

No response

Version of clang++

Homebrew clang version 18.1.4

Additional context

No response

welcome[bot] commented 2 weeks ago

Hi! Thanks for opening your first issue here! :smile:

fzyzcjy commented 2 weeks ago

Hmm, looks like currently frb needs no-modules but may be relaxed - feel free to PR for this! On the other hand, maybe we can ask in zstd repo about whether it can be compiled under no-modules mode.

In addition, it may also be some bug in wasm compile: https://github.com/rust-lang/rust/issues/93557 - could you please try that and see whether it solves the problem?

flowhorn commented 2 weeks ago

Thank you for the quick reply and telling about the limitation of no-modules. I will create in issue in the zstd-rs library for using the library with no-modules, as no trick with wasm-pack helped me actually compiling it. For now I switched to another library for zstd: ruzstd. This worked for me for now.

It's fun to integrate rust code into my app, thank you for your work!

fzyzcjy commented 2 weeks ago

You are welcome and happy to see it works!

github-actions[bot] commented 11 hours ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.