From d5c7c960b40090d6d7e3413ee2e04a4f727ba84c Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 00:13:51 +0100 Subject: [PATCH 1/2] fix(build): put nested builds beside the target directory again The pinned Cargo lays OUT_DIR out as build///out, one level deeper than build/-/out, so going a fixed four levels up landed on target/debug and nested builds went to target/debug-ui and target/debug-probe. The target directory is now two levels above the nearest build directory, which holds for both layouts. Fixes PQD-154 --- crates/piqueld-build/src/lib.rs | 55 ++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/crates/piqueld-build/src/lib.rs b/crates/piqueld-build/src/lib.rs index 01330b7c5..ecdb1805f 100644 --- a/crates/piqueld-build/src/lib.rs +++ b/crates/piqueld-build/src/lib.rs @@ -26,10 +26,10 @@ pub enum BuildError { #[source] source: io::Error, }, - /// `OUT_DIR` is not `//build/-/out`, - /// so the target directory can't be derived from it. + /// `OUT_DIR` is not under `//build`, so the target + /// directory can't be derived from it. #[error( - "the build script's OUT_DIR {} is not //build/-/out, so the nested build has no target directory beside it", + "the build script's OUT_DIR {} is not under //build, so the nested build has no target directory beside it", .0.display() )] OutDirLayout(PathBuf), @@ -198,10 +198,12 @@ impl NestedCargo { Ok(self.target_dir.join(target).join(profile)) } - /// Derives `-` from the canonical `OUT_DIR` layout - /// `//build/-/out`. + /// Derives `-` from the canonical `OUT_DIR`, which is + /// `//build///out` for the pinned Cargo and + /// `//build/-/out` for older ones: the target + /// directory is two levels above the nearest `build` directory, so no + /// package that builds nested may be named `build`. fn sibling_target_dir(out_dir: &Path, suffix: &str) -> Result { - const OUT_TO_TARGET_DEPTH: usize = 4; let canonical = out_dir .canonicalize() .map_err(|source| BuildError::OutDir { @@ -210,7 +212,8 @@ impl NestedCargo { })?; let target_dir = canonical .ancestors() - .nth(OUT_TO_TARGET_DEPTH) + .find(|ancestor| ancestor.file_name() == Some("build".as_ref())) + .and_then(|build| build.ancestors().nth(2)) .ok_or_else(|| BuildError::OutDirLayout(canonical.clone()))?; if !target_dir.is_dir() { return Err(BuildError::MissingTargetDir(target_dir.to_owned())); @@ -228,23 +231,31 @@ impl NestedCargo { mod tests { use super::*; - /// The nested target directory sits beside the outer one, named after it. + /// The nested target directory sits beside the outer one, named after it, + /// with either `OUT_DIR` layout. #[test] fn nests_beside_the_outer_target_directory() { - let root = tempfile::tempdir().unwrap(); - let out_dir = root.path().join("target/debug/build/piqueld-0123/out"); - std::fs::create_dir_all(&out_dir).unwrap(); - let nested = NestedCargo::sibling_target_dir(&out_dir, "ui").unwrap(); - assert_eq!( - nested, - root.path().canonicalize().unwrap().join("target-ui") - ); - // The root has fewer than four ancestors wherever the tests run. - let shallow = NestedCargo::sibling_target_dir(Path::new("/"), "ui").unwrap_err(); - assert!( - matches!(shallow, BuildError::OutDirLayout(_)), - "{shallow:?}" - ); + for layout in [ + "target/debug/build/piqueld/0123/out", + "target/debug/build/piqueld-0123/out", + ] { + let root = tempfile::tempdir().unwrap(); + let out_dir = root.path().join(layout); + std::fs::create_dir_all(&out_dir).unwrap(); + let nested = NestedCargo::sibling_target_dir(&out_dir, "ui").unwrap(); + assert_eq!( + nested, + root.path().canonicalize().unwrap().join("target-ui"), + "{layout}" + ); + } + } + + /// An `OUT_DIR` with no `build` directory above it has no target directory. + #[test] + fn rejects_an_out_dir_outside_a_build_directory() { + let error = NestedCargo::sibling_target_dir(Path::new("/"), "ui").unwrap_err(); + assert!(matches!(error, BuildError::OutDirLayout(_)), "{error:?}"); } /// What a failing build script prints names the problem and its cause. From b91c406bc39eda01c3d015b381af362344b824cf Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 13:24:11 +0100 Subject: [PATCH 2/2] docs(build): mention the target-triple layout in sibling_target_dir --- crates/piqueld-build/src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/piqueld-build/src/lib.rs b/crates/piqueld-build/src/lib.rs index ecdb1805f..47ed8ff02 100644 --- a/crates/piqueld-build/src/lib.rs +++ b/crates/piqueld-build/src/lib.rs @@ -200,9 +200,11 @@ impl NestedCargo { /// Derives `-` from the canonical `OUT_DIR`, which is /// `//build///out` for the pinned Cargo and - /// `//build/-/out` for older ones: the target - /// directory is two levels above the nearest `build` directory, so no - /// package that builds nested may be named `build`. + /// `//build/-/out` for older ones, with a + /// `/` before `` when building for a `--target`: the + /// directory two levels above the nearest `build` directory is the target + /// directory, or `/`, so no package that builds nested may + /// be named `build`. fn sibling_target_dir(out_dir: &Path, suffix: &str) -> Result { let canonical = out_dir .canonicalize()