fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.

Home Page:https://fzyzcjy.github.io/flutter_rust_bridge/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Are Cargo Workspaces supported?

rhian-cs opened this issue · comments

Hi!

I wanted to add a Flutter front-end to my existing Rust app (https://github.com/rhian-cs/rust-hash-calculator), but it contains 2 members in a single Cargo Workspace (core and cli).

I tried changing the Rust Output path and some other configuration but I couldn't get it to work. Is there a way to set up flutter_rust_bridge to use a single member in the workspace? Or perhaps are there any changes I could do to the project while still supporting a separate, smaller CLI crate?

Thanks,
Rhian

Hi! Thanks for opening your first issue here! 😄

Sure, it should work! It would be great to provide the error do you see, such that I can check what is going on.

You may be interested in https://cjycode.com/flutter_rust_bridge/guides/custom/codegen#flutter_rust_bridge_codegen-create flutter_rust_bridge_codegen create --rust-crate-dir ...

Hey! Thank you for your quick response. While I was trying to come up with a reproducible example I got something that kind of worked, I'll attach it (attempt 1 and 2) at the end of the comment.

The --rust-crate-dir option worked for me, but I had to do some extra steps, I'll share them here.

Attempt 3 (Successful)

cargo binstall 'flutter_rust_bridge_codegen@^2.0.0-dev.0'

# I ran the command below at the root of my Cargo Workspace

# Apparently the `../flutter_bridge` path is relative to the generated flutter project directory,
# not the current $PWD when running the command (absolute paths don't seem to work either)
flutter_rust_bridge_codegen create gui_flutter --rust-crate-dir ../flutter_bridge

After I removed the Cargo.lock file from the generated Rust project:

rm flutter_bridge/Cargo.lock

I edited the Workspace configuration so that the flutter_bridge Rust project is included as a member.

Cargo.toml:

-members = ["core", "cli", "gui"]
+members = ["core", "cli", "gui", "flutter_bridge"]

Finally:

cd gui_flutter/
flutter run

The example project worked fine, but I'll have to experiment with the library, I'll let you know in this thread if I run into any issues.

Do you think it would be relevant to add a section about Cargo Workspaces in the documentation? I can open a PR if you think that would be interesting.

Here are my other attempts, for completeness sake:

Attempt 1 (Partially Successful)

I ran the following commands:

cargo binstall 'flutter_rust_bridge_codegen@^2.0.0-dev.0'
flutter_rust_bridge_codegen create gui_flutter
cd gui_flutter

When I run flutter run, it shows the following error:

Launching lib/main.dart on Linux in debug mode...
SEVERE: error: current package believes it's in a workspace when it's not:
Building Linux application...
Error: Build process failed

Assuming it was an issue with Cargo Workspaces, I removed the Cargo.lock file.

rm rust/Cargo.lock

I then changed Cargo.toml at the project root:

-members = ["core", "cli", "gui"]
+members = ["core", "cli", "gui", "gui_flutter/rust"]

When I run flutter run again it works. However, it's odd to have a Rust project inside a Flutter project, that is also a member of the workspace. It would be nice to have, say, a flutter_bridge project at the root of the workspace. This leads to attempt 2.

Attempt 2 (Unsuccessful)

After doing the same steps as attempt 1, I moved the Rust project to the workspace root.

mv rust/ ../flutter_bridge

I changed the flutter_rust_bridge.yaml file to point to that directory:

rust_input: ../flutter_bridge/src/api/**/*.rs
dart_output: lib/src/rust

I changed the Cargo.toml file at the workspace root again:

-members = ["core", "cli", "gui", "gui_flutter/rust"]
+members = ["core", "cli", "gui", "flutter_bridge"]

Then in my Flutter project, I tried running flutter run, but it failed:

Launching lib/main.dart on Linux in debug mode...
SEVERE: PathNotFoundException: Cannot open file, path = '/home/rhian/Dev/Rust/rust-hash-calculator/gui_flutter/linux/flutter/ephemeral/.plugin_symlinks/rust_lib_gui_flutter/linux/../../rust/Cargo.toml' (OS Error: No such file or directory, errno = 2)
Building Linux application...                                           
Error: Build process failed

Happy to hear it works!

Do you think it would be relevant to add a section about Cargo Workspaces in the documentation? I can open a PR if you think that would be interesting.

Sure, feel free to PR :) One way may be put in https://cjycode.com/flutter_rust_bridge/guides/how-to folder.