From 721278da57603cd3f8c3317487a877bfa7113f77 Mon Sep 17 00:00:00 2001 From: c Date: Mon, 26 Jan 2026 17:13:21 +0100 Subject: [PATCH 1/8] refactor(tauri-build): make better use of OsString Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> --- crates/tauri-build/src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/tauri-build/src/lib.rs b/crates/tauri-build/src/lib.rs index ddfac0cc5c7b..78a085e29b0f 100644 --- a/crates/tauri-build/src/lib.rs +++ b/crates/tauri-build/src/lib.rs @@ -411,7 +411,8 @@ impl Attributes { } pub fn is_dev() -> bool { - env::var("DEP_TAURI_DEV").expect("missing `cargo:dev` instruction, please update tauri to latest") + env::var_os("DEP_TAURI_DEV") + .expect("missing `cargo:dev` instruction, please update tauri to latest") == "true" } @@ -458,7 +459,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> { println!("cargo:rerun-if-env-changed=TAURI_CONFIG"); - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + let target_os = env::var_os("CARGO_CFG_TARGET_OS").unwrap(); let mobile = target_os == "ios" || target_os == "android"; cfg_alias("desktop", !mobile); cfg_alias("mobile", mobile); @@ -503,7 +504,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> { let cargo_toml_path = Path::new("Cargo.toml").canonicalize()?; let mut manifest = Manifest::::from_path_with_metadata(cargo_toml_path)?; - let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); manifest::check(&config, &mut manifest)?; @@ -682,7 +683,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> { } } "msvc" => { - if env::var("STATIC_VCRUNTIME").is_ok_and(|v| v == "true") { + if env::var_os("STATIC_VCRUNTIME").is_some_and(|v| v == "true") { static_vcruntime::build(); } } From ae2ba5d20fa98668110f0005841a1ab712885675 Mon Sep 17 00:00:00 2001 From: sftse Date: Fri, 30 Jan 2026 11:38:21 +0100 Subject: [PATCH 2/8] refactor(tauri-build): dont wrap const value in function --- crates/tauri-build/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri-build/src/lib.rs b/crates/tauri-build/src/lib.rs index 78a085e29b0f..a6baf947665c 100644 --- a/crates/tauri-build/src/lib.rs +++ b/crates/tauri-build/src/lib.rs @@ -539,7 +539,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> { .bundle .resources .clone() - .unwrap_or_else(|| BundleResources::List(Vec::new())); + .unwrap_or(BundleResources::List(Vec::new())); if target_triple.contains("windows") { if let Some(fixed_webview2_runtime_path) = match &config.bundle.windows.webview_install_mode { WebviewInstallMode::FixedRuntime { path } => Some(path), From 7e2a9dd150ec81f84abf0193ceb44c5b16f524eb Mon Sep 17 00:00:00 2001 From: sftse Date: Fri, 30 Jan 2026 11:40:29 +0100 Subject: [PATCH 3/8] refactor(tauri-build): None codepath is never used, replace Option with Vec --- crates/tauri-build/src/manifest.rs | 38 ++++++++++-------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/crates/tauri-build/src/manifest.rs b/crates/tauri-build/src/manifest.rs index 30033e9bc2c3..5aa668eeae1c 100644 --- a/crates/tauri-build/src/manifest.rs +++ b/crates/tauri-build/src/manifest.rs @@ -23,7 +23,7 @@ struct AllowlistedDependency { name: String, alias: Option, kind: DependencyKind, - all_cli_managed_features: Option>, + all_cli_managed_features: Vec<&'static str>, expected_features: Vec, } @@ -33,7 +33,7 @@ pub fn check(config: &Config, manifest: &mut Manifest) -> Result<()> { name: "tauri-build".into(), alias: None, kind: DependencyKind::Build, - all_cli_managed_features: Some(vec!["isolation"]), + all_cli_managed_features: vec!["isolation"], expected_features: match config.app.security.pattern { PatternKind::Isolation { .. } => vec!["isolation".to_string()], _ => vec![], @@ -43,12 +43,10 @@ pub fn check(config: &Config, manifest: &mut Manifest) -> Result<()> { name: "tauri".into(), alias: None, kind: DependencyKind::Normal, - all_cli_managed_features: Some( - AppConfig::all_features() - .into_iter() - .filter(|f| f != &"tray-icon") - .collect(), - ), + all_cli_managed_features: AppConfig::all_features() + .into_iter() + .filter(|f| f != &"tray-icon") + .collect(), expected_features: config .app .features() @@ -129,23 +127,13 @@ fn check_features(dependency: Dependency, metadata: &AllowlistedDependency) -> R Dependency::Inherited(dep) => dep.features, }; - let diff = if let Some(all_cli_managed_features) = &metadata.all_cli_managed_features { - features_diff( - &features - .into_iter() - .filter(|f| all_cli_managed_features.contains(&f.as_str())) - .collect::>(), - &metadata.expected_features, - ) - } else { - features_diff( - &features - .into_iter() - .filter(|f| f.starts_with("allow-")) - .collect::>(), - &metadata.expected_features, - ) - }; + let diff = features_diff( + &features + .into_iter() + .filter(|f| metadata.all_cli_managed_features.contains(&f.as_str())) + .collect::>(), + &metadata.expected_features, + ); let mut error_message = String::new(); if !diff.remove.is_empty() { From c0375b242b8b28b8c4216e8f6cb4f1f76ee17ace Mon Sep 17 00:00:00 2001 From: sftse Date: Wed, 18 Mar 2026 14:31:35 +0100 Subject: [PATCH 4/8] refactor(tauri): use blocking apis where it makes sense --- crates/tauri/src/protocol/asset.rs | 66 ++++++++++++++---------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/crates/tauri/src/protocol/asset.rs b/crates/tauri/src/protocol/asset.rs index 78ff0e38a44a..0227d0308ad3 100644 --- a/crates/tauri/src/protocol/asset.rs +++ b/crates/tauri/src/protocol/asset.rs @@ -5,10 +5,10 @@ use crate::{path::SafePathBuf, scope, webview::UriSchemeProtocolHandler}; use http::{header::*, status::StatusCode, Request, Response}; use http_range::HttpRange; +use std::fs::File; +use std::io::{Read, Seek, Write}; use std::{borrow::Cow, io::SeekFrom}; use tauri_utils::mime_type::MimeType; -use tokio::fs::File; -use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt}; pub fn get(scope: scope::fs::Scope, window_origin: String) -> UriSchemeProtocolHandler { Box::new( @@ -49,7 +49,7 @@ fn get_response( } // Separate block for easier error handling - let mut file = match crate::async_runtime::safe_block_on(File::open(path.clone())) { + let mut file = match File::open(path.clone()) { Ok(file) => file, Err(e) => { #[cfg(target_os = "android")] @@ -70,12 +70,12 @@ fn get_response( } }; - let (mut file, len, mime_type, read_bytes) = crate::async_runtime::safe_block_on(async move { + let (mut file, len, mime_type, read_bytes) = { // get file length let len = { - let old_pos = file.stream_position().await?; - let len = file.seek(SeekFrom::End(0)).await?; - file.seek(SeekFrom::Start(old_pos)).await?; + let old_pos = file.stream_position()?; + let len = file.seek(SeekFrom::End(0))?; + file.seek(SeekFrom::Start(old_pos))?; len }; @@ -83,9 +83,9 @@ fn get_response( let (mime_type, read_bytes) = { let nbytes = len.min(8192); let mut magic_buf = Vec::with_capacity(nbytes as usize); - let old_pos = file.stream_position().await?; - (&mut file).take(nbytes).read_to_end(&mut magic_buf).await?; - file.seek(SeekFrom::Start(old_pos)).await?; + let old_pos = file.stream_position()?; + (&mut file).take(nbytes).read_to_end(&mut magic_buf)?; + file.seek(SeekFrom::Start(old_pos))?; ( MimeType::parse(&magic_buf, &path), // return the `magic_bytes` if we read the whole file @@ -94,8 +94,8 @@ fn get_response( ) }; - Ok::<(File, u64, String, Option>), anyhow::Error>((file, len, mime_type, read_bytes)) - })?; + (file, len, mime_type, read_bytes) + }; resp = resp.header(CONTENT_TYPE, &mime_type); @@ -148,12 +148,12 @@ fn get_response( // calculate number of bytes needed to be read let nbytes = end + 1 - start; - let buf = crate::async_runtime::safe_block_on(async move { + let buf = { let mut buf = Vec::with_capacity(nbytes as usize); - file.seek(SeekFrom::Start(start)).await?; - file.take(nbytes).read_to_end(&mut buf).await?; - Ok::, anyhow::Error>(buf) - })?; + file.seek(SeekFrom::Start(start))?; + file.take(nbytes).read_to_end(&mut buf)?; + buf + }; resp = resp.header(CONTENT_RANGE, format!("bytes {start}-{end}/{len}")); resp = resp.header(CONTENT_LENGTH, end + 1 - start); @@ -186,38 +186,34 @@ fn get_response( format!("multipart/byteranges; boundary={boundary}"), ); - let buf = crate::async_runtime::safe_block_on(async move { + let buf = { // multi-part range header let mut buf = Vec::new(); for (start, end) in ranges { // a new range is being written, write the range boundary - buf.write_all(boundary_sep.as_bytes()).await?; + buf.write_all(boundary_sep.as_bytes())?; // write the needed headers `Content-Type` and `Content-Range` - buf - .write_all(format!("{CONTENT_TYPE}: {mime_type}\r\n").as_bytes()) - .await?; - buf - .write_all(format!("{CONTENT_RANGE}: bytes {start}-{end}/{len}\r\n").as_bytes()) - .await?; + buf.write_all(format!("{CONTENT_TYPE}: {mime_type}\r\n").as_bytes())?; + buf.write_all(format!("{CONTENT_RANGE}: bytes {start}-{end}/{len}\r\n").as_bytes())?; // write the separator to indicate the start of the range body - buf.write_all("\r\n".as_bytes()).await?; + buf.write_all("\r\n".as_bytes())?; // calculate number of bytes needed to be read let nbytes = end + 1 - start; let mut local_buf = Vec::with_capacity(nbytes as usize); - file.seek(SeekFrom::Start(start)).await?; - (&mut file).take(nbytes).read_to_end(&mut local_buf).await?; + file.seek(SeekFrom::Start(start))?; + (&mut file).take(nbytes).read_to_end(&mut local_buf)?; buf.extend_from_slice(&local_buf); } // all ranges have been written, write the closing boundary - buf.write_all(boundary_closer.as_bytes()).await?; + buf.write_all(boundary_closer.as_bytes())?; - Ok::, anyhow::Error>(buf) - })?; + buf + }; resp.body(buf.into()) } } else if request.method() == http::Method::HEAD { @@ -230,11 +226,9 @@ fn get_response( let buf = if let Some(b) = read_bytes { b } else { - crate::async_runtime::safe_block_on(async move { - let mut local_buf = Vec::with_capacity(len as usize); - file.read_to_end(&mut local_buf).await?; - Ok::, anyhow::Error>(local_buf) - })? + let mut local_buf = Vec::with_capacity(len as usize); + file.read_to_end(&mut local_buf)?; + local_buf }; resp = resp.header(CONTENT_LENGTH, len); resp.body(buf.into()) From 0631f8a787919adc874d862a8cb4b2fa0fd2b756 Mon Sep 17 00:00:00 2001 From: sftse Date: Wed, 18 Mar 2026 15:29:56 +0100 Subject: [PATCH 5/8] refactor(tauri): better use of std::fs API --- crates/tauri/src/protocol/asset.rs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/crates/tauri/src/protocol/asset.rs b/crates/tauri/src/protocol/asset.rs index 0227d0308ad3..fefb22f8d71c 100644 --- a/crates/tauri/src/protocol/asset.rs +++ b/crates/tauri/src/protocol/asset.rs @@ -70,17 +70,9 @@ fn get_response( } }; - let (mut file, len, mime_type, read_bytes) = { - // get file length - let len = { - let old_pos = file.stream_position()?; - let len = file.seek(SeekFrom::End(0))?; - file.seek(SeekFrom::Start(old_pos))?; - len - }; - + let len = file.metadata()?.len(); + let (mime_type, read_bytes) = { // get file mime type - let (mime_type, read_bytes) = { let nbytes = len.min(8192); let mut magic_buf = Vec::with_capacity(nbytes as usize); let old_pos = file.stream_position()?; @@ -92,9 +84,6 @@ fn get_response( // to avoid reading it again later if this is not a range request if len < 8192 { Some(magic_buf) } else { None }, ) - }; - - (file, len, mime_type, read_bytes) }; resp = resp.header(CONTENT_TYPE, &mime_type); From 3f4cd31da426b0d4db966987efc17296fe06c6bd Mon Sep 17 00:00:00 2001 From: sftse Date: Wed, 18 Mar 2026 15:31:30 +0100 Subject: [PATCH 6/8] refactor(tauri): rewind to start --- crates/tauri/src/protocol/asset.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/tauri/src/protocol/asset.rs b/crates/tauri/src/protocol/asset.rs index fefb22f8d71c..7933605e90a7 100644 --- a/crates/tauri/src/protocol/asset.rs +++ b/crates/tauri/src/protocol/asset.rs @@ -75,9 +75,8 @@ fn get_response( // get file mime type let nbytes = len.min(8192); let mut magic_buf = Vec::with_capacity(nbytes as usize); - let old_pos = file.stream_position()?; (&mut file).take(nbytes).read_to_end(&mut magic_buf)?; - file.seek(SeekFrom::Start(old_pos))?; + file.rewind()?; ( MimeType::parse(&magic_buf, &path), // return the `magic_bytes` if we read the whole file From bc7a00f8a8af79b5239d0f114b8394b0bff1b701 Mon Sep 17 00:00:00 2001 From: sftse Date: Wed, 18 Mar 2026 15:32:10 +0100 Subject: [PATCH 7/8] refactor(tauri): fmt --- crates/tauri/src/protocol/asset.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/tauri/src/protocol/asset.rs b/crates/tauri/src/protocol/asset.rs index 7933605e90a7..25f822059296 100644 --- a/crates/tauri/src/protocol/asset.rs +++ b/crates/tauri/src/protocol/asset.rs @@ -73,16 +73,16 @@ fn get_response( let len = file.metadata()?.len(); let (mime_type, read_bytes) = { // get file mime type - let nbytes = len.min(8192); - let mut magic_buf = Vec::with_capacity(nbytes as usize); - (&mut file).take(nbytes).read_to_end(&mut magic_buf)?; - file.rewind()?; - ( - MimeType::parse(&magic_buf, &path), - // return the `magic_bytes` if we read the whole file - // to avoid reading it again later if this is not a range request - if len < 8192 { Some(magic_buf) } else { None }, - ) + let nbytes = len.min(8192); + let mut magic_buf = Vec::with_capacity(nbytes as usize); + (&mut file).take(nbytes).read_to_end(&mut magic_buf)?; + file.rewind()?; + ( + MimeType::parse(&magic_buf, &path), + // return the `magic_bytes` if we read the whole file + // to avoid reading it again later if this is not a range request + if len < 8192 { Some(magic_buf) } else { None }, + ) }; resp = resp.header(CONTENT_TYPE, &mime_type); From f483655fac7b623b32163443a8bb67693547b1fd Mon Sep 17 00:00:00 2001 From: sftse Date: Thu, 19 Mar 2026 19:08:09 +0100 Subject: [PATCH 8/8] add change file --- .changes/change-pr-15117.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/change-pr-15117.md diff --git a/.changes/change-pr-15117.md b/.changes/change-pr-15117.md new file mode 100644 index 000000000000..85eddd21f6ff --- /dev/null +++ b/.changes/change-pr-15117.md @@ -0,0 +1,6 @@ +--- +"tauri-build": patch:enhance +"tauri": patch:enhance +--- + +Simplify async-sync code boundaries, no externally visible changes