fix(bundler) properly detect the target directory (#895)

This commit is contained in:
Lucas Fernandes Nogueira 2020-07-25 17:04:45 -03:00 committed by GitHub
parent b96b1fb6b8
commit 63b9c64572
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 4 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-bundler": patch
---
Fixes the target directory detection, respecting the `CARGO_TARGET_DIR` and `.cargo/config (build.target-dir)` options to set the Cargo output directory.

View File

@ -298,6 +298,17 @@ impl CargoSettings {
}
}
#[derive(Deserialize)]
struct CargoBuildConfig {
#[serde(rename = "target-dir")]
target_dir: Option<String>,
}
#[derive(Deserialize)]
struct CargoConfig {
build: Option<CargoBuildConfig>,
}
impl Settings {
/// Builds a Settings from the CLI args.
///
@ -353,7 +364,7 @@ impl Settings {
}
};
let workspace_dir = Settings::get_workspace_dir(&current_dir);
let target_dir = Settings::get_target_dir(&workspace_dir, &target, is_release);
let target_dir = Settings::get_target_dir(&workspace_dir, &target, is_release)?;
let bundle_settings = match tauri_config {
Ok(config) => merge_settings(BundleSettings::default(), config.tauri.bundle),
Err(e) => {
@ -445,13 +456,42 @@ impl Settings {
project_root_dir: &PathBuf,
target: &Option<(String, TargetInfo)>,
is_release: bool,
) -> PathBuf {
let mut path = project_root_dir.join("target");
) -> crate::Result<PathBuf> {
let mut path: PathBuf = match std::env::var_os("CARGO_TARGET_DIR") {
Some(target_dir) => target_dir.into(),
None => {
let mut root_dir = project_root_dir.clone();
let target_path: Option<PathBuf> = loop {
// cargo reads configs under .cargo/config.toml or .cargo/config
let mut cargo_config_path = root_dir.join(".cargo/config");
if !cargo_config_path.exists() {
cargo_config_path = root_dir.join(".cargo/config.toml");
}
// if the path exists, parse it
if cargo_config_path.exists() {
let mut config_str = String::new();
let mut config_file = File::open(cargo_config_path)?;
config_file.read_to_string(&mut config_str)?;
let config: CargoConfig = toml::from_str(&config_str)?;
if let Some(build) = config.build {
if let Some(target_dir) = build.target_dir {
break Some(target_dir.into());
}
}
}
if !root_dir.pop() {
break None;
}
};
target_path.unwrap_or_else(|| project_root_dir.join("target"))
}
};
if let Some((ref triple, _)) = *target {
path.push(triple);
}
path.push(if is_release { "release" } else { "debug" });
path
Ok(path)
}
/// Walks up the file system, looking for a Cargo.toml file