From 8d2103f212a6530c844f1d2b698e30b535d2387a Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Thu, 8 Jul 2021 15:25:13 +0800 Subject: [PATCH 1/2] Warning when using features in patch Signed-off-by: hi-rustin --- src/cargo/core/registry.rs | 8 ++ tests/testsuite/patch.rs | 148 +++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) diff --git a/src/cargo/core/registry.rs b/src/cargo/core/registry.rs index 6d921cabb23..35d30eb680e 100644 --- a/src/cargo/core/registry.rs +++ b/src/cargo/core/registry.rs @@ -292,6 +292,14 @@ impl<'cfg> PackageRegistry<'cfg> { dep.package_name() ); + if dep.features().len() != 0 || !dep.uses_default_features() { + self.source_config.config().shell().warn(format!( + "patch for `{}` uses the features mechanism. \ + default-features and features will not take effect because the patch dependency does not support this mechanism", + dep.package_name() + ))?; + } + // Go straight to the source for resolving `dep`. Load it as we // normally would and then ask it directly for the list of summaries // corresponding to this `dep`. diff --git a/tests/testsuite/patch.rs b/tests/testsuite/patch.rs index 24513ee2602..0b6ea8ab82b 100644 --- a/tests/testsuite/patch.rs +++ b/tests/testsuite/patch.rs @@ -787,6 +787,154 @@ fn add_ignored_patch() { .run(); } +#[cargo_test] +fn add_patch_with_features() { + Package::new("bar", "0.1.0").publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies] + bar = "0.1.0" + "#, + ) + .file("src/lib.rs", "") + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/lib.rs", r#""#) + .build(); + + p.cargo("build") + .with_stderr( + "\ +[UPDATING] `[ROOT][..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] bar v0.1.0 [..] +[COMPILING] bar v0.1.0 +[COMPILING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); + p.cargo("build").with_stderr("[FINISHED] [..]").run(); + + p.change_file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies] + bar = "0.1.0" + + [patch.crates-io] + bar = { path = 'bar', features = ["some_feature"] } + "#, + ); + + p.cargo("build") + .with_stderr( + "\ +[WARNING] patch for `bar` uses the features mechanism. \ +default-features and features will not take effect because the patch dependency does not support this mechanism +[COMPILING] bar v0.1.0 ([CWD]/bar) +[COMPILING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); + p.cargo("build") + .with_stderr( + "\ +[WARNING] patch for `bar` uses the features mechanism. \ +default-features and features will not take effect because the patch dependency does not support this mechanism +[FINISHED] [..] +", + ) + .run(); +} + +#[cargo_test] +fn add_patch_with_setting_default_features() { + Package::new("bar", "0.1.0").publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies] + bar = "0.1.0" + "#, + ) + .file("src/lib.rs", "") + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/lib.rs", r#""#) + .build(); + + p.cargo("build") + .with_stderr( + "\ +[UPDATING] `[ROOT][..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] bar v0.1.0 [..] +[COMPILING] bar v0.1.0 +[COMPILING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); + p.cargo("build").with_stderr("[FINISHED] [..]").run(); + + p.change_file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies] + bar = "0.1.0" + + [patch.crates-io] + bar = { path = 'bar', default-features = false, features = ["none_default_feature"] } + "#, + ); + + p.cargo("build") + .with_stderr( + "\ +[WARNING] patch for `bar` uses the features mechanism. \ +default-features and features will not take effect because the patch dependency does not support this mechanism +[COMPILING] bar v0.1.0 ([CWD]/bar) +[COMPILING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); + p.cargo("build") + .with_stderr( + "\ +[WARNING] patch for `bar` uses the features mechanism. \ +default-features and features will not take effect because the patch dependency does not support this mechanism +[FINISHED] [..] +", + ) + .run(); +} + #[cargo_test] fn no_warn_ws_patch() { Package::new("c", "0.1.0").publish(); From a846226f7180253bf0446463aa4ea8fe989e8e24 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Fri, 9 Jul 2021 13:15:13 +0800 Subject: [PATCH 2/2] Remove unrelated cases Signed-off-by: hi-rustin --- tests/testsuite/patch.rs | 76 +++++++--------------------------------- 1 file changed, 12 insertions(+), 64 deletions(-) diff --git a/tests/testsuite/patch.rs b/tests/testsuite/patch.rs index 0b6ea8ab82b..996bc7768ee 100644 --- a/tests/testsuite/patch.rs +++ b/tests/testsuite/patch.rs @@ -795,37 +795,6 @@ fn add_patch_with_features() { .file( "Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - authors = [] - - [dependencies] - bar = "0.1.0" - "#, - ) - .file("src/lib.rs", "") - .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) - .file("bar/src/lib.rs", r#""#) - .build(); - - p.cargo("build") - .with_stderr( - "\ -[UPDATING] `[ROOT][..]` index -[DOWNLOADING] crates ... -[DOWNLOADED] bar v0.1.0 [..] -[COMPILING] bar v0.1.0 -[COMPILING] foo v0.0.1 ([CWD]) -[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] -", - ) - .run(); - p.cargo("build").with_stderr("[FINISHED] [..]").run(); - - p.change_file( - "Cargo.toml", - r#" [package] name = "foo" version = "0.0.1" @@ -837,13 +806,18 @@ fn add_patch_with_features() { [patch.crates-io] bar = { path = 'bar', features = ["some_feature"] } "#, - ); + ) + .file("src/lib.rs", "") + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/lib.rs", r#""#) + .build(); p.cargo("build") .with_stderr( "\ [WARNING] patch for `bar` uses the features mechanism. \ default-features and features will not take effect because the patch dependency does not support this mechanism +[UPDATING] `[ROOT][..]` index [COMPILING] bar v0.1.0 ([CWD]/bar) [COMPILING] foo v0.0.1 ([CWD]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] @@ -869,37 +843,6 @@ fn add_patch_with_setting_default_features() { .file( "Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - authors = [] - - [dependencies] - bar = "0.1.0" - "#, - ) - .file("src/lib.rs", "") - .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) - .file("bar/src/lib.rs", r#""#) - .build(); - - p.cargo("build") - .with_stderr( - "\ -[UPDATING] `[ROOT][..]` index -[DOWNLOADING] crates ... -[DOWNLOADED] bar v0.1.0 [..] -[COMPILING] bar v0.1.0 -[COMPILING] foo v0.0.1 ([CWD]) -[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] -", - ) - .run(); - p.cargo("build").with_stderr("[FINISHED] [..]").run(); - - p.change_file( - "Cargo.toml", - r#" [package] name = "foo" version = "0.0.1" @@ -911,13 +854,18 @@ fn add_patch_with_setting_default_features() { [patch.crates-io] bar = { path = 'bar', default-features = false, features = ["none_default_feature"] } "#, - ); + ) + .file("src/lib.rs", "") + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/lib.rs", r#""#) + .build(); p.cargo("build") .with_stderr( "\ [WARNING] patch for `bar` uses the features mechanism. \ default-features and features will not take effect because the patch dependency does not support this mechanism +[UPDATING] `[ROOT][..]` index [COMPILING] bar v0.1.0 ([CWD]/bar) [COMPILING] foo v0.0.1 ([CWD]) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]