fzyzcjy/flutter_rust_bridge

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

Closed this issue · 28 comments

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);
…
  1. 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

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!

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

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,
    );
  }

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)?

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,
    );
  }

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.

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

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!

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!

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!

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);
  }

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

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

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

Here is a test repository

  1. cargo install --version 2.6.0 flutter_rust_bridge_codegen

  2. flutter_rust_bridge_codegen --version

flutter_rust_bridge_codegen 2.6.0
  1. flutter_rust_bridge_codegen create frb_bug --template plugin

  2. cd frb_bug

  3. pubspec.yaml

flutter_rust_bridge: 2.6.0
  1. dart analyze lib/
  error • src/rust/frb_generated.dart:39:14 • The method 'initMockImpl' isn't defined for
          the type 'RustLib'. Try correcting the name to the name of an existing method,
          or defining a method named 'initMockImpl'. • undefined_method

1 issue found.
  1. cd example
  2. flutter run -d macos
--- xcodebuild: WARNING: Using the first of multiple matching destinations:
{ platform:macOS, arch:arm64, id:00008112-0004119A3E2A201E, name:My Mac }
{ platform:macOS, arch:x86_64, id:00008112-0004119A3E2A201E, name:My Mac }
../lib/src/rust/frb_generated.dart:39:14: Error: The method 'initMockImpl' isn't defined for the class 'RustLib'.
 - 'RustLib' is from 'package:frb_bug/src/rust/frb_generated.dart' ('../lib/src/rust/frb_generated.dart').
