diff --git a/.changes/fix-frontenddir-target-error.md b/.changes/fix-frontenddir-target-error.md new file mode 100644 index 000000000..4ae48f76c --- /dev/null +++ b/.changes/fix-frontenddir-target-error.md @@ -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. diff --git a/crates/tauri-cli/src/build.rs b/crates/tauri-cli/src/build.rs index 87c81b314..f8e77f3d6 100644 --- a/crates/tauri-cli/src/build.rs +++ b/crates/tauri-cli/src/build.rs @@ -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" } + )); } } diff --git a/crates/tauri-cli/src/interface/rust.rs b/crates/tauri-cli/src/interface/rust.rs index 53ba9350a..1638693af 100644 --- a/crates/tauri-cli/src/interface/rust.rs +++ b/crates/tauri-cli/src/interface/rust.rs @@ -1148,22 +1148,29 @@ pub(crate) fn get_cargo_metadata() -> crate::Result { 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 { + 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 { - 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 {