From 6694c8ea17c5f3b500e13a1facebfbbd5b2d8c95 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Thu, 5 Nov 2026 09:05:19 +0800 Subject: [PATCH] fix(dev): rewrite src/bindings.ts only when the bindings change Every launch of the debug binary exports the TypeScript bindings, including a second instance that only hands its path to the running one. Writing an unchanged file still made Vite reload the running app's windows, so opening a file from outside in a dev build reloaded the window, which lost a LightEdit session and reopened the last project. The bindings are now exported to a temporary file and src/bindings.ts is replaced only when its content differs; `pnpm gen:bindings` behaves as before. --- src-tauri/src/lib.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 3e889ed7..76acd0b6 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -28,7 +28,7 @@ mod windows; use parking_lot::Mutex; use state::AppState; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::Arc; use tauri::{Emitter, Manager}; use windows::WindowsState; @@ -40,12 +40,21 @@ pub fn specta_builder() -> tauri_specta::Builder { } pub fn export_bindings(builder: &tauri_specta::Builder) { + let target = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/bindings.ts")); + // Rewritten only when it changes: every debug launch exports, also a second instance that just + // hands its path over, and a rewrite makes Vite reload the running app's windows. + let fresh = std::env::temp_dir().join(format!("openidea-bindings-{}.ts", std::process::id())); builder .export( specta_typescript::Typescript::default().header("// @ts-nocheck\n/* eslint-disable */\n// Generated by tauri-specta. Do not edit; run `pnpm gen:bindings`."), - concat!(env!("CARGO_MANIFEST_DIR"), "/../src/bindings.ts"), + &fresh, ) .expect("failed to export TypeScript bindings"); + let text = std::fs::read(&fresh).expect("exported TypeScript bindings"); + let _ = std::fs::remove_file(&fresh); + if std::fs::read(target).ok().as_deref() != Some(text.as_slice()) { + std::fs::write(target, text).expect("failed to write TypeScript bindings"); + } } #[cfg_attr(mobile, tauri::mobile_entry_point)]