Skip to content

Commit 0dabed9

Browse files
committed
Auto merge of #5291 - matklad:regression-backport, r=matklad
[beta] backport #5288
2 parents d63299b + b9755c0 commit 0dabed9

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ matrix:
3636
- cargo doc --no-deps
3737
- (cd src/doc && mdbook build --dest-dir ../../target/doc)
3838

39-
- before_script: rustup component add rustfmt-preview
40-
script: cargo fmt -- --write-mode diff
41-
4239
exclude:
4340
- rust: stable
4441

src/cargo/ops/resolve.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,30 @@ fn register_previous_locks<'a>(
455455
}
456456

457457
// If we match *anything* in the dependency graph then we consider
458-
// ourselves A-OK and assume that we'll resolve to that. If,
459-
// however, nothing matches, then we poison the source of this
460-
// dependencies and the previous lock file.
458+
// ourselves A-OK and assume that we'll resolve to that.
461459
if resolve.iter().any(|id| dep.matches_ignoring_source(id)) {
462460
continue;
463461
}
462+
463+
// If this dependency didn't match anything special then we may want
464+
// to poison the source as it may have been added. If this path
465+
// dependencies is *not* a workspace member, however, and it's an
466+
// optional/non-transitive dependency then it won't be necessarily
467+
// be in our lock file. If this shows up then we avoid poisoning
468+
// this source as otherwise we'd repeatedly update the registry.
469+
//
470+
// TODO: this breaks adding an optional dependency in a
471+
// non-workspace member and then simultaneously editing the
472+
// dependency on that crate to enable the feature. For now
473+
// this bug is better than the always updating registry
474+
// though...
475+
if !ws.members().any(|pkg| pkg.package_id() == member.package_id()) &&
476+
(dep.is_optional() || !dep.is_transitive()) {
477+
continue
478+
}
479+
480+
// Ok if nothing matches, then we poison the source of this
481+
// dependencies and the previous lock file.
464482
for id in resolve.iter().filter(|id| id.source_id() == source) {
465483
add_deps(resolve, id, &mut avoid_locking);
466484
}

tests/testsuite/freshness.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::io::prelude::*;
44
use cargotest::sleep_ms;
55
use cargotest::support::{execs, project, path2url};
66
use cargotest::support::paths::CargoPathExt;
7+
use cargotest::support::registry::Package;
78
use hamcrest::{assert_that, existing_file};
89

910
#[test]
@@ -1007,3 +1008,60 @@ fn no_rebuild_when_rename_dir() {
10071008
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]"),
10081009
);
10091010
}
1011+
1012+
#[test]
1013+
fn unused_optional_dep() {
1014+
Package::new("registry1", "0.1.0").publish();
1015+
Package::new("registry2", "0.1.0").publish();
1016+
Package::new("registry3", "0.1.0").publish();
1017+
1018+
let p = project("p")
1019+
.file(
1020+
"Cargo.toml",
1021+
r#"
1022+
[package]
1023+
name = "p"
1024+
authors = []
1025+
version = "0.1.0"
1026+
1027+
[dependencies]
1028+
foo = { path = "foo" }
1029+
bar = { path = "bar" }
1030+
registry1 = "*"
1031+
"#,
1032+
)
1033+
.file("src/lib.rs", "")
1034+
.file(
1035+
"foo/Cargo.toml",
1036+
r#"
1037+
[package]
1038+
name = "foo"
1039+
version = "0.1.1"
1040+
authors = []
1041+
1042+
[dev-dependencies]
1043+
registry2 = "*"
1044+
"#,
1045+
)
1046+
.file("foo/src/lib.rs", "")
1047+
.file(
1048+
"bar/Cargo.toml",
1049+
r#"
1050+
[package]
1051+
name = "bar"
1052+
version = "0.1.1"
1053+
authors = []
1054+
1055+
[dependencies]
1056+
registry3 = { version = "*", optional = true }
1057+
"#,
1058+
)
1059+
.file("bar/src/lib.rs", "")
1060+
.build();
1061+
1062+
assert_that(p.cargo("build"), execs().with_status(0));
1063+
assert_that(
1064+
p.cargo("build"),
1065+
execs().with_status(0).with_stderr("[FINISHED] [..]"),
1066+
);
1067+
}

0 commit comments

Comments
 (0)