From 1d52d7d9abd5f6ba479a23f04a802b48db260fd6 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Mon, 3 Aug 2026 10:58:06 +0200 Subject: [PATCH] Adjust test detection for new `build-dir` layout of cargo See rust-lang/cargo#17258 --- src/main.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3cb34ed..b34ed62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,13 +67,27 @@ pub(crate) fn runner(args: RunnerArgs) -> Result { .executable .parent() .ok_or_else(|| anyhow!("kernel executable has no parent"))?; - let is_doctest = exe_parent + let exe_parent_name = exe_parent .file_name() .ok_or_else(|| anyhow!("kernel executable's parent has no file name"))? .to_str() - .ok_or_else(|| anyhow!("kernel executable's parent file name is not valid UTF-8"))? - .starts_with("rustdoctest"); - let is_test = is_doctest || exe_parent.ends_with("deps"); + .ok_or_else(|| anyhow!("kernel executable's parent file name is not valid UTF-8"))?; + let is_doctest = exe_parent_name.starts_with("rustdoctest"); + // Cargo compiles test and benchmark executables into a separate directory, + // which lets us distinguish them from normal `run` binaries. + // + // - In the classic build-dir layout they live in `/deps/`. + // - Since the build-dir layout v2 (nightly, rust-lang/cargo#17258) they + // live in `/build///out/` instead, i.e. the `out` + // directory's third ancestor is the `build` directory. + let is_test = is_doctest + || exe_parent_name == "deps" + || (exe_parent_name == "out" + && exe_parent + .ancestors() + .nth(3) + .and_then(|ancestor| ancestor.file_name()) + == Some("build".as_ref())); let bin_name = args .executable