Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/target-dir-nightly-out-dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-build': 'patch:bug'
---

Resolve the target directory by walking up from `OUT_DIR` to the `build` directory instead of assuming it is exactly three levels up. Recent nightly toolchains add another level to `OUT_DIR`, which made sidecars and resources land in `target/debug/build` instead of `target/debug`.
54 changes: 46 additions & 8 deletions crates/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use tauri_utils::{

use std::{
collections::HashMap,
env, fs,
env,
ffi::OsStr,
fs,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -205,6 +207,15 @@ fn copy_frameworks(dest_dir: &Path, frameworks: &[String]) -> Result<()> {
Ok(())
}

// resolves the target dir from `OUT_DIR`, which is `<target dir>/build/<crate dirs>/out`.
Comment thread
lazerg marked this conversation as resolved.
Resolved
// there is a single crate dir on stable and two of them on the new nightly layout.
fn target_dir_from_out_dir(out_dir: &Path) -> Option<&Path> {
out_dir
.ancestors()
.find(|path| path.file_name() == Some(OsStr::new("build")))
.and_then(|build_dir| build_dir.parent())
}

// creates a cfg alias if `has_feature` is true.
// `alias` must be a snake case string.
fn cfg_alias(alias: &str, has_feature: bool) {
Expand Down Expand Up @@ -574,13 +585,8 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
env::set_var("TAURI_ENV_TARGET_TRIPLE", &target_triple);

// TODO: far from ideal, but there's no other way to get the target dir, see <https://github.com/rust-lang/cargo/issues/5457>
Comment thread
lazerg marked this conversation as resolved.
Resolved
let target_dir = out_dir
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
let target_dir = target_dir_from_out_dir(&out_dir)
.with_context(|| format!("failed to resolve the target directory from {out_dir:?}"))?;

if let Some(paths) = &config.bundle.external_bin {
copy_binaries(
Expand Down Expand Up @@ -785,6 +791,38 @@ fn should_static_link_vc_runtime(config: &Config, attributes: &Attributes) -> bo
#[cfg(test)]
mod tests {
use semver::Version;
use std::path::Path;

#[test]
fn target_dir_from_stable_out_dir() {
let out_dir = Path::new("/app/target/debug/build/app-63ba68eead531e35/out");

assert_eq!(
crate::target_dir_from_out_dir(out_dir),
Some(Path::new("/app/target/debug"))
);
}

#[test]
fn target_dir_from_nightly_out_dir() {
let out_dir = Path::new("/app/target/debug/build/app/63ba68eead531e35/out");

assert_eq!(
crate::target_dir_from_out_dir(out_dir),
Some(Path::new("/app/target/debug"))
);
}

#[test]
fn target_dir_from_out_dir_with_triple() {
let out_dir =
Path::new("/app/target/aarch64-apple-darwin/release/build/app/63ba68eead531e35/out");

assert_eq!(
crate::target_dir_from_out_dir(out_dir),
Some(Path::new("/app/target/aarch64-apple-darwin/release"))
);
}

#[test]
fn version_uses_numeric_build_metadata() {
Expand Down
Loading