tauri-apps / tauri

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

[bug] If an underscore “_” is used for an argument, the function will not work. #10684

Open Masa-Ryu opened 4 weeks ago

Masa-Ryu commented 4 weeks ago

Describe the bug

If an underscore is used in the variable name of the argument on the React side, the function on the Rust side will not be called and no error message will be displayed in dev mode.

Reproduction

App.tsx is here

import { useState } from "react";
import { invoke } from "@tauri-apps/api/tauri";
import "./App.css";

function App() {
  const [result, setResult] = useState("");

  async function handleProcess() {
    setResult(await invoke("greet", {
      msg_: "World"
    }));
  }

  return (
    <div>
      <button onClick={() => handleProcess()}>Print</button>
      <p>{result}</p>
    </div>
  );
}

export default App;

main.rs is here

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

#[tauri::command]
fn greet(msg_: String) -> String {
    format!("Hello {} from Tauri!", msg_)
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

It is worth noting that when executing the Rust-side greets function in app.tsx, the argument msg_ is used as input. If the argument name contains underscores, the Rust-side greet function will not be executed. This is the bug.

Expected behavior

It works even if underscores are included.

Full tauri info output

npm run tauri info

> find_vanity_address@0.1.0 tauri
> tauri info

[✔] Environment
    - OS: Mac OS 13.6.3 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.80.1 (3f5fd8dd4 2024-08-06) (Homebrew)
    ✔ cargo: 1.80.1
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: 
    - node: 18.17.0
    - yarn: 1.22.22
    - npm: 9.6.7

[-] Packages
    - tauri [RUST]: 1.7.1
    - tauri-build [RUST]: 1.5.3
    - wry [RUST]: 0.24.10
    - tao [RUST]: 0.16.9
    - @tauri-apps/api [NPM]: 1.6.0
    - @tauri-apps/cli [NPM]: 1.6.0

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:1420/
    - framework: React
    - bundler: Vite

Stack trace

No response

Additional context

No response

FabianLars commented 4 weeks ago

argument names are automatically renamed, some_arg becomes someArg and i guess some_ is a bit of undefined behavior but i would assume it gets converted to some. You can disable the renaming by changing #[tauri::command] to #[tauri::command(rename_all = "snake_case")].

I will leave this issue open for now so the team can check how it's supposed to behave on arg names ending with an underscore.

Masa-Ryu commented 3 weeks ago

I did not know that it was automatically converted. However, I too would like to know how it behaves when underscored.