tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
81.59k stars 2.44k forks source link

[feat] Allow using #[command] across workspaces #9362

Open AMythicDev opened 5 months ago

AMythicDev commented 5 months ago

Describe the problem

It seems that right now I can only call the #[tauri::command] on the root src/ folder of my project. Due to the size and complexity of the project that I am working on, I need to separate parts of the codebase into Cargo workspaces. I want to be able to call certain functions in these workspaces from the frontend so I have added these workspace as a dependency of the root package which is responsible for initialising my main app. However when I declare a function inside a workspace as a command throws an error like this:

error[E0255]: the name `__cmd__get_details` is defined multiple times
  --> config/src/lib.rs:62:8
   |
61 | #[tauri::command]
   | ----------------- previous definition of the macro `__cmd__get_details` here
62 | pub fn get_details() -> Result<UserConfig> {
   |        ^^^^^^^^^^^ `__cmd__get_details` reimported here
   |
   = note: `__cmd__get_details` must be defined only once in the macro namespace of this module

Describe the solution you'd like

I would like to be able to declare functions in workspaces as tauri commands just as easily as I can do it in my root package.

Alternatives considered

My current approach is to make wrappers for all the functions in my root package and declare then declare these wrappers as tauri commands. This is terrible in my eyes as it is very error-prone, adds a useless function call and requires me to use weird gimmicks to properly bubble up the errors in the wrapper function.

Additional context

Tauri version: v1.6.1 OS: Fedora Linux 39 webkitgtk6.0 version: 2.42.1

FabianLars commented 5 months ago

Would it be possible to turn those crates into tauri plugins? They have their own invoke_handler call which would work around this issue.

FabianLars commented 5 months ago

Actually i just remembered a discord discussion and it's actually possible. In that discussion the solution was to change this

use my_library::commands::greet;

.invoke_handler(generate_handler![greet])

to this

use my_library::commands;

.invoke_handler(generate_handler![commands::greet])
AMythicDev commented 5 months ago

I have written the same in my code but I don't have an inner module like command so essentially what I have is like this:

.invoke_handler(generate_handler![config::get_details])

And this too gave the same error message.

FabianLars commented 5 months ago

Ahh right, you can trigger that same error in your tauri-app's main.rs/lib.rs file by having the command pub. I did not find a solution to this on our side (though i am not good with macros at all) so at least for now you must move them into a submodule (inside that seperate crate).

AMythicDev commented 5 months ago

so at least for now you must move them into a submodule (inside that seperate crate).

From what I understand from this is that I should move all the functions into a commands module. Am I getting that right?

FabianLars commented 5 months ago

Yes. The exact name of the module is up to you, but commands is popular.

AMythicDev commented 5 months ago

Thanks @FabianLars, that worked. However I am still keeping this open because for the tauri team to look into.