diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index de77aa71..6c8ee8b4 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2128,6 +2128,7 @@ dependencies = [ name = "kromacut" version = "4.2.0" dependencies = [ + "embed-resource", "gio", "log", "percent-encoding", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 3f3555fe..b10313cd 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -16,6 +16,7 @@ crate-type = ["staticlib", "cdylib", "rlib"] [build-dependencies] tauri-build = { version = "2.6.0", features = [] } +embed-resource = "3" [dependencies] serde_json = "1.0" diff --git a/src-tauri/build.rs b/src-tauri/build.rs index d860e1e6..c083ea5f 100644 --- a/src-tauri/build.rs +++ b/src-tauri/build.rs @@ -1,3 +1,34 @@ fn main() { - tauri_build::build() + let mut attributes = tauri_build::Attributes::new(); + // tauri-build links its Windows app manifest into binaries only, but the unit test executable + // links the same Tauri and dialog code. Without the manifest's Common Controls v6 dependency it + // fails to start with STATUS_ENTRYPOINT_NOT_FOUND. So embed the manifest ourselves, in every + // target. + let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); + let target_env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default(); + if target_os == "windows" && target_env == "msvc" { + attributes = attributes + .windows_attributes(tauri_build::WindowsAttributes::new_without_app_manifest()); + embed_manifest_in_every_target(); + } + tauri_build::try_build(attributes).expect("failed to run tauri-build"); +} + +/// Writes tauri-build's default manifest into a resource script in tauri-winres' format, so the +/// app's embedded manifest bytes do not change, then links it into tests and libraries as well as +/// binaries. `windows/app-manifest.xml` is a verbatim copy of tauri-build 2.6.3's +/// `windows-app-manifest.xml`; re-diff it when upgrading tauri-build. Only `"` is escaped, the +/// one character tauri-winres escapes that the manifest contains. +fn embed_manifest_in_every_target() { + let manifest = include_str!("windows/app-manifest.xml"); + let mut script = String::from("#pragma code_page(65001)\n1 24\n{\n"); + for line in manifest.lines() { + script.push_str(&format!("\" {} \"\n", line.trim().replace('"', "\"\""))); + } + script.push_str("}\n"); + let path = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("app-manifest.rc"); + std::fs::write(&path, script).expect("failed to write the manifest resource script"); + embed_resource::compile_for_everything(&path, embed_resource::NONE) + .manifest_required() + .expect("failed to compile the manifest resource"); } diff --git a/src-tauri/src/safe_save.rs b/src-tauri/src/safe_save.rs index 5e9ac333..cfcffab4 100644 --- a/src-tauri/src/safe_save.rs +++ b/src-tauri/src/safe_save.rs @@ -87,8 +87,16 @@ fn commit_temp(temp: &Path, destination: &Path) -> Result<(), String> { /// Whether the path is in the app's local data folder, which holds the webview data that the fs /// plugin denies the webview by default. fn is_in_app_local_data(app: &AppHandle, path: &Path) -> bool { - let canonical = - |path: &Path| std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()); + // Canonicalize the deepest folder that exists, so both sides compare in the same form even + // when one is missing (on Windows, canonical paths gain a `\\?\` prefix). + let canonical = |path: &Path| { + path.ancestors() + .find_map(|ancestor| { + let rest = path.strip_prefix(ancestor).ok()?; + Some(std::fs::canonicalize(ancestor).ok()?.join(rest)) + }) + .unwrap_or_else(|| path.to_path_buf()) + }; match (app.path().app_local_data_dir(), path.parent()) { (Ok(data), Some(directory)) => canonical(directory).starts_with(canonical(&data)), _ => false, diff --git a/src-tauri/windows/app-manifest.xml b/src-tauri/windows/app-manifest.xml new file mode 100644 index 00000000..2d510ed3 --- /dev/null +++ b/src-tauri/windows/app-manifest.xml @@ -0,0 +1,14 @@ + + + + + + +