fix(codegen): Context generation with custom assets (#14883)

when custom assets are provided (`tauri::generate_context!(assets = my_assets)`) we can't use the fn inner logic directly and capture variables - we must pass them as arguments
This commit is contained in:
Lucas Fernandes Nogueira 2026-02-03 11:01:12 -03:00 committed by GitHub
parent 540c5b4e59
commit eb5d88427a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 8 deletions

View File

@ -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.

View File

@ -453,7 +453,7 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
#[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<TokenStream> {
context
});
Ok(quote!({
// Wrapping in a function to make rust analyzer faster,
// see https://github.com/tauri-apps/tauri/pull/14457
fn inner<R: #root::Runtime>() -> #root::Context<R> {
// 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<R: #root::Runtime, A: #root::Assets<R> + 'static>(assets: A) -> #root::Context<R> {
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<TokenStream> {
}
}
}
inner()
}))
inner(#assets)
});
Ok(output)
}
fn find_icon(