From a8fe057ad4fd4e4f344eaea0e9e7342f258b591d Mon Sep 17 00:00:00 2001 From: bors Date: Tue, 17 Mar 2020 17:47:28 +0000 Subject: [PATCH] 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. --- src/cargo/core/profiles.rs | 18 ++++++++++------ tests/testsuite/profile_config.rs | 36 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index 562bf1b3e3f..2fbdd1d5897 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -999,27 +999,33 @@ fn merge_config_profiles( Some(profiles) => profiles.get_all().clone(), None => BTreeMap::new(), }; - // List of profile names to check if defined in config only. - let mut check_to_add = vec![requested_profile]; + // Set of profile names to check if defined in config only. + let mut check_to_add = HashSet::new(); + check_to_add.insert(requested_profile); // Merge config onto manifest profiles. for (name, profile) in &mut profiles { if let Some(config_profile) = get_config_profile(name, config, features)? { profile.merge(&config_profile); } if let Some(inherits) = &profile.inherits { - check_to_add.push(*inherits); + check_to_add.insert(*inherits); } } + // Add the built-in profiles. This is important for things like `cargo + // test` which implicitly use the "dev" profile for dependencies. + for name in &["dev", "release", "test", "bench"] { + check_to_add.insert(InternedString::new(name)); + } // Add config-only profiles. // Need to iterate repeatedly to get all the inherits values. - let mut current = Vec::new(); + let mut current = HashSet::new(); while !check_to_add.is_empty() { std::mem::swap(&mut current, &mut check_to_add); - for name in current.drain(..) { + for name in current.drain() { if !profiles.contains_key(&name) { if let Some(config_profile) = get_config_profile(&name, config, features)? { if let Some(inherits) = &config_profile.inherits { - check_to_add.push(*inherits); + check_to_add.insert(*inherits); } profiles.insert(name, config_profile); } diff --git a/tests/testsuite/profile_config.rs b/tests/testsuite/profile_config.rs index e9928c5ae5b..ce1f07557a0 100644 --- a/tests/testsuite/profile_config.rs +++ b/tests/testsuite/profile_config.rs @@ -1,6 +1,7 @@ //! Tests for profiles defined in config files. use cargo_test_support::paths::CargoPathExt; +use cargo_test_support::registry::Package; use cargo_test_support::{basic_lib_manifest, paths, project}; #[cargo_test] @@ -454,3 +455,38 @@ fn named_env_profile() { .with_stderr_contains("[..]-C codegen-units=1 [..]") .run(); } + +#[cargo_test] +fn test_with_dev_profile() { + // `cargo test` uses "dev" profile for dependencies. + Package::new("somedep", "1.0.0").publish(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + somedep = "1.0" + "#, + ) + .file("src/lib.rs", "") + .build(); + p.cargo("test --lib --no-run -v") + .env("CARGO_PROFILE_DEV_DEBUG", "0") + .with_stderr( + "\ +[UPDATING] [..] +[DOWNLOADING] [..] +[DOWNLOADED] [..] +[COMPILING] somedep v1.0.0 +[RUNNING] `rustc --crate-name somedep [..]-C debuginfo=0[..] +[COMPILING] foo v0.1.0 [..] +[RUNNING] `rustc --crate-name foo [..]-C debuginfo=2[..] +[FINISHED] [..] +", + ) + .run(); +}