Try correcting the name to the name of an existing method, or defining a method named 'initMockImpl'.
    instance.initMockImpl(
             ^^^^^^^^^^^^
Target kernel_snapshot_program failed: Exception

Command PhaseScriptExecution failed with a nonzero exit code
warning: Run script build phase 'Run Script' will be run during every build because it does not specify any outputs. To address this issue, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase. (in target 'Flutter Assemble' from project 'Runner')
** BUILD FAILED **

flutter doctor -v

[✓] 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 (9 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 (3 available)
    • 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.

• No issues found!

Weird... I will check this soon.

I git cloned the repo, modify pubspec.yaml to use lower min dart version, run flutter pub get, then

 dart analyze lib
Analyzing lib...                       1.6s
No issues found!

I will upgrade my local flutter and retry to reproduce.

Well, still cannot reproduce it...

flutter version

flutter --version
Flutter 3.24.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision dec2ee5c1f (9 days ago) • 2024-11-13 11:13:06 -0800
Engine • revision a18df97ca5
Tools • Dart 3.5.4 • DevTools 2.37.3

clone code

git clone https://github.com/ethicnology/frb_bug
Cloning into 'frb_bug'...
remote: Enumerating objects: 258, done.
remote: Counting objects: 100% (258/258), done.
remote: Compressing objects: 100% (180/180), done.
remote: Total 258 (delta 33), reused 258 (delta 33), pack-reused 0 (from 0)
Receiving objects: 100% (258/258), 283.47 KiB | 284.00 KiB/s, done.
Resolving deltas: 100% (33/33), done.

pub get

flutter pub get
Downloading Material fonts...                                    2,069ms
Downloading Gradle Wrapper...                                    1,028ms
Downloading package sky_engine...                                1,759ms
Downloading package flutter_gpu...                               1,024ms
Downloading flutter_patched_sdk tools...                         2,124ms
Downloading flutter_patched_sdk_product tools...                 2,033ms
Downloading darwin-x64 tools...                                     3.9s
Downloading libimobiledevice...                                  1,148ms
Downloading usbmuxd...                                             951ms
Downloading libplist...                                            844ms
Downloading openssl...                                           1,700ms
Downloading ios-deploy...                                        1,251ms
Downloading darwin-x64/font-subset tools...                      2,524ms
Resolving dependencies... (4.3s)
Downloading packages... 
+ args 2.6.0
+ async 2.11.0 (2.12.0 available)
+ boolean_selector 2.1.1 (2.1.2 available)
+ build_cli_annotations 2.1.0
+ characters 1.3.0 (1.3.1 available)
+ cli_util 0.4.2
+ clock 1.1.1 (1.1.2 available)
+ collection 1.18.0 (1.19.1 available)
+ fake_async 1.3.1 (1.3.2 available)
+ ffi 2.1.3
+ ffigen 11.0.0 (16.0.0 available)
+ file 7.0.0 (7.0.1 available)
+ flutter 0.0.0 from sdk flutter
+ flutter_driver 0.0.0 from sdk flutter
+ flutter_lints 4.0.0 (5.0.0 available)
+ flutter_rust_bridge 2.6.0
+ flutter_test 0.0.0 from sdk flutter
+ fuchsia_remote_debug_protocol 0.0.0 from sdk flutter
+ glob 2.1.2
+ integration_test 0.0.0 from sdk flutter
+ leak_tracker 10.0.5 (10.0.8 available)
+ leak_tracker_flutter_testing 3.0.5 (3.0.9 available)
+ leak_tracker_testing 3.0.1
+ lints 4.0.0 (5.1.0 available)
+ logging 1.3.0
+ matcher 0.12.16+1 (0.12.17 available)
+ material_color_utilities 0.11.1 (0.12.0 available)
+ meta 1.15.0 (1.16.0 available)
+ package_config 2.1.0
+ path 1.9.0 (1.9.1 available)
+ platform 3.1.5 (3.1.6 available)
+ plugin_platform_interface 2.1.8
+ process 5.0.2 (5.0.3 available)
+ quiver 3.2.2
+ sky_engine 0.0.99 from sdk flutter
+ source_span 1.10.0
+ stack_trace 1.11.1 (1.12.0 available)
+ stream_channel 2.1.2
+ string_scanner 1.2.0 (1.4.0 available)
+ sync_http 0.3.1
+ term_glyph 1.2.1
+ test_api 0.7.2 (0.7.3 available)
+ vector_math 2.1.4
+ vm_service 14.2.5 (14.3.1 available)
+ web 1.1.0
+ webdriver 3.0.3 (3.0.4 available)
+ yaml 3.1.2
+ yaml_edit 2.2.1
Changed 48 dependencies!
23 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Resolving dependencies in `./example`... 
Downloading packages... 
Got dependencies in `./example`.

analyze

dart analyze lib
Analyzing lib...                       3.5s
No issues found!

🤯

If you don't have a dev or updated version of flutter_rust_bridge_codegen, I don't see what it can be…

I switched to 2.5.0 and repeated the same experience without any issue

If you don't have a dev or updated version of flutter_rust_bridge_codegen, I don't see what it can be…

Indeed I do not do anything related to flutter_rust_bridge_codegen - I just clone your repo and run flutter commands (without any frb commands)...

So could you please also try that? i.e. clone and run flutter commands

I tried, and I still error:

  1. git clone git@github.com:ethicnology/frb_bug.git
  2. flutter pub get
  3. dart analyze lib
Analyzing lib...                       0.9s

  error • src/rust/frb_generated.dart:39:14 • The method 'initMockImpl'
          isn't defined for the type 'RustLib'. Try correcting the name to
          the name of an existing method, or defining a method named
          'initMockImpl'. • undefined_method

1 issue found.

Is it possible that your locally cached version of flutter_rust_bridge runtime is somehow corrupted? Maybe clear the cache (and ensure you remove those files), and retry.

Both my machine and the various CIs (which is always run for each commit of frb) pass, so I guess it may not very likely to be a bug in frb.

Do you think it's the flutter or cargo cache, maybe both ?

flutter cache i guess, because it is the dart runtime package being weird

flutter clean correctly deleted .dart_tool.

Solving the issue:

  1. rm -rf ~/.pub-cache/hosted/pub.dev/*
  2. cd <flutter_project_path> && flutter pub get

pub-cache corrupted, good catch, thank you

Happy to see it is solved, you are welcome!

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.