Skip to content

Commit eb5d884

Browse files
authored
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
1 parent 540c5b4 commit eb5d884

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-codegen": patch:bug
3+
---
4+
5+
Fix `tauri::Context` code generation failing with `can't capture dynamic environment in a fn item` when custom assets are provided.

crates/tauri-codegen/src/context.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
453453
#[allow(unused_mut, clippy::let_and_return)]
454454
let mut context = #root::Context::new(
455455
#config,
456-
::std::boxed::Box::new(#assets),
456+
::std::boxed::Box::new(assets),
457457
#default_window_icon,
458458
#app_icon,
459459
#package_info,
@@ -468,14 +468,16 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
468468
context
469469
});
470470

471-
Ok(quote!({
472-
// Wrapping in a function to make rust analyzer faster,
473-
// see https://github.com/tauri-apps/tauri/pull/14457
474-
fn inner<R: #root::Runtime>() -> #root::Context<R> {
471+
// Wrapping in a function to make rust analyzer faster,
472+
// see https://github.com/tauri-apps/tauri/pull/14457
473+
// We take the assets as an argument so when the caller provides custom `assets` the closure
474+
// does not capture from the caller's scope ("can't capture dynamic environment in a fn item").
475+
let output = quote!({
476+
fn inner<R: #root::Runtime, A: #root::Assets<R> + 'static>(assets: A) -> #root::Context<R> {
475477
let thread = ::std::thread::Builder::new()
476478
.name(String::from("generated tauri context creation"))
477479
.stack_size(8 * 1024 * 1024)
478-
.spawn(|| #context)
480+
.spawn(move || #context)
479481
.expect("unable to create thread with 8MiB stack");
480482

481483
match thread.join() {
@@ -486,8 +488,10 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
486488
}
487489
}
488490
}
489-
inner()
490-
}))
491+
inner(#assets)
492+
});
493+
494+
Ok(output)
491495
}
492496

493497
fn find_icon(

0 commit comments

Comments
 (0)