Skip to content

Commit 63b9c64

Browse files
authored
fix(bundler) properly detect the target directory (#895)
1 parent b96b1fb commit 63b9c64

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

.changes/bundler-target-dir.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-bundler": patch
3+
---
4+
5+
Fixes the target directory detection, respecting the `CARGO_TARGET_DIR` and `.cargo/config (build.target-dir)` options to set the Cargo output directory.

cli/tauri-bundler/src/bundle/settings.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,17 @@ impl CargoSettings {
298298
}
299299
}
300300

301+
#[derive(Deserialize)]
302+
struct CargoBuildConfig {
303+
#[serde(rename = "target-dir")]
304+
target_dir: Option<String>,
305+
}
306+
307+
#[derive(Deserialize)]
308+
struct CargoConfig {
309+
build: Option<CargoBuildConfig>,
310+
}
311+
301312
impl Settings {
302313
/// Builds a Settings from the CLI args.
303314
///
@@ -353,7 +364,7 @@ impl Settings {
353364
}
354365
};
355366
let workspace_dir = Settings::get_workspace_dir(&current_dir);
356-
let target_dir = Settings::get_target_dir(&workspace_dir, &target, is_release);
367+
let target_dir = Settings::get_target_dir(&workspace_dir, &target, is_release)?;
357368
let bundle_settings = match tauri_config {
358369
Ok(config) => merge_settings(BundleSettings::default(), config.tauri.bundle),
359370
Err(e) => {
@@ -445,13 +456,42 @@ impl Settings {
445456
project_root_dir: &PathBuf,
446457
target: &Option<(String, TargetInfo)>,
447458
is_release: bool,
448-
) -> PathBuf {
449-
let mut path = project_root_dir.join("target");
459+
) -> crate::Result<PathBuf> {
460+
let mut path: PathBuf = match std::env::var_os("CARGO_TARGET_DIR") {
461+
Some(target_dir) => target_dir.into(),
462+
None => {
463+
let mut root_dir = project_root_dir.clone();
464+
let target_path: Option<PathBuf> = loop {
465+
// cargo reads configs under .cargo/config.toml or .cargo/config
466+
let mut cargo_config_path = root_dir.join(".cargo/config");
467+
if !cargo_config_path.exists() {
468+
cargo_config_path = root_dir.join(".cargo/config.toml");
469+
}
470+
// if the path exists, parse it
471+
if cargo_config_path.exists() {
472+
let mut config_str = String::new();
473+
let mut config_file = File::open(cargo_config_path)?;
474+
config_file.read_to_string(&mut config_str)?;
475+
let config: CargoConfig = toml::from_str(&config_str)?;
476+
if let Some(build) = config.build {
477+
if let Some(target_dir) = build.target_dir {
478+
break Some(target_dir.into());
479+
}
480+
}
481+
}
482+
if !root_dir.pop() {
483+
break None;
484+
}
485+
};
486+
target_path.unwrap_or_else(|| project_root_dir.join("target"))
487+
}
488+
};
489+
450490
if let Some((ref triple, _)) = *target {
451491
path.push(triple);
452492
}
453493
path.push(if is_release { "release" } else { "debug" });
454-
path
494+
Ok(path)
455495
}
456496

457497
/// Walks up the file system, looking for a Cargo.toml file

0 commit comments

Comments
 (0)