Skip to content

Commit 35aa7e1

Browse files
fix(cli): Allow the use of target dir inside frontendDist/distDir (unless it's cargo's target dir) (#13294)
Co-authored-by: Fabian-Lars <[email protected]>
1 parent 23b9da7 commit 35aa7e1

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": "patch:bug"
3+
"@tauri-apps/cli": "patch:bug"
4+
---
5+
6+
fix: allow the target directory to be inside frontendDir as long as it is not the Rust target directory inside frontendDir.

crates/tauri-cli/src/build.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use crate::{
99
app_paths::tauri_dir,
1010
config::{get as get_config, ConfigHandle, FrontendDist},
1111
},
12-
interface::{AppInterface, Interface},
12+
interface::{rust::get_cargo_target_dir, AppInterface, Interface},
1313
ConfigValue, Result,
1414
};
1515
use anyhow::Context;
1616
use clap::{ArgAction, Parser};
17-
use std::env::set_current_dir;
17+
use std::{env::set_current_dir, fs};
1818
use tauri_utils::platform::Target;
1919

2020
#[derive(Debug, Clone, Parser)]
@@ -172,19 +172,32 @@ pub fn setup(
172172
));
173173
}
174174

175+
// Issue #13287 - Allow the use of target dir inside frontendDist/distDir
176+
// https://github.com/tauri-apps/tauri/issues/13287
177+
let target_path = fs::canonicalize(get_cargo_target_dir(&options.args)?)?;
175178
let mut out_folders = Vec::new();
176-
for folder in &["node_modules", "src-tauri", "target"] {
177-
if web_asset_path.join(folder).is_dir() {
178-
out_folders.push(folder.to_string());
179+
if let Ok(web_asset_canonical) = web_asset_path.canonicalize() {
180+
if let Ok(relative_path) = target_path.strip_prefix(&web_asset_canonical) {
181+
let relative_str = relative_path.to_string_lossy();
182+
if !relative_str.is_empty() {
183+
out_folders.push(relative_str.to_string());
184+
}
185+
}
186+
187+
for folder in &["node_modules", "src-tauri"] {
188+
let sub_path = web_asset_canonical.join(folder);
189+
if sub_path.is_dir() {
190+
out_folders.push(folder.to_string());
191+
}
179192
}
180193
}
194+
181195
if !out_folders.is_empty() {
182196
return Err(anyhow::anyhow!(
183-
"The configured frontendDist includes the `{:?}` {}. Please isolate your web assets on a separate folder and update `tauri.conf.json > build > frontendDist`.",
184-
out_folders,
185-
if out_folders.len() == 1 { "folder" } else { "folders" }
186-
)
187-
);
197+
"The configured frontendDist includes the `{:?}` {}. Please isolate your web assets on a separate folder and update `tauri.conf.json > build > frontendDist`.",
198+
out_folders,
199+
if out_folders.len() == 1 { "folder" } else { "folders" }
200+
));
188201
}
189202
}
190203

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,22 +1148,29 @@ pub(crate) fn get_cargo_metadata() -> crate::Result<CargoMetadata> {
11481148
Ok(serde_json::from_slice(&output.stdout)?)
11491149
}
11501150

1151-
/// This function determines the 'target' directory and suffixes it with the profile
1152-
/// to determine where the compiled binary will be located.
1153-
fn get_target_dir(triple: Option<&str>, options: &Options) -> crate::Result<PathBuf> {
1154-
let mut path = if let Some(target) = get_cargo_option(&options.args, "--target-dir") {
1151+
/// Get the cargo target directory based on the provided arguments.
1152+
/// If "--target-dir" is specified in args, use it as the target directory (relative to current directory).
1153+
/// Otherwise, use the target directory from cargo metadata.
1154+
pub(crate) fn get_cargo_target_dir(args: &[String]) -> crate::Result<PathBuf> {
1155+
let path = if let Some(target) = get_cargo_option(args, "--target-dir") {
11551156
std::env::current_dir()?.join(target)
11561157
} else {
1157-
let mut path = get_cargo_metadata()
1158+
get_cargo_metadata()
11581159
.with_context(|| "failed to get cargo metadata")?
1159-
.target_directory;
1160+
.target_directory
1161+
};
11601162

1161-
if let Some(triple) = triple {
1162-
path.push(triple);
1163-
}
1163+
Ok(path)
1164+
}
11641165

1165-
path
1166-
};
1166+
/// This function determines the 'target' directory and suffixes it with the profile
1167+
/// to determine where the compiled binary will be located.
1168+
fn get_target_dir(triple: Option<&str>, options: &Options) -> crate::Result<PathBuf> {
1169+
let mut path = get_cargo_target_dir(&options.args)?;
1170+
1171+
if let Some(triple) = triple {
1172+
path.push(triple);
1173+
}
11671174

11681175
path.push(get_profile_dir(options));
11691176

@@ -1633,7 +1640,10 @@ mod tests {
16331640
);
16341641
assert_eq!(
16351642
get_target_dir(Some("x86_64-pc-windows-msvc"), &options).unwrap(),
1636-
current_dir.join("path/to/some/dir/release")
1643+
current_dir
1644+
.join("path/to/some/dir")
1645+
.join("x86_64-pc-windows-msvc")
1646+
.join("release")
16371647
);
16381648

16391649
let options = Options {

0 commit comments

Comments
 (0)