Running a third-party function from src-tauri/srcmain.rs that spawns a call to an external command-line tool through an env var causes the Tauri app to …
spawn a (fully functional) clone of itself
stalls the execution of the external command until the spawned Tauri app is closed
Both of these are undesirable for obvious reasons.
The third-party function in this case is ProjectWorkspace.run_build_scripts(…) (docs) from rust-analyzer's ra_ap_project_model crate (docs) and the external command-line tool is cargo check with the env var being RUSTC_WRAPPER (cargo book).
The ProjectWorkspace.run_build_scripts(…) function works just fine outside of Tauri.
Reproduction
Create a barebones project:
yarn create tauri-app:
yarn create v1.22.21
...
success Installed "create-tauri-app@3.11.7" with binaries:
- create-tauri-app
✔ Project name · tauri-repro
✔ Choose which language to use for your frontend · TypeScript / JavaScript - (pnpm, yarn, npm, bun)
✔ Choose your package manager · yarn
✔ Choose your UI template · Vanilla
✔ Choose your UI flavor · TypeScript
Change the value of const WRAP_RUSTC_IN_BUILD_SCRIPTS: bool in main.rs to true.
Open your IDE's console and re-launch the tauri app.
Notice how the program execution seemingly stalls and how a second app instance has spawned all by itself, right on top of the original one, with the console's tail looking something like this:
[… DEBUG tauri_repro] 🟡 Running build scripts ...
[… DEBUG tauri_repro] 🔴 Execution stalled, waiting for clone app to be closed.
Close the clone app instance and notice how the original app's execution continues, and the UI shows "It worked!", with the console finally showing the following:
Changing WRAP_RUSTC_IN_BUILD_SCRIPTS from false to true causes rust-analyzer (the library, aka the ra_ap_… dependencies we're linking) to wrap its invocation of cargo check through the RUSTC_WRAPPER env var.
RUSTC_WRAPPER — Instead of simply running rustc, Cargo will execute this specified wrapper, passing as its command-line arguments the rustc invocation, with the first argument being the path to the actual rustc. Useful to set up a build cache tool such as sccache. See build.rustc-wrapper to set via config. Setting this to the empty string overwrites the config and resets cargo to not use a wrapper.
My initial suspicion was that I was missing a call to let _ = fix_path_env::fix(); to make this work on macOS, but as evident by the reproduction above the problem persists, even with the env fix.
Describe the bug
Running a third-party function from
src-tauri/srcmain.rs
that spawns a call to an external command-line tool through an env var causes the Tauri app to …Both of these are undesirable for obvious reasons.
The third-party function in this case is
ProjectWorkspace.run_build_scripts(…)
(docs) fromrust-analyzer
'sra_ap_project_model
crate (docs) and the external command-line tool iscargo check
with the env var beingRUSTC_WRAPPER
(cargo book).The
ProjectWorkspace.run_build_scripts(…)
function works just fine outside of Tauri.Reproduction
Create a barebones project:
yarn create tauri-app
:Then add the following dependencies:
And modify the
src-tauri/src/main.rs
file to look like this:Create a dummy Rust project (e.g. via
cargo new "tauri-dummy"
in a separate shell) somewhere else on your machine.Run the tauri app via
yarn tauri dev
/npm run tauri dev
.Pass the dummy project's manifest path (e.g.
/path/to/tauri-dummy/Cargo.toml
) into the greet input field and click "Greet".The tauri web UI should show "It worked!" as the greeting and the logs should look something like this:
Change the value of
const WRAP_RUSTC_IN_BUILD_SCRIPTS: bool
inmain.rs
totrue
.Open your IDE's console and re-launch the tauri app.
Notice how the program execution seemingly stalls and how a second app instance has spawned all by itself, right on top of the original one, with the console's tail looking something like this:
Close the clone app instance and notice how the original app's execution continues, and the UI shows "It worked!", with the console finally showing the following:
Expected behavior
The app should neither …
Full
tauri info
outputStack trace
Additional context
Changing
WRAP_RUSTC_IN_BUILD_SCRIPTS
fromfalse
totrue
causesrust-analyzer
(the library, aka thera_ap_…
dependencies we're linking) to wrap its invocation ofcargo check
through theRUSTC_WRAPPER
env var.My initial suspicion was that I was missing a call to
let _ = fix_path_env::fix();
to make this work on macOS, but as evident by the reproduction above the problem persists, even with the env fix.