fzyzcjy / flutter_rust_bridge

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

Error: 'initMockImpl' isn't defined for the type 'RustLib' #2413

Open ethicnology opened 1 day ago

ethicnology commented 1 day ago

Describe the bug

I maintain the rust-bip85 library and am working on porting it to the Flutter community, leveraging your excellent tool.

I’ve encountered an issue in a Flutter project that involves multiple packages using flutter_rust_bridge.

I've noticed that some package such as bdk_flutter works with pre-compiled binaries. Other package like lwk has a dedicated await LibLwk.init()

Could you help me understand why in the case of a new project I face no issues, but run into problems when working with multiple other flutter_rust_bridge projects ?

What are the steps to produce a package that is compatible with others ?

It would be really helpful to have a step-by-step guide for properly wrapping a Rust project for pub.dev

Steps to reproduce

  1. git clone git@github.com:SatoshiPortal/bullbitcoin-mobile.git

  2. flutter pub add bip85

  3. Add in lib/main.dart

    import 'lib/package:bip85/bip85.dart' as bip85;
    …
    
    runZonedGuarded(() async {
    if (!fromTest) WidgetsFlutterBinding.ensureInitialized();
    await LibLwk.init();
    await LibBoltz.init();
    
    await bip85.RustLib.init();
    const xprv = 'xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaLLHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb';
    final mnemonic = bip85.toMnemonic(xprv: xprv, wordCount: 12, index: 0);
    print(mnemonic);
    …
  4. flutter run ios/android simulator

Logs

Error (Xcode):
/lib/src/rust/frb_generated.dart:39:14: Error: The method 'initMockImpl' isn't defined for
the class 'RustLib'.

Expected behavior

No response

Generated binding code

No response

OS

MacOS

Version of flutter_rust_bridge_codegen

2.6

Flutter info

[✓] Flutter (Channel stable, 3.24.5, on macOS 15.0.1 24A348 darwin-arm64, locale
    en-US)
    • Flutter version 3.24.5 on channel stable at
      /opt/homebrew/Caskroom/flutter/3.24.3/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision dec2ee5c1f (7 days ago), 2024-11-13 11:13:06 -0800
    • Engine revision a18df97ca5
    • Dart version 3.5.4
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/azad/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /Applications/Android
      Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16B40
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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.11+0-17.0.11b1207.24-11852314)

[✓] VS Code (version 1.95.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.100.0

[✓] Connected device (4 available)            
    • iPhone 16 Plus (mobile)         • 4662AA9C-CCDA-427E-892A-D3F0F981B7F0 •
      ios            • com.apple.CoreSimulator.SimRuntime.iOS-18-0 (simulator)
    • macOS (desktop)                 • macos                                •
      darwin-arm64   • macOS 15.0.1 24A348 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad                •
      darwin         • macOS 15.0.1 24A348 darwin-arm64
    • Chrome (web)                    • chrome                               •
      web-javascript • Google Chrome 130.0.6723.119

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

Version of clang++

No response

Additional context

No response

fzyzcjy commented 1 day ago

What are the steps to produce a package that is compatible with others ?

What about:

https://cjycode.com/flutter_rust_bridge/quickstart

image

Feel free to ping me if you have other questions after using this!

ethicnology commented 1 day ago

Yes, I've built my package using the plugin template but is it enough ?

I've also follow the guide for precompiled_binaries using cargokit

ethicnology commented 1 day ago

To solve this error:

Error (Xcode):
/lib/src/rust/frb_generated.dart:39:14: Error: The method 'initMockImpl' isn't defined for
the class 'RustLib'.

I have deleted the initMock code from frb_generated.dart but flutter_rust_bridge_codegen generate keep re-generate it:

  /// Initialize flutter_rust_bridge in mock mode.
  /// No libraries for FFI are loaded.
  static void initMock({
    required LibBip85Api api,
  }) {
    instance.initMockImpl(
      api: api,
    );
  }
fzyzcjy commented 22 hours ago

Hmm, looks like a bug that some functions is not generated? Could you please provide a minimal reproducible sample (probably just a almost blank project)?

ethicnology commented 19 hours ago

Using 2.6.0 execute flutter_rust_bridge_codegen create new_plugin --template plugin and you can see initMock with an undefined initMockImpl, I guess it happened when I bump 2.5.0 to 2.6.0:

  /// Initialize flutter_rust_bridge in mock mode.
  /// No libraries for FFI are loaded.
  static void initMock({
    required RustLibApi api,
  }) {
    instance.initMockImpl(
      api: api,
    );
  }
fzyzcjy commented 19 hours ago

Great, I will check that later.

Btw, is your flutter_rust_bridge dart dependency in pubspec.yaml latest? IMHO in https://github.com/fzyzcjy/flutter_rust_bridge/blob/master/frb_dart/lib/src/main_components/entrypoint.dart#L68 the initMockImpl is there, so it should be able to find it.

ethicnology commented 18 hours ago

When I execute the command flutter_rust_bridge_codegen create new_plugin --template plugin the flutter_rust_bridge dependency is 2.6.0 in the pubspec.yaml.

On my own project, I started with 2.5.0, then upgraded to 2.6.0 and later, I had to downgrade to 2.0.0 because most other projects that rely on flutter_rust_bridge are stuck in this version and it creates incompatibility.

By the way, to reduce incompatibilities between packages made by people using rust_flutter_bridge , it would be great if version is ^2.X.X instead of strictly 2.X.X but I don't know if there are restrictions about this.

This tool is great, it help us to grow the dart/flutter ecosystem

fzyzcjy commented 18 hours ago

I had to downgrade to 2.0.0

Ok then I guess this is the issue: The 2.6.0 codegen generates code that is compatible with 2.6.0 runtime, instead of 2.0.0, which does not have that new function.

By the way, to reduce incompatibilities between packages made by people using rust_flutter_bridge , it would be great if version is ^2.X.X instead of strictly 2.X.X but I don't know if there are restrictions about this.

Yes that would be great. But currently it is the latter, because new codegen generated code often requires new code in the runtime. For example, the initMockImpl in the runtime as you have seen.

This tool is great, it help us to grow the dart/flutter ecosystem

Thanks and happy to see the ecosystem growing!

ethicnology commented 18 hours ago

Ok then I guess this is the issue: The 2.6.0 codegen generates code that is compatible with 2.6.0 runtime, instead of 2.0.0, which does not have that new function.

On my project yes. But the error initMockImpl isn't defined for the type 'RustLib'. appears on a brand new --template plugin that is fully 2.6.0

Thanks and happy to see the ecosystem growing!

Thank to your work!

fzyzcjy commented 18 hours ago

But the error

Hmm then it's a bug. I guess it is relatively easy to fix, so feel free to PR! Alternatively I will work on it in the next batch.

Thank to your work!

You are welcome!

ethicnology commented 13 hours ago

Is it only to add the following there ?

  @protected
  void initMockImpl({
    required A api,
  }) {
    if (__state != null) {
      throw StateError('Should not initialize flutter_rust_bridge twice');
    }

    __state = _FakeEntrypointState(api: api);
  }
fzyzcjy commented 7 hours ago

No, indeed I think it should already work, because the class extends BaseEntrypoint, and BaseEntrypoint has a initMockImpl method...

fzyzcjy commented 7 hours ago

Well I cannot reproduce the bug:

Create it

(base) ➜  temp flutter_rust_bridge_codegen create --template plugin hi_app
[2024-11-22T01:05:37.800Z INFO frb_codegen/src/library/commands/flutter.rs:24] Execute `flutter create hi_app --template plugin_ffi --platforms android,ios,linux,macos,windows` (this may take a while)
[2024-11-22T01:05:46.736Z INFO frb_codegen/src/library/integration/creator.rs:45] Step: Inject flutter_rust_bridge related code
[2024-11-22T01:05:46.739Z INFO frb_codegen/src/library/integration/integrator.rs:37] Overlay template onto project
[2024-11-22T01:05:46.750Z INFO frb_codegen/src/library/integration/integrator.rs:65] Modify file permissions
[2024-11-22T01:05:46.750Z INFO frb_codegen/src/library/integration/integrator.rs:68] Add pub dependencies
[2024-11-22T01:05:46.750Z INFO frb_codegen/src/library/commands/flutter.rs:37] Execute flutter pub add integration_test --dev --sdk=flutter (this may take a while)
[2024-11-22T01:05:54.616Z INFO frb_codegen/src/library/commands/flutter.rs:37] Execute flutter pub add flutter_rust_bridge:2.6.0 (this may take a while)
[2024-11-22T01:06:01.550Z INFO frb_codegen/src/library/commands/flutter.rs:37] Execute flutter pub add integration_test --dev --sdk=flutter (this may take a while)
[2024-11-22T01:06:07.336Z INFO frb_codegen/src/library/integration/integrator.rs:76] Setup cargokit dependencies
[2024-11-22T01:06:07.336Z INFO frb_codegen/src/library/commands/flutter.rs:53] Execute `flutter pub get` inside "/Users/tom/temp/hi_app/cargokit/build_tool" (this may take a while)
[2024-11-22T01:06:14.667Z INFO frb_codegen/src/library/integration/integrator.rs:79] Apply Dart fixes
[2024-11-22T01:06:24.960Z INFO frb_codegen/src/library/integration/integrator.rs:82] Format Dart code
(base) ➜  temp cd hi_app

Analyze it:

➜  hi_app dart analyze
Analyzing hi_app...                    6.2s
No issues found!

Run it: flutter run -d macos

image

fzyzcjy commented 7 hours ago

I still guess it may be because your dart runtime version does not match codegen version...