Skip to content

Commit 8d2103f

Browse files
committed
Warning when using features in patch
Signed-off-by: hi-rustin <[email protected]>
1 parent cc80b40 commit 8d2103f

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

src/cargo/core/registry.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ impl<'cfg> PackageRegistry<'cfg> {
292292
dep.package_name()
293293
);
294294

295+
if dep.features().len() != 0 || !dep.uses_default_features() {
296+
self.source_config.config().shell().warn(format!(
297+
"patch for `{}` uses the features mechanism. \
298+
default-features and features will not take effect because the patch dependency does not support this mechanism",
299+
dep.package_name()
300+
))?;
301+
}
302+
295303
// Go straight to the source for resolving `dep`. Load it as we
296304
// normally would and then ask it directly for the list of summaries
297305
// corresponding to this `dep`.

tests/testsuite/patch.rs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,154 @@ fn add_ignored_patch() {
787787
.run();
788788
}
789789

790+
#[cargo_test]
791+
fn add_patch_with_features() {
792+
Package::new("bar", "0.1.0").publish();
793+
794+
let p = project()
795+
.file(
796+
"Cargo.toml",
797+
r#"
798+
[package]
799+
name = "foo"
800+
version = "0.0.1"
801+
authors = []
802+
803+
[dependencies]
804+
bar = "0.1.0"
805+
"#,
806+
)
807+
.file("src/lib.rs", "")
808+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
809+
.file("bar/src/lib.rs", r#""#)
810+
.build();
811+
812+
p.cargo("build")
813+
.with_stderr(
814+
"\
815+
[UPDATING] `[ROOT][..]` index
816+
[DOWNLOADING] crates ...
817+
[DOWNLOADED] bar v0.1.0 [..]
818+
[COMPILING] bar v0.1.0
819+
[COMPILING] foo v0.0.1 ([CWD])
820+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
821+
",
822+
)
823+
.run();
824+
p.cargo("build").with_stderr("[FINISHED] [..]").run();
825+
826+
p.change_file(
827+
"Cargo.toml",
828+
r#"
829+
[package]
830+
name = "foo"
831+
version = "0.0.1"
832+
authors = []
833+
834+
[dependencies]
835+
bar = "0.1.0"
836+
837+
[patch.crates-io]
838+
bar = { path = 'bar', features = ["some_feature"] }
839+
"#,
840+
);
841+
842+
p.cargo("build")
843+
.with_stderr(
844+
"\
845+
[WARNING] patch for `bar` uses the features mechanism. \
846+
default-features and features will not take effect because the patch dependency does not support this mechanism
847+
[COMPILING] bar v0.1.0 ([CWD]/bar)
848+
[COMPILING] foo v0.0.1 ([CWD])
849+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
850+
",
851+
)
852+
.run();
853+
p.cargo("build")
854+
.with_stderr(
855+
"\
856+
[WARNING] patch for `bar` uses the features mechanism. \
857+
default-features and features will not take effect because the patch dependency does not support this mechanism
858+
[FINISHED] [..]
859+
",
860+
)
861+
.run();
862+
}
863+
864+
#[cargo_test]
865+
fn add_patch_with_setting_default_features() {
866+
Package::new("bar", "0.1.0").publish();
867+
868+
let p = project()
869+
.file(
870+
"Cargo.toml",
871+
r#"
872+
[package]
873+
name = "foo"
874+
version = "0.0.1"
875+
authors = []
876+
877+
[dependencies]
878+
bar = "0.1.0"
879+
"#,
880+
)
881+
.file("src/lib.rs", "")
882+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
883+
.file("bar/src/lib.rs", r#""#)
884+
.build();
885+
886+
p.cargo("build")
887+
.with_stderr(
888+
"\
889+
[UPDATING] `[ROOT][..]` index
890+
[DOWNLOADING] crates ...
891+
[DOWNLOADED] bar v0.1.0 [..]
892+
[COMPILING] bar v0.1.0
893+
[COMPILING] foo v0.0.1 ([CWD])
894+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
895+
",
896+
)
897+
.run();
898+
p.cargo("build").with_stderr("[FINISHED] [..]").run();
899+
900+
p.change_file(
901+
"Cargo.toml",
902+
r#"
903+
[package]
904+
name = "foo"
905+
version = "0.0.1"
906+
authors = []
907+
908+
[dependencies]
909+
bar = "0.1.0"
910+
911+
[patch.crates-io]
912+
bar = { path = 'bar', default-features = false, features = ["none_default_feature"] }
913+
"#,
914+
);
915+
916+
p.cargo("build")
917+
.with_stderr(
918+
"\
919+
[WARNING] patch for `bar` uses the features mechanism. \
920+
default-features and features will not take effect because the patch dependency does not support this mechanism
921+
[COMPILING] bar v0.1.0 ([CWD]/bar)
922+
[COMPILING] foo v0.0.1 ([CWD])
923+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
924+
",
925+
)
926+
.run();
927+
p.cargo("build")
928+
.with_stderr(
929+
"\
930+
[WARNING] patch for `bar` uses the features mechanism. \
931+
default-features and features will not take effect because the patch dependency does not support this mechanism
932+
[FINISHED] [..]
933+
",
934+
)
935+
.run();
936+
}
937+
790938
#[cargo_test]
791939
fn no_warn_ws_patch() {
792940
Package::new("c", "0.1.0").publish();

0 commit comments

Comments
 (0)