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

Error while compiling #1907

Closed Phill030 closed 3 days ago

Phill030 commented 2 weeks ago

Describe the bug

I'm trying to implement my Rust code into Flutter but as soon as I start the debugger, the compilation stops logging only the error, provided in Logs. After running flutter_rust_bridge_codegen generate, the generated Rust files contain many missing Types, from different workspace folders: image & the error:

SEVERE: error[E0277]: std::sync::mpsc::Receiver<ClientPackets> cannot be shared between threads safely

Steps to reproduce

  1. Clone https://github.com/Phill030/flutter_rust_bridge_bug
  2. Run flutter_rust_bridge_codegen generate
  3. Press F5
  4. Watch the Debug Console

Logs

Launching lib\main.dart on Windows in debug mode...
SEVERE : warning : use of `async fn` in public traits is discouraged as auto trait bounds cannot be specified [E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
SEVERE : warning : use of `async fn` in public traits is discouraged as auto trait bounds cannot be specified [E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
4
SEVERE : warning : use of `async fn` in public traits is discouraged as auto trait bounds cannot be specified [E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
SEVERE : warning : `shared` (lib) generated 6 warnings [E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
SEVERE : warning : unused variable: `chat_sender` [E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
SEVERE : warning : unreachable pattern [E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
2
  SEVERE: error[E0277]: `std::sync::mpsc::Receiver<ClientPackets>` cannot be shared between threads safely
SEVERE : warning : `rust_lib_family_app` (lib) generated 2 warnings [E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
SEVERE : error : could not compile `rust_lib_family_app` (lib) due to 2 previous errors; 2 warnings emitted [E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(254,5): error MSB8066: Custom build for 'E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\CMakeFiles\fcafc3ccf4704c172f7bd55d6747d48d\rust_lib_family_app.dll.rule;E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\CMakeFiles\badb3b8f8825f85e9d8271bc892833f0\rust_lib_family_app_cargokit.rule' exited with code -1. [E:\Phill030.de\Flutter\family_app\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
Error: Build process failed.

Exited (1).

Expected behavior

The App should open and there shouldn't be a compilation error.

Generated binding code

No response

OS

Windows 10

Version of flutter_rust_bridge_codegen

2.0.0-dev.32

Flutter info

[√] Flutter (Channel stable, 3.19.6, on Microsoft Windows [Version 10.0.19045.4291], locale de-DE)
    • Flutter version 3.19.6 on channel stable at C:\Users\pmsal\flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 54e66469a9 (2 weeks ago), 2024-04-17 13:08:03 -0700
    • Engine revision c4cd48e186
    • Dart version 3.3.4
    • DevTools version 2.31.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\pmsal\AppData\Local\Android\Sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: D:\AndroidStudio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)       
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.9.6)  
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community  
    • Visual Studio Community 2022 version 17.9.34728.123
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2023.2)
    • Android Studio at D:\AndroidStudio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)       

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19045.4291]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 124.0.6367.91
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 124.0.2478.51

[√] Network resources
    • All expected network resources are available.

• No issues found!

Version of clang++

No response

Additional context

The Rust code itself works without using Flutter.

welcome[bot] commented 2 weeks ago

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

fzyzcjy commented 2 weeks ago

Hi,

SEVERE: error[E0277]: std::sync::mpsc::Receiver<ClientPackets> cannot be shared between threads safely

e.g. https://users.rust-lang.org/t/cannot-be-shared-between-threads-safely/49146

What about putting it in a Mutex

Phill030 commented 2 weeks ago

What about putting it in a Mutex

I thought it is not recommended to edit an autogenerated file?

Phill030 commented 2 weeks ago

Trying to fix it though results in this error in frb_generated.rs

error[E0277]: the trait bound `std::io::Error: SseEncode` is not satisfied
  --> flutter_bridge\src\frb_generated.rs:68:21
   |
67 |                 transform_result_sse(
   |                 -------------------- required by a bound introduced by this call
68 |                     (move || async move { crate::api::main::init_app().await })().await,
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SseEncode` is not implemented for `std::io::Error`
   |
   = help: the following other types implement trait `SseEncode`:
             bool
             i32
             usize
             u8
             anyhow::Error
             RustOpaqueBase<tokio::sync::RwLock<OwnedReadHalf>, frb_generated::MoiArc<tokio::sync::RwLock<OwnedReadHalf>>>
             RustOpaqueBase<tokio::sync::RwLock<OwnedWriteHalf>, frb_generated::MoiArc<tokio::sync::RwLock<OwnedWriteHalf>>>
             RustOpaqueBase<tokio::sync::RwLock<tokio::sync::mpsc::Sender<ClientPackets>>, frb_generated::MoiArc<tokio::sync::RwLock<tokio::sync::mpsc::Sender<ClientPackets>>>>
           and 8 others
note: required by a bound in `transform_result_sse`
  --> flutter_bridge\src\frb_generated.rs:29:1
   |
29 | / flutter_rust_bridge::frb_generated_boilerplate!(
30 | |     default_stream_sink_codec = SseCodec,
31 | |     default_rust_opaque = RustOpaqueMoi,
32 | |     default_rust_auto_opaque = RustAutoOpaqueMoi,
33 | | );
   | | ^
   | | |
   | |_required by a bound in this function
   |   required by this bound in `transform_result_sse`
   = note: this error originates in the macro `$crate::frb_generated_sse_codec` which comes from the expansion of the macro `flutter_rust_bridge::frb_generated_boilerplate` (in Nightly builds, run with -Z macro-backtrace for more info)
fzyzcjy commented 2 weeks ago

I thought it is not recommended to edit an autogenerated file?

Well, I mean editing your code and change the type a little bit.

Phill030 commented 2 weeks ago

It's kinda complicated to import all the types after every code generation and wrapping it in a Mutex doesn't really satisfy my usecase.

fzyzcjy commented 2 weeks ago

Not very sure whether the message is conveyed clearly. So I meant that, maybe change

https://github.com/Phill030/flutter_rust_bridge_bug/blob/558112a8032693bc425a85c760774ada14777ddd/flutter_bridge/src/api/main.rs#L60

to

pub async fn read_messages(mut reader: OwnedReadHalf, sender: Mutex<Sender<ClientPackets>>) {

which will not need to be done after every code generation. Indeed this just changes the source code.

Phill030 commented 2 weeks ago

That would be a unnecessary workaround. My code works fine without the bridge and every time I generate the files, all my types are missing. OwnedReadHalf, OwnedWriteHalf, ClientPackets, ServerPackets, etc. are all not used in the generated files no matter if I change it to a Mutex in my Sourcecode. There must be something wrong with the Generator since the initial error is

error[E0277]: the trait bound `std::io::Error: SseEncode` is not satisfied
  --> flutter_bridge\src\frb_generated.rs:68:21
   |
67 |                 transform_result_sse(
   |                 -------------------- required by a bound introduced by this call
68 |                     (move || async move { crate::api::main::init_app().await })().await,
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SseEncode` is not implemented for `std::io::Error`
   |
   = help: the following other types implement trait `SseEncode`:
             bool
             i32
             usize
             u8
             anyhow::Error
             RustOpaqueBase<tokio::sync::RwLock<OwnedReadHalf>, frb_generated::MoiArc<tokio::sync::RwLock<OwnedReadHalf>>>
             RustOpaqueBase<tokio::sync::RwLock<OwnedWriteHalf>, frb_generated::MoiArc<tokio::sync::RwLock<OwnedWriteHalf>>>
             RustOpaqueBase<tokio::sync::RwLock<tokio::sync::mpsc::Sender<ClientPackets>>, frb_generated::MoiArc<tokio::sync::RwLock<tokio::sync::mpsc::Sender<ClientPackets>>>>
           and 8 others
note: required by a bound in `transform_result_sse`
  --> flutter_bridge\src\frb_generated.rs:29:1
   |
29 | / flutter_rust_bridge::frb_generated_boilerplate!(
30 | |     default_stream_sink_codec = SseCodec,
31 | |     default_rust_opaque = RustOpaqueMoi,
32 | |     default_rust_auto_opaque = RustAutoOpaqueMoi,
33 | | );
   | | ^
   | | |
   | |_required by a bound in this function
   |   required by this bound in `transform_result_sse`
   = note: this error originates in the macro `$crate::frb_generated_sse_codec` which comes from the expansion of the macro `flutter_rust_bridge::frb_generated_boilerplate` (in Nightly builds, run with -Z macro-backtrace for more info)

which has nothing to do with a Mutex

fzyzcjy commented 2 weeks ago

Oh I see, I originally thought you meant a "cannot be shared between threads safely" errror. In that case, could you please provide a minimal reproducible sample (e.g. create a brand new project, modify as few lines as possible in it, and run it, and see the error)?

Phill030 commented 2 weeks ago

I think I may have found one possible cause. switching the init_app() function in Rust for example to async, breaks the generated code by throwing this error:

error[E0277]: the trait bound `impl std::future::Future<Output = ()>: SseEncode` is not satisfied
  --> src\frb_generated.rs:66:38
   |
66 |                 transform_result_sse((move || Result::<_, ()>::Ok(crate::api::main::init_app()))())
   |                 -------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SseEncode` is not implemented for `impl std::future::Future<Output = ()>`
   |                 |
   |                 required by a bound introduced by this call
   |
   = help: the following other types implement trait `SseEncode`:
             bool
             i32
             ()
note: required by a bound in `transform_result_sse`
  --> src\frb_generated.rs:28:1
   |
28 | / flutter_rust_bridge::frb_generated_boilerplate!(
29 | |     default_stream_sink_codec = SseCodec,
30 | |     default_rust_opaque = RustOpaqueMoi,
31 | |     default_rust_auto_opaque = RustAutoOpaqueMoi,
32 | | );
   | | ^
   | | |
   | |_required by a bound in this function
   |   required by this bound in `transform_result_sse`
   = note: this error originates in the macro `$crate::frb_generated_sse_codec` which comes from the expansion of the macro `flutter_rust_bridge::frb_generated_boilerplate` (in Nightly builds, run with -Z macro-backtrace for more info)
#[flutter_rust_bridge::frb(init)]
pub async fn init_app() {
[...]
}

is there any alternative?

fzyzcjy commented 2 weeks ago

Nice observation - that looks like a bug, async fn is supported by frb. Feel free to PR, alternatively I will work on it in the next batch (hopefully within several days together with other things)

Btw, what if you remove the #[frb(init)] - does it still error?

P.S. after modification, flutter_rust_bridge_codgen generate needs to be re-run

Phill030 commented 2 weeks ago

It's a weird behavior, I created a new project with FRB and I got no errors when using async in the init function. In my old script I commented all my Rust code (only in the Bridge folder) and got following error AFTER trying to launcher Flutter with no Rust errors visible:

SEVERE : error : failed to run custom build command for `dart-sys-fork v4.1.1` [C:\Users\Philipp\Downloads\flutter_rust_bridge_bug-master\flutter_rust_bridge_bug-master\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
  SEVERE:   C:\Users\Philipp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\dart-sys-fork-4.1.1\dart-sdk\include\dart_api_dl.c(26): warning C4152: nonstandard extension, function/data pointer conversion in expression
  SEVERE:   C:\Users\Philipp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\dart-sys-fork-4.1.1\dart-sdk\include\dart_api_dl.c(33): warning C4100: 'external_size': unreferenced formal parameter
  SEVERE:   C:\Users\Philipp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\dart-sys-fork-4.1.1\dart-sdk\include\dart_api_dl.c(33): warning C4100: 'object': unreferenced formal parameter
  SEVERE:   C:\Users\Philipp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\dart-sys-fork-4.1.1\dart-sdk\include\dart_api_dl.c(40): warning C4100: 'external_allocation_size': unreferenced formal parameter
  SEVERE:   C:\Users\Philipp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\dart-sys-fork-4.1.1\dart-sdk\include\dart_api_dl.c(39): warning C4100: 'strong_ref_to_object': unreferenced formal parameter
  SEVERE:   C:\Users\Philipp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\dart-sys-fork-4.1.1\dart-sdk\include\dart_api_dl.c(38): warning C4100: 'object': unreferenced formal parameter
  SEVERE:   C:\Users\Philipp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\dart-sys-fork-4.1.1\dart-sdk\include\dart_api_dl.c : fatal error C1083: Cannot open compiler generated file: '': Invalid argument
SEVERE : error occurred: Command "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.39.33519\\bin\\HostX64\\x64\\cl.exe" "-nologo" "-MD" "-Z7" "-Brepro" "-I" "C:\\Users\\Philipp\\.cargo\\registry\\src\\index.crates.io-6f17d22bba15001f\\dart-sys-fork-4.1.1\\dart-sdk\\include" "-W4" "-FoC:/Users/Philipp/Downloads/flutter_rust_bridge_bug-master/flutter_rust_bridge_bug-master/family_app/build/windows/x64/plugins/rust_lib_family_app/cargokit_build\\x86_64-pc-windows-msvc\\debug\\build\\dart-sys-fork-5065ce4c84b1ffbf\\out\\d1c77295b97ecd40-dart_api_dl.o" "-c" "C:\\Users\\Philipp\\.cargo\\registry\\src\\index.crates.io-6f17d22bba15001f\\dart-sys-fork-4.1.1\\dart-sdk\\include\\dart_api_dl.c" with args cl.exe did not execute successfully (status code exit code: 1). [C:\Users\Philipp\Downloads\flutter_rust_bridge_bug-master\flutter_rust_bridge_bug-master\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
SEVERE : warning : build failed, waiting for other jobs to finish... [C:\Users\Philipp\Downloads\flutter_rust_bridge_bug-master\flutter_rust_bridge_bug-master\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(254,5): error MSB8066: Custom build for 'C:\Users\Philipp\Downloads\flutter_rust_bridge_bug-master\flutter_rust_bridge_bug-master\family_app\build\windows\x64\CMakeFiles\a02731e01f04b1b06338480881f96f2c\rust_lib_family_app.dll.rule;C:\Users\Philipp\Downloads\flutter_rust_bridge_bug-master\flutter_rust_bridge_bug-master\family_app\build\windows\x64\CMakeFiles\b2a1a40e75578b69304d3a8b851273ea\rust_lib_family_app_cargokit.rule' exited with code -1. [C:\Users\Philipp\Downloads\flutter_rust_bridge_bug-master\flutter_rust_bridge_bug-master\family_app\build\windows\x64\plugins\rust_lib_family_app\rust_lib_family_app_cargokit.vcxproj]
Error: Build process failed.
fzyzcjy commented 2 weeks ago

Hmm, maybe the old one has some wrong configurations? One way without debugging is to just create a brand new project, and copy-paste original code to it.

Phill030 commented 2 weeks ago

I've created a new project, which only imports the dependencies from a workspace. The code itself works after manually importing the types inside the generated file but it seems like the Thread is blocking the Window from showing up. grafik

Phill030 commented 2 weeks ago

Update: I don't know how but it finally works grafik

Only issue right now is manually importing all my types. Interesting thing, that as soon as I import them in frb_generated.rs, it appears, that they don't need to be imported in frb_generated.io.rs

fzyzcjy commented 2 weeks ago

that as soon as I import them in frb_generated.rs, it appears, that they don't need to be imported in frb_generated.io.rs

Btw, just to be sure: the generated files should not be modified by hand.

As for imports, just pub use YourType in your files, and it should be automatically useable.

fzyzcjy commented 3 days ago

Close since this seems to work. Feel free to reopen if having any issues!