Skip to content

Commit 7b16daf

Browse files
authored
fix(tauri-utils): sort csp/plugin/header config maps during codegen so generate_context! is deterministic (tauri-apps#14986)
* fix(tauri-utils): sort csp/plugin/header config maps during codegen so generate_context! is deterministic * add comments explaining rationale, and todo for removing the hack in v3
1 parent 7782c55 commit 7b16daf

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'tauri-utils': 'patch:bug'
3+
---
4+
5+
Sort csp/plugin/header configs when generating HashMap constructors so that `generate_context!` is deterministic.
6+
7+
See: https://github.com/tauri-apps/tauri/issues/14978 for more information

crates/tauri-utils/src/config.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3842,9 +3842,14 @@ mod build {
38423842
quote!(#prefix::Policy(#policy.into()))
38433843
}
38443844
Self::DirectiveMap(list) => {
3845+
// Pass a sorted vec so the HashMap constructor is deterministic
3846+
// see: https://github.com/tauri-apps/tauri/issues/14978
3847+
// TODO: Remove this in v3, use a BTreeMap instead of a HashMap
3848+
let mut sorted: Vec<_> = list.iter().collect();
3849+
sorted.sort_by_key(|(k, _)| *k);
38453850
let map = map_lit(
38463851
quote! { ::std::collections::HashMap },
3847-
list,
3852+
sorted,
38483853
str_lit,
38493854
identity,
38503855
);
@@ -3900,7 +3905,17 @@ mod build {
39003905
quote!(#prefix::List(#list))
39013906
}
39023907
Self::Map(m) => {
3903-
let map = map_lit(quote! { ::std::collections::HashMap }, m, str_lit, str_lit);
3908+
// Pass a sorted vec so the HashMap constructor is deterministic
3909+
// see: https://github.com/tauri-apps/tauri/issues/14978
3910+
// TODO: Remove this in v3, use a BTreeMap instead of a HashMap
3911+
let mut sorted: Vec<_> = m.iter().collect();
3912+
sorted.sort_by_key(|(k, _)| *k);
3913+
let map = map_lit(
3914+
quote! { ::std::collections::HashMap },
3915+
sorted,
3916+
str_lit,
3917+
str_lit,
3918+
);
39043919
quote!(#prefix::Map(#map))
39053920
}
39063921
})
@@ -4047,9 +4062,14 @@ mod build {
40474062

40484063
impl ToTokens for PluginConfig {
40494064
fn to_tokens(&self, tokens: &mut TokenStream) {
4065+
// Pass a sorted vec so the HashMap constructor is deterministic
4066+
// see: https://github.com/tauri-apps/tauri/issues/14978
4067+
// TODO: Remove this in v3, use a BTreeMap instead of a HashMap
4068+
let mut sorted: Vec<_> = self.0.iter().collect();
4069+
sorted.sort_by_key(|(k, _)| *k);
40504070
let config = map_lit(
40514071
quote! { ::std::collections::HashMap },
4052-
&self.0,
4072+
sorted,
40534073
str_lit,
40544074
json_value_lit,
40554075
);

0 commit comments

Comments
 (0)