Skip to content

Commit d61fbdc

Browse files
authored
fix(cli): update tauri dependencies in both main and target-specific tables (#16109)
1 parent 0e4ded7 commit d61fbdc

3 files changed

Lines changed: 120 additions & 14 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
Fix Cargo.toml feature injection and the v1 migration only updating either `[dependencies]` or `[target.'cfg(..)'.dependencies]`, whichever came first in the file. Both the main and all target-specific dependency tables are now updated.

‎crates/tauri-cli/src/interface/rust/manifest.rs‎

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ fn find_dependency<'a>(
122122
DependencyKind::Normal => "dependencies",
123123
};
124124

125+
let mut matching_deps = Vec::new();
125126
let m = manifest.as_table_mut();
126127
for (k, v) in m.iter_mut() {
127128
if let Some(t) = v.as_table_mut() {
128129
if k == table {
129130
if let Some(item) = t.get_mut(name) {
130-
return vec![item];
131+
matching_deps.push(item);
131132
}
132133
} else if k == "target" {
133-
let mut matching_deps = Vec::new();
134134
for (_, target_value) in t.iter_mut() {
135135
if let Some(target_table) = target_value.as_table_mut() {
136136
if let Some(deps) = target_table.get_mut(table) {
@@ -140,12 +140,11 @@ fn find_dependency<'a>(
140140
}
141141
}
142142
}
143-
return matching_deps;
144143
}
145144
}
146145
}
147146

148-
Vec::new()
147+
matching_deps
149148
}
150149

