diff --git a/crates/piqueld-build/src/lib.rs b/crates/piqueld-build/src/lib.rs index 01330b7c5..47ed8ff02 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,14 @@ 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, 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 { - const OUT_TO_TARGET_DEPTH: usize = 4; let canonical = out_dir .canonicalize() .map_err(|source| BuildError::OutDir { @@ -210,7 +214,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 +233,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.