Skip to content

Commit ceaa4c2

Browse files
committed
Auto merge of #8012 - ehuss:fix-config-profile-test, r=alexcrichton
Fix config profiles using "dev" in `cargo test`. Fix a bug where the "dev" profile was not loaded from config when running `cargo test` when "dev" is not listed in `Cargo.toml`. There was a mistake in #7750 where it did not consider implicit profiles. Config profiles need to be loaded explicitly in order to properly handle environment variables. However, it was only looking at the profile requested on the command-line and those listed in `Cargo.toml`. `cargo test` also implicitly uses the "dev" profile for dependencies, so make sure those are loaded from config as well.
2 parents 5e09ab5 + 51f3253 commit ceaa4c2

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/cargo/core/profiles.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1014,27 +1014,33 @@ fn merge_config_profiles(
10141014
Some(profiles) => profiles.get_all().clone(),
10151015
None => BTreeMap::new(),
10161016
};
1017-
// List of profile names to check if defined in config only.
1018-
let mut check_to_add = vec![requested_profile];
1017+
// Set of profile names to check if defined in config only.
1018+
let mut check_to_add = HashSet::new();
1019+
check_to_add.insert(requested_profile);
10191020
// Merge config onto manifest profiles.
10201021
for (name, profile) in &mut profiles {
10211022
if let Some(config_profile) = get_config_profile(name, config, features)? {
10221023
profile.merge(&config_profile);
10231024
}
10241025
if let Some(inherits) = &profile.inherits {
1025-
check_to_add.push(*inherits);
1026+
check_to_add.insert(*inherits);
10261027
}
10271028
}
1029+
// Add the built-in profiles. This is important for things like `cargo
1030+
// test` which implicitly use the "dev" profile for dependencies.
1031+
for name in &["dev", "release", "test", "bench"] {
1032+
check_to_add.insert(InternedString::new(name));
1033+
}
10281034
// Add config-only profiles.
10291035
// Need to iterate repeatedly to get all the inherits values.
1030-
let mut current = Vec::new();
1036+
let mut current = HashSet::new();
10311037
while !check_to_add.is_empty() {
10321038
std::mem::swap(&mut current, &mut check_to_add);
1033-
for name in current.drain(..) {
1039+
for name in current.drain() {
10341040
if !profiles.contains_key(&name) {
10351041
if let Some(config_profile) = get_config_profile(&name, config, features)? {
10361042
if let Some(inherits) = &config_profile.inherits {
1037-
check_to_add.push(*inherits);
1043+
check_to_add.insert(*inherits);
10381044
}
10391045
profiles.insert(name, config_profile);
10401046
}

tests/testsuite/profile_config.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Tests for profiles defined in config files.
22
33
use cargo_test_support::paths::CargoPathExt;
4+
use cargo_test_support::registry::Package;
45
use cargo_test_support::{basic_lib_manifest, paths, project};
56

67
#[cargo_test]
@@ -454,3 +455,38 @@ fn named_env_profile() {
454455
.with_stderr_contains("[..]-C codegen-units=1 [..]")
455456
.run();
456457
}
458+
459+
#[cargo_test]
460+
fn test_with_dev_profile() {
461+
// `cargo test` uses "dev" profile for dependencies.
462+
Package::new("somedep", "1.0.0").publish();
463+
let p = project()
464+
.file(
465+
"Cargo.toml",
466+
r#"
467+
[package]
468+
name = "foo"
469+
version = "0.1.0"
470+
471+
[dependencies]
472+
somedep = "1.0"
473+
"#,
474+
)
475+
.file("src/lib.rs", "")
476+
.build();
477+
p.cargo("test --lib --no-run -v")
478+
.env("CARGO_PROFILE_DEV_DEBUG", "0")
479+
.with_stderr(
480+
"\
481+
[UPDATING] [..]
482+
[DOWNLOADING] [..]
483+
[DOWNLOADED] [..]
484+
[COMPILING] somedep v1.0.0
485+
[RUNNING] `rustc --crate-name somedep [..]-C debuginfo=0[..]
486+
[COMPILING] foo v0.1.0 [..]
487+
[RUNNING] `rustc --crate-name foo [..]-C debuginfo=2[..]
488+
[FINISHED] [..]
489+
",
490+
)
491+
.run();
492+
}

0 commit comments

Comments
 (0)