diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index e872d9409a7..9c2aa3f1f81 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -398,7 +398,10 @@ pub fn compile_ws<'a>( .iter() .any(|unit| unit.mode.is_rustc_test() && unit.target.harness()) { - crates.push("test".to_string()); + // Only build libtest when libstd is built (libtest depends on libstd) + if crates.iter().any(|c| c == "std") { + crates.push("test".to_string()); + } } standard_lib::generate_std_roots(&bcx, &crates, std_resolve.as_ref().unwrap())? } else { diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs index 4fa1d0edc5a..0ca7856d979 100644 --- a/tests/testsuite/standard_lib.rs +++ b/tests/testsuite/standard_lib.rs @@ -37,6 +37,7 @@ fn std_lib() { hashbrown(); libc(); test(); + custom_test_framework(); target_proc_macro(); bench(); doc(); @@ -186,6 +187,47 @@ fn test() { .run(); } +fn custom_test_framework() { + let p = project() + .file( + "src/lib.rs", + r#" + #![no_std] + #![cfg_attr(test, no_main)] + #![feature(custom_test_frameworks)] + #![test_runner(crate::test_runner)] + + pub fn test_runner(_tests: &[&dyn Fn()]) {} + + #[panic_handler] + fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} + } + "#, + ) + .file( + "target.json", + r#" + { + "llvm-target": "x86_64-unknown-none-gnu", + "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128", + "arch": "x86_64", + "target-endian": "little", + "target-pointer-width": "64", + "target-c-int-width": "32", + "os": "none", + "linker-flavor": "ld.lld", + "linker": "rust-lld", + "executables": true, + "panic-strategy": "abort" + } + "#, + ) + .build(); + + cargo_build_std(&p, "test --target target.json --no-run -v", "core").run(); +} + fn target_proc_macro() { let p = project() .file(