diff --git a/.changes/codegen-custom-context-assets-fix.md b/.changes/codegen-custom-context-assets-fix.md new file mode 100644 index 000000000..09363e9eb --- /dev/null +++ b/.changes/codegen-custom-context-assets-fix.md @@ -0,0 +1,5 @@ +--- +"tauri-codegen": patch:bug +--- + +Fix `tauri::Context` code generation failing with `can't capture dynamic environment in a fn item` when custom assets are provided. diff --git a/crates/tauri-codegen/src/context.rs b/crates/tauri-codegen/src/context.rs index b60d2b9fd..bc889bd8d 100644 --- a/crates/tauri-codegen/src/context.rs +++ b/crates/tauri-codegen/src/context.rs @@ -453,7 +453,7 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult { #[allow(unused_mut, clippy::let_and_return)] let mut context = #root::Context::new( #config, - ::std::boxed::Box::new(#assets), + ::std::boxed::Box::new(assets), #default_window_icon, #app_icon, #package_info, @@ -468,14 +468,16 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult { context }); - Ok(quote!({ - // Wrapping in a function to make rust analyzer faster, - // see https://github.com/tauri-apps/tauri/pull/14457 - fn inner() -> #root::Context { + // Wrapping in a function to make rust analyzer faster, + // see https://github.com/tauri-apps/tauri/pull/14457 + // We take the assets as an argument so when the caller provides custom `assets` the closure + // does not capture from the caller's scope ("can't capture dynamic environment in a fn item"). + let output = quote!({ + fn inner + 'static>(assets: A) -> #root::Context { let thread = ::std::thread::Builder::new() .name(String::from("generated tauri context creation")) .stack_size(8 * 1024 * 1024) - .spawn(|| #context) + .spawn(move || #context) .expect("unable to create thread with 8MiB stack"); match thread.join() { @@ -486,8 +488,10 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult { } } } - inner() - })) + inner(#assets) + }); + + Ok(output) } fn find_icon(