Skip to content

Commit 5a0e922

Browse files
authored
feat(cli): discover src/main.rs binary automatically (#11007)
1 parent 4f44ebf commit 5a0e922

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

.changes/cli-handle-main-binary.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:enhance
3+
"@tauri-apps/cli": patch:enhance
4+
---
5+
6+
Automatically discover the `src-tauri/src/main.rs` binary when it is not explicitly defined in the Cargo manifest bin array.

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

+41-18
Original file line numberDiff line numberDiff line change
@@ -906,28 +906,51 @@ impl AppSettings for RustAppSettings {
906906
}
907907
}
908908

909-
let mut bins_path = tauri_dir().to_path_buf();
910-
bins_path.push("src/bin");
911-
if let Ok(fs_bins) = std::fs::read_dir(bins_path) {
912-
for entry in fs_bins {
913-
let path = entry?.path();
914-
if let Some(name) = path.file_stem() {
915-
// see https://github.com/tauri-apps/tauri/pull/10977#discussion_r1759742414
916-
let bin_exists = binaries.iter().any(|bin| {
917-
bin.name() == name || path.ends_with(bin.src_path().unwrap_or(&"".to_string()))
918-
});
919-
if !bin_exists {
920-
binaries.push(BundleBinary::new(
921-
format!("{}{}", name.to_string_lossy(), ext),
922-
false,
923-
))
924-
}
925-
}
909+
let mut binaries_paths = std::fs::read_dir(tauri_dir().join("src/bin"))
910+
.map(|dir| {
911+
dir
912+
.into_iter()
913+
.flatten()
914+
.map(|entry| {
915+
(
916+
entry
917+
.path()
918+
.file_stem()
919+
.unwrap_or_default()
920+
.to_string_lossy()
921+
.into_owned(),
922+
entry.path(),
923+
)
924+
})
925+
.collect::<Vec<_>>()
926+
})
927+
.unwrap_or_default();
928+
929+
if !binaries_paths
930+
.iter()
931+
.any(|(_name, path)| path == Path::new("src/main.rs"))
932+
&& tauri_dir().join("src/main.rs").exists()
933+
{
934+
binaries_paths.push((
935+
self.cargo_package_settings.name.clone(),
936+
tauri_dir().join("src/main.rs"),
937+
));
938+
}
939+
940+
for (name, path) in binaries_paths {
941+
// see https://github.com/tauri-apps/tauri/pull/10977#discussion_r1759742414
942+
let bin_exists = binaries
943+
.iter()
944+
.any(|bin| bin.name() == name || path.ends_with(bin.src_path().unwrap_or(&"".to_string())));
945+
if !bin_exists {
946+
binaries.push(BundleBinary::new(format!("{name}{ext}"), false))
926947
}
927948
}
928949

929950
if let Some(default_run) = self.package_settings.default_run.as_ref() {
930-
if !binaries.iter_mut().any(|bin| bin.name() == default_run) {
951+
if let Some(binary) = binaries.iter_mut().find(|bin| bin.name() == default_run) {
952+
binary.set_main(true);
953+
} else {
931954
binaries.push(BundleBinary::new(format!("{}{}", default_run, ext), true));
932955
}
933956
}

0 commit comments

Comments
 (0)