From fcfe0b8988a71321773918be903c6d43c2341b35 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Thu, 10 Oct 2019 14:39:30 -0700 Subject: [PATCH] Rename `overrides` to `package` in profiles. --- src/cargo/core/profiles.rs | 22 +++---- src/cargo/util/toml/mod.rs | 37 +++++++++--- src/doc/src/reference/unstable.md | 10 ++-- tests/testsuite/config.rs | 14 ++--- tests/testsuite/profile_config.rs | 22 +++---- tests/testsuite/profile_custom.rs | 6 +- tests/testsuite/profile_overrides.rs | 89 ++++++++++++++++++++-------- 7 files changed, 131 insertions(+), 69 deletions(-) diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index fef773f4a22..5a12f954714 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -433,12 +433,12 @@ impl Profiles { } } -/// An object used for handling the profile override hierarchy. +/// An object used for handling the profile hierarchy. /// /// The precedence of profiles are (first one wins): /// - Profiles in `.cargo/config` files (using same order as below). -/// - [profile.dev.overrides.name] -- a named package. -/// - [profile.dev.overrides."*"] -- this cannot apply to workspace members. +/// - [profile.dev.package.name] -- a named package. +/// - [profile.dev.package."*"] -- this cannot apply to workspace members. /// - [profile.dev.build-override] -- this can only apply to `build.rs` scripts /// and their dependencies. /// - [profile.dev] @@ -511,8 +511,8 @@ impl ProfileMaker { Some(ref toml) => toml, None => return Ok(()), }; - let overrides = match toml.overrides { - Some(ref overrides) => overrides, + let overrides = match toml.package.as_ref().or(toml.overrides.as_ref()) { + Some(overrides) => overrides, None => return Ok(()), }; // Verify that a package doesn't match multiple spec overrides. @@ -543,8 +543,8 @@ impl ProfileMaker { .collect::>() .join(", "); failure::bail!( - "multiple profile overrides in profile `{}` match package `{}`\n\ - found profile override specs: {}", + "multiple package overrides in profile `{}` match package `{}`\n\ + found package specs: {}", self.default.name, pkg_id, specs @@ -581,12 +581,12 @@ impl ProfileMaker { let suggestion = closest_msg(&spec.name(), packages.package_ids(), |p| p.name().as_str()); shell.warn(format!( - "profile override spec `{}` did not match any packages{}", + "package profile spec `{}` did not match any packages{}", spec, suggestion ))?; } else { shell.warn(format!( - "version or URL in profile override spec `{}` does not \ + "version or URL in package profile spec `{}` does not \ match any of the packages: {}", spec, name_matches.join(", ") @@ -609,7 +609,7 @@ fn merge_toml_overrides( merge_profile(profile, build_override); } } - if let Some(ref overrides) = toml.overrides { + if let Some(overrides) = toml.package.as_ref().or(toml.overrides.as_ref()) { if !is_member { if let Some(all) = overrides.get(&ProfilePackageSpec::All) { merge_profile(profile, all); @@ -634,7 +634,7 @@ fn merge_toml_overrides( // no additional matches. assert!( matches.next().is_none(), - "package `{}` matched multiple profile overrides", + "package `{}` matched multiple package profile overrides", pkg_id ); } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 91531d112a3..828b4351e32 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -408,7 +408,10 @@ pub struct TomlProfile { pub panic: Option, pub overflow_checks: Option, pub incremental: Option, + // `overrides` has been renamed to `package`, this should be removed when + // stabilized. pub overrides: Option>, + pub package: Option>, pub build_override: Option>, pub dir_name: Option, pub inherits: Option, @@ -457,12 +460,23 @@ impl TomlProfile { ) -> CargoResult<()> { if let Some(ref profile) = self.build_override { features.require(Feature::profile_overrides())?; - profile.validate_override()?; + profile.validate_override("build-override")?; } if let Some(ref override_map) = self.overrides { + warnings.push( + "profile key `overrides` has been renamed to `package`, \ + please update the manifest to the new key name" + .to_string(), + ); features.require(Feature::profile_overrides())?; for profile in override_map.values() { - profile.validate_override()?; + profile.validate_override("package")?; + } + } + if let Some(ref packages) = self.package { + features.require(Feature::profile_overrides())?; + for profile in packages.values() { + profile.validate_override("package")?; } } @@ -555,18 +569,21 @@ impl TomlProfile { Ok(()) } - fn validate_override(&self) -> CargoResult<()> { - if self.overrides.is_some() || self.build_override.is_some() { - bail!("Profile overrides cannot be nested."); + fn validate_override(&self, which: &str) -> CargoResult<()> { + if self.overrides.is_some() || self.package.is_some() { + bail!("package-specific profiles cannot be nested"); + } + if self.build_override.is_some() { + bail!("build-override profiles cannot be nested"); } if self.panic.is_some() { - bail!("`panic` may not be specified in a profile override.") + bail!("`panic` may not be specified in a `{}` profile", which) } if self.lto.is_some() { - bail!("`lto` may not be specified in a profile override.") + bail!("`lto` may not be specified in a `{}` profile", which) } if self.rpath.is_some() { - bail!("`rpath` may not be specified in a profile override.") + bail!("`rpath` may not be specified in a `{}` profile", which) } Ok(()) } @@ -612,6 +629,10 @@ impl TomlProfile { self.overrides = Some(v.clone()); } + if let Some(v) = &profile.package { + self.package = Some(v.clone()); + } + if let Some(v) = &profile.build_override { self.build_override = Some(v.clone()); } diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index 7509a58c446..933ae2fb761 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -23,11 +23,11 @@ index each time. * Original Issue: [#6477](https://github.com/rust-lang/cargo/pull/6477) * Cache usage meta tracking issue: [#7150](https://github.com/rust-lang/cargo/issues/7150) -The `-Z mtime-on-use` flag is an experiment to have Cargo update the mtime of -used files to make it easier for tools like cargo-sweep to detect which files +The `-Z mtime-on-use` flag is an experiment to have Cargo update the mtime of +used files to make it easier for tools like cargo-sweep to detect which files are stale. For many workflows this needs to be set on *all* invocations of cargo. To make this more practical setting the `unstable.mtime_on_use` flag in `.cargo/config` -or the corresponding ENV variable will apply the `-Z mtime-on-use` to all +or the corresponding ENV variable will apply the `-Z mtime-on-use` to all invocations of nightly cargo. (the config flag is ignored by stable) ### avoid-dev-deps @@ -104,12 +104,12 @@ opt-level = 0 debug = true # the `image` crate will be compiled with -Copt-level=3 -[profile.dev.overrides.image] +[profile.dev.package.image] opt-level = 3 # All dependencies (but not this crate itself or any workspace member) # will be compiled with -Copt-level=2 . This includes build dependencies. -[profile.dev.overrides."*"] +[profile.dev.package."*"] opt-level = 2 # Build scripts or proc-macros and their dependencies will be compiled with diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index 3632b4d37f8..d6af8614fea 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -302,7 +302,7 @@ incremental = true [profile.dev.build-override] opt-level = 1 -[profile.dev.overrides.bar] +[profile.dev.package.bar] codegen-units = 9 [profile.no-lto] @@ -315,26 +315,26 @@ lto = false let config = new_config(&[ ("CARGO_PROFILE_DEV_CODEGEN_UNITS", "5"), ("CARGO_PROFILE_DEV_BUILD_OVERRIDE_CODEGEN_UNITS", "11"), - ("CARGO_PROFILE_DEV_OVERRIDES_env_CODEGEN_UNITS", "13"), - ("CARGO_PROFILE_DEV_OVERRIDES_bar_OPT_LEVEL", "2"), + ("CARGO_PROFILE_DEV_PACKAGE_env_CODEGEN_UNITS", "13"), + ("CARGO_PROFILE_DEV_PACKAGE_bar_OPT_LEVEL", "2"), ]); // TODO: don't use actual `tomlprofile`. let p: toml::TomlProfile = config.get("profile.dev").unwrap(); - let mut overrides = collections::BTreeMap::new(); + let mut packages = collections::BTreeMap::new(); let key = toml::ProfilePackageSpec::Spec(::cargo::core::PackageIdSpec::parse("bar").unwrap()); let o_profile = toml::TomlProfile { opt_level: Some(toml::TomlOptLevel("2".to_string())), codegen_units: Some(9), ..Default::default() }; - overrides.insert(key, o_profile); + packages.insert(key, o_profile); let key = toml::ProfilePackageSpec::Spec(::cargo::core::PackageIdSpec::parse("env").unwrap()); let o_profile = toml::TomlProfile { codegen_units: Some(13), ..Default::default() }; - overrides.insert(key, o_profile); + packages.insert(key, o_profile); assert_eq!( p, @@ -348,7 +348,7 @@ lto = false panic: Some("abort".to_string()), overflow_checks: Some(true), incremental: Some(true), - overrides: Some(overrides), + package: Some(packages), build_override: Some(Box::new(toml::TomlProfile { opt_level: Some(toml::TomlOptLevel("1".to_string())), codegen_units: Some(11), diff --git a/tests/testsuite/profile_config.rs b/tests/testsuite/profile_config.rs index cfb76d3fe88..f48c46ff99c 100644 --- a/tests/testsuite/profile_config.rs +++ b/tests/testsuite/profile_config.rs @@ -53,7 +53,7 @@ fn profile_config_validate_warnings() { [profile.dev.build-override] bad-key-bo = true - [profile.dev.overrides.bar] + [profile.dev.package.bar] bad-key-bar = true "#, ) @@ -66,7 +66,7 @@ fn profile_config_validate_warnings() { [WARNING] unused key `profile.asdf` in config file `[..].cargo/config` [WARNING] unused key `profile.test` in config file `[..].cargo/config` [WARNING] unused key `profile.dev.bad-key` in config file `[..].cargo/config` -[WARNING] unused key `profile.dev.overrides.bar.bad-key-bar` in config file `[..].cargo/config` +[WARNING] unused key `profile.dev.package.bar.bad-key-bar` in config file `[..].cargo/config` [WARNING] unused key `profile.dev.build-override.bad-key-bo` in config file `[..].cargo/config` [COMPILING] foo [..] [FINISHED] [..] @@ -127,7 +127,7 @@ fn profile_config_validate_errors() { .file( ".cargo/config", r#" - [profile.dev.overrides.foo] + [profile.dev.package.foo] panic = "abort" "#, ) @@ -141,7 +141,7 @@ fn profile_config_validate_errors() { [ERROR] config profile `profile.dev` is not valid Caused by: - `panic` may not be specified in a profile override. + `panic` may not be specified in a `package` profile ", ) .run(); @@ -194,10 +194,10 @@ fn profile_config_override_spec_multiple() { .file( ".cargo/config", r#" - [profile.dev.overrides.bar] + [profile.dev.package.bar] opt-level = 3 - [profile.dev.overrides."bar:0.5.0"] + [profile.dev.package."bar:0.5.0"] opt-level = 3 "#, ) @@ -222,8 +222,8 @@ fn profile_config_override_spec_multiple() { .with_status(101) .with_stderr( "\ -[ERROR] multiple profile overrides in profile `dev` match package `bar v0.5.0 ([..])` -found profile override specs: bar, bar:0.5.0", +[ERROR] multiple package overrides in profile `dev` match package `bar v0.5.0 ([..])` +found package specs: bar, bar:0.5.0", ) .run(); } @@ -291,7 +291,7 @@ fn profile_config_override_precedence() { [profile.dev] codegen-units = 2 - [profile.dev.overrides.bar] + [profile.dev.package.bar] opt-level = 3 "#, ) @@ -310,7 +310,7 @@ fn profile_config_override_precedence() { .file( ".cargo/config", r#" - [profile.dev.overrides.bar] + [profile.dev.package.bar] opt-level = 2 "#, ) @@ -346,7 +346,7 @@ fn profile_config_no_warn_unknown_override() { .file( ".cargo/config", r#" - [profile.dev.overrides.bar] + [profile.dev.package.bar] codegen-units = 4 "#, ) diff --git a/tests/testsuite/profile_custom.rs b/tests/testsuite/profile_custom.rs index 9fcb43f831c..1b95a80aff3 100644 --- a/tests/testsuite/profile_custom.rs +++ b/tests/testsuite/profile_custom.rs @@ -296,16 +296,16 @@ fn overrides_with_custom() { [profile.dev] codegen-units = 7 - [profile.dev.overrides.xxx] + [profile.dev.package.xxx] codegen-units = 5 - [profile.dev.overrides.yyy] + [profile.dev.package.yyy] codegen-units = 3 [profile.other] inherits = "dev" codegen-units = 2 - [profile.other.overrides.yyy] + [profile.other.package.yyy] codegen-units = 6 "#, ) diff --git a/tests/testsuite/profile_overrides.rs b/tests/testsuite/profile_overrides.rs index fd9124ad69b..e0465dc9bc6 100644 --- a/tests/testsuite/profile_overrides.rs +++ b/tests/testsuite/profile_overrides.rs @@ -43,7 +43,7 @@ consider adding `cargo-features = [\"profile-overrides\"]` to the manifest version = "0.0.1" authors = [] - [profile.dev.overrides."*"] + [profile.dev.package."*"] opt-level = 3 "#, ) @@ -85,7 +85,7 @@ fn profile_override_basic() { [profile.dev] opt-level = 1 - [profile.dev.overrides.bar] + [profile.dev.package.bar] opt-level = 3 "#, ) @@ -121,13 +121,13 @@ fn profile_override_warnings() { [dependencies] bar = {path = "bar"} - [profile.dev.overrides.bart] + [profile.dev.package.bart] opt-level = 3 - [profile.dev.overrides.no-suggestion] + [profile.dev.package.no-suggestion] opt-level = 3 - [profile.dev.overrides."bar:1.2.3"] + [profile.dev.package."bar:1.2.3"] opt-level = 3 "#, ) @@ -138,11 +138,11 @@ fn profile_override_warnings() { p.cargo("build").masquerade_as_nightly_cargo().with_stderr_contains( "\ -[WARNING] version or URL in profile override spec `bar:1.2.3` does not match any of the packages: bar v0.5.0 ([..]) -[WARNING] profile override spec `bart` did not match any packages +[WARNING] version or URL in package profile spec `bar:1.2.3` does not match any of the packages: bar v0.5.0 ([..]) +[WARNING] package profile spec `bart` did not match any packages Did you mean `bar`? -[WARNING] profile override spec `no-suggestion` did not match any packages +[WARNING] package profile spec `no-suggestion` did not match any packages [COMPILING] [..] ", ) @@ -154,17 +154,20 @@ fn profile_override_bad_settings() { let bad_values = [ ( "panic = \"abort\"", - "`panic` may not be specified in a profile override.", + "`panic` may not be specified in a `package` profile", ), ( "lto = true", - "`lto` may not be specified in a profile override.", + "`lto` may not be specified in a `package` profile", ), ( "rpath = true", - "`rpath` may not be specified in a profile override.", + "`rpath` may not be specified in a `package` profile", + ), + ( + "overrides = {}", + "package-specific profiles cannot be nested", ), - ("overrides = {}", "Profile overrides cannot be nested."), ]; for &(snippet, expected) in bad_values.iter() { let p = project() @@ -181,7 +184,7 @@ fn profile_override_bad_settings() { [dependencies] bar = {{path = "bar"}} - [profile.dev.overrides.bar] + [profile.dev.package.bar] {} "#, snippet @@ -215,10 +218,10 @@ fn profile_override_hierarchy() { [profile.dev] codegen-units = 1 - [profile.dev.overrides.m2] + [profile.dev.package.m2] codegen-units = 2 - [profile.dev.overrides."*"] + [profile.dev.package."*"] codegen-units = 3 [profile.dev.build-override] @@ -276,10 +279,10 @@ fn profile_override_hierarchy() { // Profiles should be: // m3: 4 (as build.rs dependency) // m3: 1 (as [profile.dev] as workspace member) - // dep: 3 (as [profile.dev.overrides."*"] as non-workspace member) + // dep: 3 (as [profile.dev.package."*"] as non-workspace member) // m1 build.rs: 4 (as [profile.dev.build-override]) - // m2 build.rs: 2 (as [profile.dev.overrides.m2]) - // m2: 2 (as [profile.dev.overrides.m2]) + // m2 build.rs: 2 (as [profile.dev.package.m2]) + // m2: 2 (as [profile.dev.package.m2]) // m1: 1 (as [profile.dev]) p.cargo("build -v").masquerade_as_nightly_cargo().with_stderr_unordered("\ @@ -317,10 +320,10 @@ fn profile_override_spec_multiple() { [dependencies] bar = { path = "bar" } - [profile.dev.overrides.bar] + [profile.dev.package.bar] opt-level = 3 - [profile.dev.overrides."bar:0.5.0"] + [profile.dev.package."bar:0.5.0"] opt-level = 3 "#, ) @@ -334,8 +337,8 @@ fn profile_override_spec_multiple() { .with_status(101) .with_stderr_contains( "\ -[ERROR] multiple profile overrides in profile `dev` match package `bar v0.5.0 ([..])` -found profile override specs: bar, bar:0.5.0", +[ERROR] multiple package overrides in profile `dev` match package `bar v0.5.0 ([..])` +found package specs: bar, bar:0.5.0", ) .run(); } @@ -351,10 +354,10 @@ fn profile_override_spec() { [workspace] members = ["m1", "m2"] - [profile.dev.overrides."dep:1.0.0"] + [profile.dev.package."dep:1.0.0"] codegen-units = 1 - [profile.dev.overrides."dep:2.0.0"] + [profile.dev.package."dep:2.0.0"] codegen-units = 2 "#, ) @@ -471,3 +474,41 @@ fn override_proc_macro() { ) .run(); } + +#[cargo_test] +fn override_package_rename() { + // backwards-compatibility test + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["profile-overrides"] + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + bar = {path = "bar"} + + [profile.dev] + opt-level = 1 + + [profile.dev.overrides.bar] + opt-level = 3 + "#, + ) + .file("src/lib.rs", "") + .file("bar/Cargo.toml", &basic_lib_manifest("bar")) + .file("bar/src/lib.rs", "") + .build(); + + p.cargo("check") + .masquerade_as_nightly_cargo() + .with_stderr("\ +[WARNING] profile key `overrides` has been renamed to `package`, please update the manifest to the new key name +[CHECKING] bar [..] +[CHECKING] foo [..] +[FINISHED] [..] +") + .run(); +}