151150
fn write_features<F: Fn(&str) -> bool>(
@@ -242,6 +241,9 @@ fn inject_features(
242241
for dependency in dependencies {
243242
let name = dependency.name.clone();
244243
let items = find_dependency(manifest, &dependency.name, dependency.kind);
244+
// features collected from all matching sections,
245+
// each section only gets its own non CLI-managed features
246+
let mut all_features = dependency.features.clone();
245247

246248
for item in items {
247249
// do not rewrite if dependency uses workspace inheritance
@@ -259,14 +261,17 @@ fn inject_features(
259261
let is_managed_feature: Box<dyn Fn(&str) -> bool> =
260262
Box::new(move |feature| all_cli_managed_features.contains(&feature));
261263

262-
let should_write =
263-
write_features(&name, item, is_managed_feature, &mut dependency.features)?;
264+
let mut features = dependency.features.clone();
265+
let should_write = write_features(&name, item, is_managed_feature, &mut features)?;
266+
all_features.extend(features);
264267

265268
if !persist {
266269
persist = should_write;
267270
}
268271
}
269272
}
273+
274+
dependency.features = all_features;
270275
}
271276

272277
Ok(persist)
@@ -345,8 +350,9 @@ mod tests {
345350

346351
let mut expected = HashMap::new();
347352
for dep in &dependencies {
348-
let mut features = dep.features.clone();
353+
let mut expected_per_item = Vec::new();
349354
for item in super::find_dependency(&mut manifest, &dep.name, dep.kind) {
355+
let mut features = dep.features.clone();
350356
let item_table = if let Some(table) = item.as_table() {
351357
Some(table.clone())
352358
} else if let Some(toml_edit::Value::InlineTable(table)) = item.as_value() {
@@ -362,15 +368,18 @@ mod tests {
362368
}
363369
}
364370
}
371+
expected_per_item.push(features);
365372
}
366-
expected.insert(dep.name.clone(), features);
373+
expected.insert(dep.name.clone(), expected_per_item);
367374
}
368375

369376
super::inject_features(&mut manifest, &mut dependencies).expect("failed to migrate manifest");
370377

371378
for dep in dependencies {
372-
let expected_features = expected.get(&dep.name).unwrap();
373-
for item in super::find_dependency(&mut manifest, &dep.name, dep.kind) {
379+
let expected_per_item = expected.get(&dep.name).unwrap();
380+
let items = super::find_dependency(&mut manifest, &dep.name, dep.kind);
381+
assert_eq!(items.len(), expected_per_item.len());
382+
for (item, expected_features) in items.into_iter().zip(expected_per_item) {
374383
let item_table = if let Some(table) = item.as_table() {
375384
table.clone()
376385
} else if let Some(toml_edit::Value::InlineTable(table)) = item.as_value() {
@@ -463,6 +472,54 @@ mod tests {
463472
);
464473
}
465474

475+
#[test]
476+
fn find_dependency_in_all_sections() {
477+
for toml in [
478+
r#"
479+
[target."cfg(windows)".dependencies]
480+
tauri = { version = "2", features = ["a"] }
481+
482+
[dependencies]
483+
tauri = { version = "2" }
484+
"#,
485+
r#"
486+
[dependencies]
487+
tauri = { version = "2" }
488+
489+
[target."cfg(windows)".dependencies]
490+
tauri = { version = "2", features = ["a"] }
491+
"#,
492+
] {
493+
let mut manifest = toml.parse::<toml_edit::DocumentMut>().unwrap();
494+
assert_eq!(
495+
super::find_dependency(&mut manifest, "tauri", DependencyKind::Normal).len(),
496+
2
497+
);
498+
499+
let mut dependencies = vec![tauri_dependency(HashSet::from_iter(vec![
500+
"isolation".into(),
501+
]))];
502+
super::inject_features(&mut manifest, &mut dependencies).unwrap();
503+
504+
let main_features = manifest["dependencies"]["tauri"]["features"]
505+
.as_array()
506+
.expect("features not injected in [dependencies]");
507+
// target-only features must not leak into the main section
508+
assert_eq!(
509+
main_features.iter().map(|f| f.as_str()).collect::<Vec<_>>(),
510+
vec![Some("isolation")]
511+
);
512+
let target_features = manifest["target"]["cfg(windows)"]["dependencies"]["tauri"]["features"]
513+
.as_array()
514+
.unwrap();
515+
assert!(
516+
target_features
517+
.iter()
518+
.any(|f| f.as_str() == Some("isolation"))
519+
);
520+
}
521+
}
522+
466523
#[test]
467524
fn inject_features_inline_table() {
468525
inject_features(

‎crates/tauri-cli/src/migrate/migrations/v1/manifest.rs‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ fn find_dependency<'a>(
129129
name: &'a str,
130130
table: &'a str,
131131
) -> Vec<&'a mut Item> {
132+
let mut matching_deps = Vec::new();
132133
let m = manifest.as_table_mut();
133134
for (k, v) in m.iter_mut() {
134135
if let Some(t) = v.as_table_mut() {
135136
if k == table {
136137
if let Some(item) = t.get_mut(name) {
137-
return vec![item];
138+
matching_deps.push(item);
138139
}
139140
} else if k == "target" {
140-
let mut matching_deps = Vec::new();
141141
for (_, target_value) in t.iter_mut() {
142142
if let Some(target_table) = target_value.as_table_mut() {
143143
if let Some(deps) = target_table.get_mut(table) {
@@ -147,12 +147,11 @@ fn find_dependency<'a>(
147147
}
148148
}
149149
}
150-
return matching_deps;
151150
}
152151
}
153152
}
154153

155-
Vec::new()
154+
matching_deps
156155
}
157156

158157
fn features_to_rename() -> Vec<(&'static str, &'static str)> {
@@ -382,6 +381,50 @@ mod tests {
382381
})
383382
}
384383

384+
#[test]
385+
fn migrate_all_dependency_sections() {
386+
for toml in [
387+
r#"
388+
[target."cfg(windows)".dependencies]
389+
tauri = { version = "1.0.0", features = ["system-tray"] }
390+
391+
[dependencies]
392+
tauri = { version = "1.0.0", features = ["system-tray"] }
393+
"#,
394+
r#"
395+
[dependencies]
396+
tauri = { version = "1.0.0", features = ["system-tray"] }
397+
398+
[target."cfg(windows)".dependencies]
399+
tauri = { version = "1.0.0", features = ["system-tray"] }
400+
"#,
401+
] {
402+
let mut manifest = toml
403+
.parse::<toml_edit::DocumentMut>()
404+
.expect("invalid toml");
405+
super::migrate_manifest(&mut manifest).expect("failed to migrate manifest");
406+
407+
for tauri in [
408+
&manifest["dependencies"]["tauri"],
409+
&manifest["target"]["cfg(windows)"]["dependencies"]["tauri"],
410+
] {
411+
assert_eq!(
412+
tauri["version"].as_str(),
413+
Some(super::dependency_version().as_str())
414+
);
415+
assert_eq!(
416+
tauri["features"]
417+
.as_array()
418+
.unwrap()
419+
.iter()
420+
.map(|f| f.as_str().unwrap())
421+
.collect::<Vec<_>>(),
422+
vec!["tray-icon"]
423+
);
424+
}
425+
}
426+
}
427+
385428
#[test]
386429
fn migrate_add_crate_types() {
387430
let toml = r#"

0 commit comments

Comments
 (0)