Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/fix-nondeterministic-context-codegen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'tauri-utils': 'patch:bug'
---

Sort csp/plugin/header configs when generating HashMap constructors so that `generate_context!` is deterministic.

See: https://github.com/tauri-apps/tauri/issues/14978 for more information
26 changes: 23 additions & 3 deletions crates/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3842,9 +3842,14 @@ mod build {
quote!(#prefix::Policy(#policy.into()))
}
Self::DirectiveMap(list) => {
// Pass a sorted vec so the HashMap constructor is deterministic
// see: https://github.com/tauri-apps/tauri/issues/14978
// TODO: Remove this in v3, use a BTreeMap instead of a HashMap
let mut sorted: Vec<_> = list.iter().collect();
sorted.sort_by_key(|(k, _)| *k);
let map = map_lit(
quote! { ::std::collections::HashMap },
list,
sorted,
str_lit,
identity,
);
Expand Down Expand Up @@ -3900,7 +3905,17 @@ mod build {
quote!(#prefix::List(#list))
}
Self::Map(m) => {
let map = map_lit(quote! { ::std::collections::HashMap }, m, str_lit, str_lit);
// Pass a sorted vec so the HashMap constructor is deterministic
// see: https://github.com/tauri-apps/tauri/issues/14978
// TODO: Remove this in v3, use a BTreeMap instead of a HashMap
let mut sorted: Vec<_> = m.iter().collect();
sorted.sort_by_key(|(k, _)| *k);
let map = map_lit(
quote! { ::std::collections::HashMap },
sorted,
str_lit,
str_lit,
);
quote!(#prefix::Map(#map))
}
})
Expand Down Expand Up @@ -4047,9 +4062,14 @@ mod build {

impl ToTokens for PluginConfig {
fn to_tokens(&self, tokens: &mut TokenStream) {
// Pass a sorted vec so the HashMap constructor is deterministic
// see: https://github.com/tauri-apps/tauri/issues/14978
// TODO: Remove this in v3, use a BTreeMap instead of a HashMap
let mut sorted: Vec<_> = self.0.iter().collect();
sorted.sort_by_key(|(k, _)| *k);
let config = map_lit(
quote! { ::std::collections::HashMap },
&self.0,
sorted,
str_lit,
json_value_lit,
);
Expand Down
Loading