Skip to content

Commit 3ba9de8

Browse files
committed
Make libtest optional.
1 parent d4c3eea commit 3ba9de8

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/cargo/core/compiler/standard_lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub fn parse_unstable_flag(value: Option<&str>) -> Vec<String> {
1919
crates.insert("core");
2020
crates.insert("panic_unwind");
2121
crates.insert("compiler_builtins");
22-
crates.insert("test");
2322
} else if crates.contains("core") {
2423
crates.insert("compiler_builtins");
2524
}
@@ -83,7 +82,11 @@ pub fn resolve_std<'cfg>(
8382
// TODO: Consider doing something to enforce --locked? Or to prevent the
8483
// lock file from being written, such as setting ephemeral.
8584
let std_ws = Workspace::new_virtual(src_path, current_manifest, virtual_manifest, config)?;
86-
let spec = Packages::Packages(Vec::from(crates));
85+
// `test` is not in the default set because it is optional, but it needs
86+
// to be part of the resolve in case we do need it.
87+
let mut spec_pkgs = Vec::from(crates);
88+
spec_pkgs.push("test".to_string());
89+
let spec = Packages::Packages(spec_pkgs);
8790
let specs = spec.to_package_id_specs(&std_ws)?;
8891
let features = vec!["panic-unwind".to_string(), "backtrace".to_string()];
8992
// dev_deps setting shouldn't really matter here.

src/cargo/ops/cargo_compile.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,16 @@ pub fn compile_ws<'a>(
390390
)?;
391391

392392
let std_roots = if let Some(crates) = &config.cli_unstable().build_std {
393-
standard_lib::generate_std_roots(&bcx, crates, std_resolve.as_ref().unwrap())?
393+
// Only build libtest if it looks like it is needed.
394+
let mut crates = crates.clone();
395+
if !crates.iter().any(|c| c == "test")
396+
&& units
397+
.iter()
398+
.any(|unit| unit.mode.is_rustc_test() && unit.target.harness())
399+
{
400+
crates.push("test".to_string());
401+
}
402+
standard_lib::generate_std_roots(&bcx, &crates, std_resolve.as_ref().unwrap())?
394403
} else {
395404
Vec::new()
396405
};

0 commit comments

Comments
 (0)