Skip to content

Commit 3249786

Browse files
authored
Clarify warning for using features or default-features in patch (#15953)
### What does this PR try to resolve? When attempting to substitute a local version of a dependency, since the `patch` section uses syntax similar to a dependency (and the documentation even says "The syntax is similar to the `[dependencies]` section", it's natural to assume that other things from `[dependencies]` also work, such as `features` or `default-features`. Attempting to use them produces this warning: > patch for `crate-name-here` uses the features mechanism. > default-features and features will not take effect because the patch dependency does not support this mechanism The phrasing "the patch dependency does not support this mechanism" makes it seem at first glance like `patch` just doesn't support this at all. But the actual problem is that the user needs to move the `features`/`default-features` keys to an entry in `dependencies` instead. Closes #13522 ### How to test and review this PR? This applies the review feedback from #13522 that the author didn't have a chance to get back to.
2 parents 5b7c4e3 + 5030f23 commit 3249786

File tree

4 files changed

+71
-31
lines changed

4 files changed

+71
-31
lines changed

src/cargo/core/registry.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,23 @@ impl<'gctx> PackageRegistry<'gctx> {
379379
dep.package_name()
380380
);
381381

382-
if dep.features().len() != 0 || !dep.uses_default_features() {
383-
self.source_config.gctx().shell().warn(format!(
384-
"patch for `{}` uses the features mechanism. \
385-
default-features and features will not take effect because the patch dependency does not support this mechanism",
386-
dep.package_name()
382+
let mut unused_fields = Vec::new();
383+
if dep.features().len() != 0 {
384+
unused_fields.push("`features`");
385+
}
386+
if !dep.uses_default_features() {
387+
unused_fields.push("`default-features`")
388+
}
389+
if !unused_fields.is_empty() {
390+
let mut shell = self.source_config.gctx().shell();
391+
shell.warn(format!(
392+
"unused field in patch for `{}`: {}",
393+
dep.package_name(),
394+
unused_fields.join(", ")
395+
))?;
396+
shell.note(format!(
397+
"configure {} in the `dependencies` entry",
398+
unused_fields.join(", ")
387399
))?;
388400
}
389401

src/cargo/ops/resolve.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,24 @@ pub fn resolve_ws_with_opts<'gctx>(
209209
.warn(format!("package replacement is not used: {}", replace_spec))?
210210
}
211211

212-
if dep.features().len() != 0 || !dep.uses_default_features() {
213-
ws.gctx()
214-
.shell()
215-
.warn(format!(
216-
"replacement for `{}` uses the features mechanism. \
217-
default-features and features will not take effect because the replacement dependency does not support this mechanism",
218-
dep.package_name()
219-
))?
212+
let mut unused_fields = Vec::new();
213+
if dep.features().len() != 0 {
214+
unused_fields.push("`features`");
215+
}
216+
if !dep.uses_default_features() {
217+
unused_fields.push("`default-features`")
218+
}
219+
if !unused_fields.is_empty() {
220+
let mut shell = ws.gctx().shell();
221+
shell.warn(format!(
222+
"unused field in replacement for `{}`: {}",
223+
dep.package_name(),
224+
unused_fields.join(", ")
225+
))?;
226+
shell.note(format!(
227+
"configure {} in the `dependencies` entry",
228+
unused_fields.join(", ")
229+
))?;
220230
}
221231
}
222232

tests/testsuite/patch.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -928,20 +928,26 @@ fn add_patch_with_features() {
928928
.file("bar/src/lib.rs", r#""#)
929929
.build();
930930

931-
p.cargo("check").with_stderr_data(str![[r#"
932-
[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
931+
p.cargo("check")
932+
.with_stderr_data(str![[r#"
933+
[WARNING] unused field in patch for `bar`: `features`
934+
[NOTE] configure `features` in the `dependencies` entry
933935
[UPDATING] `dummy-registry` index
934936
[LOCKING] 1 package to latest compatible version
935937
[CHECKING] bar v0.1.0 ([ROOT]/foo/bar)
936938
[CHECKING] foo v0.0.1 ([ROOT]/foo)
937939
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
938940
939-
"#]]).run();
940-
p.cargo("check").with_stderr_data(str![[r#"
941-
[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
941+
"#]])
942+
.run();
943+
p.cargo("check")
944+
.with_stderr_data(str![[r#"
945+
[WARNING] unused field in patch for `bar`: `features`
946+
[NOTE] configure `features` in the `dependencies` entry
942947
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
943948
944-
"#]]).run();
949+
"#]])
950+
.run();
945951
}
946952

947953
#[cargo_test]
@@ -970,20 +976,26 @@ fn add_patch_with_setting_default_features() {
970976
.file("bar/src/lib.rs", r#""#)
971977
.build();
972978

973-
p.cargo("check").with_stderr_data(str![[r#"
974-
[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
979+
p.cargo("check")
980+
.with_stderr_data(str![[r#"
981+
[WARNING] unused field in patch for `bar`: `features`, `default-features`
982+
[NOTE] configure `features`, `default-features` in the `dependencies` entry
975983
[UPDATING] `dummy-registry` index
976984
[LOCKING] 1 package to latest compatible version
977985
[CHECKING] bar v0.1.0 ([ROOT]/foo/bar)
978986
[CHECKING] foo v0.0.1 ([ROOT]/foo)
979987
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
980988
981-
"#]]).run();
982-
p.cargo("check").with_stderr_data(str![[r#"
983-
[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
989+
"#]])
990+
.run();
991+
p.cargo("check")
992+
.with_stderr_data(str![[r#"
993+
[WARNING] unused field in patch for `bar`: `features`, `default-features`
994+
[NOTE] configure `features`, `default-features` in the `dependencies` entry
984995
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
985996
986-
"#]]).run();
997+
"#]])
998+
.run();
987999
}
9881000

9891001
#[cargo_test]

tests/testsuite/replace.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,19 @@ fn override_with_features() {
8989
)
9090
.build();
9191

92-
p.cargo("check").with_stderr_data(str![[r#"
92+
p.cargo("check")
93+
.with_stderr_data(str![[r#"
9394
[UPDATING] `dummy-registry` index
9495
[UPDATING] git repository `[ROOTURL]/override`
9596
[LOCKING] 2 packages to latest compatible versions
96-
[WARNING] replacement for `bar` uses the features mechanism. default-features and features will not take effect because the replacement dependency does not support this mechanism
97+
[WARNING] unused field in replacement for `bar`: `features`
98+
[NOTE] configure `features` in the `dependencies` entry
9799
[CHECKING] bar v0.1.0 ([ROOTURL]/override#[..])
98100
[CHECKING] foo v0.0.1 ([ROOT]/foo)
99101
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
100102
101-
"#]]).run();
103+
"#]])
104+
.run();
102105
}
103106

104107
#[cargo_test]
@@ -136,16 +139,19 @@ fn override_with_setting_default_features() {
136139
)
137140
.build();
138141

139-
p.cargo("check").with_stderr_data(str![[r#"
142+
p.cargo("check")
143+
.with_stderr_data(str![[r#"
140144
[UPDATING] `dummy-registry` index
141145
[UPDATING] git repository `[ROOTURL]/override`
142146
[LOCKING] 2 packages to latest compatible versions
143-
[WARNING] replacement for `bar` uses the features mechanism. default-features and features will not take effect because the replacement dependency does not support this mechanism
147+
[WARNING] unused field in replacement for `bar`: `features`, `default-features`
148+
[NOTE] configure `features`, `default-features` in the `dependencies` entry
144149
[CHECKING] bar v0.1.0 ([ROOTURL]/override#[..])
145150
[CHECKING] foo v0.0.1 ([ROOT]/foo)
146151
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
147152
148-
"#]]).run();
153+
"#]])
154+
.run();
149155
}
150156

151157
#[cargo_test]

0 commit comments

Comments
 (0)