Skip to content

Commit 3417598

Browse files
authored
Auto merge of #2940 - matklad:ambiguous-deps, r=alexcrichton
Warn about unused keys in dependency specification Closes #2869 and #2695
2 parents 1ceb0c9 + 5010618 commit 3417598

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

src/cargo/util/toml.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,44 @@ impl TomlDependency {
775775
cx.warnings.push(msg);
776776
}
777777

778+
if details.git.is_none() {
779+
let git_only_keys = [
780+
(&details.branch, "branch"),
781+
(&details.tag, "tag"),
782+
(&details.rev, "rev")
783+
];
784+
785+
for &(key, key_name) in git_only_keys.iter() {
786+
if key.is_some() {
787+
let msg = format!("key `{}` is ignored for dependency ({}). \
788+
This will be considered an error in future versions",
789+
key_name, name);
790+
cx.warnings.push(msg)
791+
}
792+
}
793+
}
794+
778795
let new_source_id = match (details.git.as_ref(), details.path.as_ref()) {
779-
(Some(git), _) => {
796+
(Some(git), maybe_path) => {
797+
if maybe_path.is_some() {
798+
let msg = format!("dependency ({}) specification is ambiguous. \
799+
Only one of `git` or `path` is allowed. \
800+
This will be considered an error in future versions", name);
801+
cx.warnings.push(msg)
802+
}
803+
804+
let n_details = [&details.branch, &details.tag, &details.rev]
805+
.iter()
806+
.filter(|d| d.is_some())
807+
.count();
808+
809+
if n_details > 1 {
810+
let msg = format!("dependency ({}) specification is ambiguous. \
811+
Only one of `branch`, `tag` or `rev` is allowed. \
812+
This will be considered an error in future versions", name);
813+
cx.warnings.push(msg)
814+
}
815+
780816
let reference = details.branch.clone().map(GitReference::Branch)
781817
.or_else(|| details.tag.clone().map(GitReference::Tag))
782818
.or_else(|| details.rev.clone().map(GitReference::Rev))

tests/bad-config.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,71 @@ in the future.
513513
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
514514
"));
515515
}
516+
517+
#[test]
518+
fn ambiguous_git_reference() {
519+
let foo = project("foo")
520+
.file("Cargo.toml", r#"
521+
[package]
522+
name = "foo"
523+
version = "0.0.0"
524+
authors = []
525+
526+
[dependencies.bar]
527+
git = "https://example.com"
528+
branch = "master"
529+
tag = "some-tag"
530+
"#)
531+
.file("src/lib.rs", "");
532+
533+
assert_that(foo.cargo_process("build").arg("-v"),
534+
execs().with_stderr_contains("\
535+
[WARNING] dependency (bar) specification is ambiguous. \
536+
Only one of `branch`, `tag` or `rev` is allowed. \
537+
This will be considered an error in future versions
538+
"));
539+
}
540+
541+
#[test]
542+
fn both_git_and_path_specified() {
543+
let foo = project("foo")
544+
.file("Cargo.toml", r#"
545+
[package]
546+
name = "foo"
547+
version = "0.0.0"
548+
authors = []
549+
550+
[dependencies.bar]
551+
git = "https://example.com"
552+
path = "bar"
553+
"#)
554+
.file("src/lib.rs", "");
555+
556+
assert_that(foo.cargo_process("build").arg("-v"),
557+
execs().with_stderr_contains("\
558+
[WARNING] dependency (bar) specification is ambiguous. \
559+
Only one of `git` or `path` is allowed. \
560+
This will be considered an error in future versions
561+
"));
562+
}
563+
564+
#[test]
565+
fn ignored_git_revision() {
566+
let foo = project("foo")
567+
.file("Cargo.toml", r#"
568+
[package]
569+
name = "foo"
570+
version = "0.0.0"
571+
authors = []
572+
573+
[dependencies.bar]
574+
path = "bar"
575+
branch = "spam"
576+
"#)
577+
.file("src/lib.rs", "");
578+
579+
assert_that(foo.cargo_process("build").arg("-v"),
580+
execs().with_stderr_contains("\
581+
[WARNING] key `branch` is ignored for dependency (bar). \
582+
This will be considered an error in future versions"));
583+
}

0 commit comments

Comments
 (0)