Files
qbz/src-tauri/build.rs
T
vicrodh 014af6fbfb fix(build): stop infinite tauri dev rebuild loop on auto-updater.json
The build script unconditionally rewrote capabilities/auto-updater.json
on every run, which bumped the file's mtime. tauri-build emits
cargo:rerun-if-changed=capabilities so any touch in that directory
re-runs this script, and 'tauri dev' also watches that folder for
source reloads. The two watchers fed each other: compile → rewrite
(same bytes, fresh mtime) → cargo marks build.rs dirty → recompile
→ tauri dev sees mtime change → request another build → repeat.

Now we read the existing file first and only write when the content
actually differs. mtime stays stable when the generated text is
identical across runs, so neither cargo nor tauri dev observe a
change and the loop terminates.

cc
2026-04-13 20:27:44 -06:00

48 lines
1.8 KiB
Rust

use std::fs;
use std::path::Path;
/// Auto-updater capability, emitted to capabilities/ only when the `updater`
/// Cargo feature is active. Gitignored so Flatpak/Snap builds (which disable
/// the feature) never reference permissions for plugins that aren't linked.
const AUTO_UPDATER_CAPABILITY: &str = r#"{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "auto-updater",
"description": "Permissions for the in-app auto-updater (macOS + Linux AppImage). Generated by build.rs when the `updater` Cargo feature is enabled.",
"windows": ["main", "miniplayer"],
"permissions": [
"updater:default",
"process:allow-restart"
]
}
"#;
fn main() {
let capability_path = Path::new("capabilities/auto-updater.json");
if cfg!(feature = "updater") {
// IMPORTANT: only write if contents actually changed. tauri-build emits
// `cargo:rerun-if-changed=capabilities` which makes cargo re-run this
// build script whenever anything in that directory is touched. If we
// unconditionally `fs::write` we update the file's mtime every run,
// which re-triggers the build script AND makes `tauri dev`'s file
// watcher rebuild the app in an infinite loop.
let needs_write = match fs::read_to_string(capability_path) {
Ok(existing) => existing != AUTO_UPDATER_CAPABILITY,
Err(_) => true,
};
if needs_write {
if let Err(e) = fs::write(capability_path, AUTO_UPDATER_CAPABILITY) {
panic!("Failed to emit auto-updater capability: {e}");
}
}
} else if capability_path.exists() {
if let Err(e) = fs::remove_file(capability_path) {
panic!("Failed to remove stale auto-updater capability: {e}");
}
}
println!("cargo:rerun-if-changed=build.rs");
tauri_build::build()
}