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

Native Linux build can not find shared library when using Cargo workspaces #2460

Open
konsti219 opened this issue Dec 19, 2024 · 4 comments
Open
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc bug Something isn't working

Comments

@konsti219
Copy link

Describe the bug

When using cargo workspaces the built Linux application crashes on startup because it is unable to locate the .so built from the bridge Rust crate. I followed the Cargo workspace How-to.

The Android build with workspaces and the Linux build without workspaces both work fine on my system.

Changing ioDirectory: 'rust/target/release/', to ioDirectory: '../target/release/', in frb_generated.dart can fix the issue, but I don't want to modify the generated code.

Steps to reproduce

  1. mkdir workspace_test && cd workspace_test
  2. echo [workspace]\nmembers = ["my_flutter_bridge"] > Cargo.toml
  3. flutter_rust_bridge_codegen create my_flutter_project --rust-crate-dir ../my_flutter_bridge How-to
  4. cargo build --release
  5. cd my_flutter_project
  6. flutter run

Logs

Launching lib/main.dart on Linux in debug mode...
Building Linux application...                                           
✓ Built build/linux/x64/debug/bundle/my_flutter_project
Gtk-Message: 17:05:21.111: Failed to load module "appmenu-gtk-module": 'gtk_module_display_init': /run/current-system/sw/lib/gtk-3.0/modules/libcolorreload-gtk-module.so: undefined symbol: gtk_module_display_init

(my_flutter_project:74359): Atk-CRITICAL **: 17:05:21.251: atk_socket_embed: assertion 'plug_id != NULL' failed
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library 'librust_lib_my_flutter_project.so': librust_lib_my_flutter_project.so: cannot open shared object file: No such file or directory
#0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:43)
#1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:22:12)
#2      new ExternalLibrary.open (package:flutter_rust_bridge/src/platform_types/_io.dart:42:47)
#3      loadExternalLibraryRaw.<anonymous closure> (package:flutter_rust_bridge/src/loader/_io.dart:81:46)
#4      loadExternalLibraryRaw.tryAssumingNonPackaged (package:flutter_rust_bridge/src/loader/_io.dart:49:22)
#5      loadExternalLibraryRaw (package:flutter_rust_bridge/src/loader/_io.dart:80:12)
#6      loadExternalLibrary (package:flutter_rust_bridge/src/loader/_io.dart:14:10)
#7      BaseEntrypoint._loadDefaultExternalLibrary (package:flutter_rust_bridge/src/main_components/entrypoint.dart:141:13)
#8      BaseEntrypoint.initImpl (package:flutter_rust_bridge/src/main_components/entrypoint.dart:48:31)
#9      RustLib.init (package:my_flutter_project/src/rust/frb_generated.dart:27:20)
#10     main (package:my_flutter_project/main.dart:6:17)
#11     _runMain.<anonymous closure> (dart:ui/hooks.dart:301:23)
#12     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#13     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

Expected behavior

No response

Generated binding code

No response

OS

NixOS

Version of flutter_rust_bridge_codegen

2.7.0

Flutter info

[✓] Flutter (Channel stable, 3.24.4, on NixOS 24.11 (Vicuna) 6.12.5, locale en_US.UTF-8)
    • Flutter version 3.24.4 on channel stable at /nix/store/8d1ajjrgvwf4ph93gbcywh4v3dlg0gwr-flutter-wrapped-3.24.4-sdk-links
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision nixpkgs000 (), 1970-01-01 00:00:00
    • Engine revision db49896cf2
    • Dart version 3.5.4
    • DevTools version 2.37.3

[...Android]

[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Linux toolchain - develop for Linux desktop
    • clang version 18.1.8
    • cmake version 3.30.5
    • ninja version 1.12.1
    • pkg-config version 0.29.2

[...Android Studio]

[✓] Connected device (1 available)
    • Linux (desktop) • linux • linux-x64 • NixOS 24.11 (Vicuna) 6.12.5

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

Version of clang++

No response

Additional context

No response

@konsti219 konsti219 added the bug Something isn't working label Dec 19, 2024
Copy link

welcome bot commented Dec 19, 2024

Hi! Thanks for opening your first issue here! 😄

@fzyzcjy
Copy link
Owner

fzyzcjy commented Dec 19, 2024

Hi, maybe try to use https://cjycode.com/flutter_rust_bridge/guides/how-to/load-library and customize the path of the library

@fzyzcjy fzyzcjy added the awaiting Waiting for responses, PR, further discussions, upstream release, etc label Dec 19, 2024
@konsti219
Copy link
Author

Using this method I wrote this fix I am happy with.

import 'dart:io' show Platform;
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated_io.dart';

var config = ExternalLibraryLoaderConfig(
  stem: 'my_flutter_bridge',
  ioDirectory: !Platform.isLinux ? 'rust/target/release/' : '../target/release/',
  webPrefix: 'pkg/',
);
await RustLib.init(externalLibrary: await loadExternalLibrary(config));

However, I would prefer a proper fix on the build system level. Or at least a mention in the docs for this problem. But the priority of these things is up to you.

I am going to keep the issue open in case you do want to address this, otherwise it can be closed.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Dec 21, 2024

However, I would prefer a proper fix on the build system level. Or at least a mention in the docs for this problem. But the priority of these things is up to you.

That looks reasonable! The most complete solution may be checking where is cargo workspaces and find out the target directory. But since this may not be of top priority, I guess the second way - mentioning in doc - is great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants