tauri-apps / tauri

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

[bug] cargo tauri dev command doesn't respect .cargo/config.toml [env] section settings #8169

Open templateK opened 10 months ago

templateK commented 10 months ago

Describe the bug

If you set environment variable through cargo config.toml file, like below, cargo tauri dev doesn't add it to the target app.

.cargo/config.toml

[env]
FOO = "bar"

Reproduction

  1. preprare barebone tauri-app

    cargo install create-tauri-app --locked
    cargo create-tauri-app
  2. edit tauri-app/src-tauri/src/main.rs

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

[tauri::command]

fn greet(name: &str) -> String { format!("Hello, {}! You've been greeted from Rust!", name) }

fn main() {

match std::env::var("FOO") {
    Ok(v) => println!("FOO={v}"),
    Err(e) => println!("Error: {:?}",e),
}

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

}


3. edit **.cargo/config.toml**
```toml
[env]
FOO = "bar"
  1. run cargo tauri dev

/r/tauri-app> cargo tauri dev Info Watching tauri-app/src-tauri for changes... Compiling tauri-app v0.0.0 (tauri-app/src-tauri) Finished dev [unoptimized + debuginfo] target(s) in 2.55s Error: NotPresent

Expected behavior

Running command cargo tauri dev should print below.

/r/tauri-app> cargo tauri dev Info Watching tauri-app/src-tauri for changes... Compiling tauri-app v0.0.0 (tauri-app/src-tauri) Finished dev [unoptimized + debuginfo] target(s) in 2.55s FOO=bar

Platform and versions

[✔] Environment
    - OS: Mac OS 10.14.5 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.70.0 (90c541806 2023-05-31)
    ✔ cargo: 1.70.0 (ec8a8a0ca 2023-04-25)
    ✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔ Rust toolchain: stable-x86_64-apple-darwin (environment override by RUSTUP_TOOLCHAIN)
    - node: 20.5.0
    - yarn: 1.22.11
    - npm: 9.8.0

[-] Packages
    - tauri [RUST]: 1.5.2
    - tauri-build [RUST]: 1.5.0
    - wry [RUST]: 0.24.4
    - tao [RUST]: 0.16.5
    - tauri-cli [RUST]: 1.5.6
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli [NPM]: 1.5.6

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../src
    - devPath: ../src

Stack trace

No response

Additional context

If we run tauri-app by running command cd src-tauri; cargo run, the app correctly prints the environment variable set by .cargo/config.toml.

FabianLars commented 10 months ago

Similar-ish to this one https://github.com/tauri-apps/tauri/issues/4724#issuecomment-1548974637 (Only commenting this so we fix both at the same time).

p.s. thanks for the report. and using cargo run in the meantime is fine btw, for simple apps the only thing you'll loose is the beforeDevCommand which you can just run in a seperate terminal yourself. What i mean is that there shouldn't be any noteworthy runtime consequences (again, for simple apps, once you add sidecars and resources etc it can get tricky).