fix(cli): Allow the use of target dir inside frontendDist/distDir (unless it's cargo's target dir) (#13294)

Co-authored-by: Fabian-Lars <github@fabianlars.de>
This commit is contained in:
Kingsword
2025-04-28 03:37:50 +08:00
committed by GitHub
parent 23b9da75b9
commit 35aa7e1218
3 changed files with 52 additions and 23 deletions

View File

@@ -0,0 +1,6 @@
---
"tauri-cli": "patch:bug"
"@tauri-apps/cli": "patch:bug"
---
fix: allow the target directory to be inside frontendDir as long as it is not the Rust target directory inside frontendDir.

View File

@@ -9,12 +9,12 @@ use crate::{
app_paths::tauri_dir,
config::{get as get_config, ConfigHandle, FrontendDist},
},
interface::{AppInterface, Interface},
interface::{rust::get_cargo_target_dir, AppInterface, Interface},
ConfigValue, Result,
};
use anyhow::Context;
use clap::{ArgAction, Parser};
use std::env::set_current_dir;
use std::{env::set_current_dir, fs};
use tauri_utils::platform::Target;
#[derive(Debug, Clone, Parser)]
@@ -172,19 +172,32 @@ pub fn setup(
));
}
// Issue #13287 - Allow the use of target dir inside frontendDist/distDir
// https://github.com/tauri-apps/tauri/issues/13287
let target_path = fs::canonicalize(get_cargo_target_dir(&options.args)?)?;
let mut out_folders = Vec::new();
for folder in &["node_modules", "src-tauri", "target"] {
if web_asset_path.join(folder).is_dir() {
out_folders.push(folder.to_string());
if let Ok(web_asset_canonical) = web_asset_path.canonicalize() {
if let Ok(relative_path) = target_path.strip_prefix(&web_asset_canonical) {
let relative_str = relative_path.to_string_lossy();
if !relative_str.is_empty() {
out_folders.push(relative_str.to_string());
}
}
for folder in &["node_modules", "src-tauri"] {
let sub_path = web_asset_canonical.join(folder);
if sub_path.is_dir() {
out_folders.push(folder.to_string());
}
}
}
if !out_folders.is_empty() {
return Err(anyhow::anyhow!(
"The configured frontendDist includes the `{:?}` {}. Please isolate your web assets on a separate folder and update `tauri.conf.json > build > frontendDist`.",
out_folders,
if out_folders.len() == 1 { "folder" } else { "folders" }
)
);
"The configured frontendDist includes the `{:?}` {}. Please isolate your web assets on a separate folder and update `tauri.conf.json > build > frontendDist`.",
out_folders,
if out_folders.len() == 1 { "folder" } else { "folders" }
));
}
}

View File

@@ -1148,22 +1148,29 @@ pub(crate) fn get_cargo_metadata() -> crate::Result<CargoMetadata> {
Ok(serde_json::from_slice(&output.stdout)?)
}
/// Get the cargo target directory based on the provided arguments.
/// If "--target-dir" is specified in args, use it as the target directory (relative to current directory).
/// Otherwise, use the target directory from cargo metadata.
pub(crate) fn get_cargo_target_dir(args: &[String]) -> crate::Result<PathBuf> {
let path = if let Some(target) = get_cargo_option(args, "--target-dir") {
std::env::current_dir()?.join(target)
} else {
get_cargo_metadata()
.with_context(|| "failed to get cargo metadata")?
.target_directory
};
Ok(path)
}
/// This function determines the 'target' directory and suffixes it with the profile
/// to determine where the compiled binary will be located.
fn get_target_dir(triple: Option<&str>, options: &Options) -> crate::Result<PathBuf> {
let mut path = if let Some(target) = get_cargo_option(&options.args, "--target-dir") {
std::env::current_dir()?.join(target)
} else {
let mut path = get_cargo_metadata()
.with_context(|| "failed to get cargo metadata")?
.target_directory;
let mut path = get_cargo_target_dir(&options.args)?;
if let Some(triple) = triple {
path.push(triple);
}
path
};
if let Some(triple) = triple {
path.push(triple);
}
path.push(get_profile_dir(options));
@@ -1633,7 +1640,10 @@ mod tests {
);
assert_eq!(
get_target_dir(Some("x86_64-pc-windows-msvc"), &options).unwrap(),
current_dir.join("path/to/some/dir/release")
current_dir
.join("path/to/some/dir")
.join("x86_64-pc-windows-msvc")
.join("release")
);
let options = Options {