1

I’m trying to create a Tauri app by moving some commands into lib.rs.

lib.rs

#[tauri::command]
pub async fn get_file_path_in_rust(file_path: String) {
    println!("The Path Received: {}", file_path);
}

main.rs

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri_plugin_opener::OpenerExt;
use whoami;
use crate::get_file_path_in_rust;

#[tauri::command]
fn get_user() -> String {
    whoami::username()
}

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_opener::init())
        .invoke_handler(tauri::generate_handler![
            get_user,
            get_file_path_in_rust
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri app");
}

But when I run it, I get this error:

the name `__cmd__get_file_path_in_rust` is defined multiple times
`__cmd__get_file_path_in_rust` must be defined only once in the macro namespace of this module

I only have the function once, so I don’t understand why the macro is being generated twice.

Question:

What’s the correct way to put #[tauri::command] functions in lib.rs and call them from main.rs?

How do I avoid this duplicate macro error?

2
  • Can you try making get_file_path_in_rust pub(crate) or module-private and see if that fixes it? Commented Sep 18 at 17:25
  • Why do you even have a lib.rs? It only makes sense if you have multiple binaries that need to use it, either as multiple binaries in your crate (but in that case you probably wouldn't have a main.rs) or from other crates (is that the case?) Commented Sep 19 at 6:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.