Skip to content

Commit 3423351

Browse files
committed
Auto merge of #4584 - alexcrichton:beta-next, r=matklad
[beta] Fix [patch] causing updates with a virtual manifest This is a backport of #4583
2 parents 5068cb6 + f0a5263 commit 3423351

File tree

2 files changed

+82
-24
lines changed

2 files changed

+82
-24
lines changed

src/cargo/core/resolver/encode.rs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::str::FromStr;
55
use serde::ser;
66
use serde::de;
77

8-
use core::{Package, PackageId, SourceId, Workspace};
8+
use core::{Package, PackageId, SourceId, Workspace, Dependency};
99
use util::{Graph, Config, internal};
1010
use util::errors::{CargoResult, CargoResultExt, CargoError};
1111

@@ -60,7 +60,12 @@ impl EncodableResolve {
6060
let id = match pkg.source.as_ref().or(path_deps.get(&pkg.name)) {
6161
// We failed to find a local package in the workspace.
6262
// It must have been removed and should be ignored.
63-
None => continue,
63+
None => {
64+
debug!("path dependency now missing {} v{}",
65+
pkg.name,
66+
pkg.version);
67+
continue
68+
}
6469
Some(source) => PackageId::new(&pkg.name, &pkg.version, source)?
6570
};
6671

@@ -199,34 +204,49 @@ fn build_path_deps(ws: &Workspace) -> HashMap<String, SourceId> {
199204
visited.insert(member.package_id().source_id().clone());
200205
}
201206
for member in members.iter() {
202-
build(member, ws.config(), &mut ret, &mut visited);
207+
build_pkg(member, ws.config(), &mut ret, &mut visited);
208+
}
209+
for (_, deps) in ws.root_patch() {
210+
for dep in deps {
211+
build_dep(dep, ws.config(), &mut ret, &mut visited);
212+
}
213+
}
214+
for &(_, ref dep) in ws.root_replace() {
215+
build_dep(dep, ws.config(), &mut ret, &mut visited);
203216
}
204217

205218
return ret;
206219

207-
fn build(pkg: &Package,
208-
config: &Config,
209-
ret: &mut HashMap<String, SourceId>,
210-
visited: &mut HashSet<SourceId>) {
211-
let replace = pkg.manifest().replace().iter().map(|p| &p.1);
212-
let patch = pkg.manifest().patch().values().flat_map(|v| v);
213-
let deps = pkg.dependencies()
214-
.iter()
215-
.chain(replace)
216-
.chain(patch)
217-
.map(|d| d.source_id())
218-
.filter(|id| !visited.contains(id) && id.is_path())
219-
.filter_map(|id| id.url().to_file_path().ok())
220-
.map(|path| path.join("Cargo.toml"))
221-
.filter_map(|path| Package::for_path(&path, config).ok())
222-
.collect::<Vec<_>>();
223-
for pkg in deps {
224-
ret.insert(pkg.name().to_string(),
225-
pkg.package_id().source_id().clone());
226-
visited.insert(pkg.package_id().source_id().clone());
227-
build(&pkg, config, ret, visited);
220+
fn build_pkg(pkg: &Package,
221+
config: &Config,
222+
ret: &mut HashMap<String, SourceId>,
223+
visited: &mut HashSet<SourceId>) {
224+
for dep in pkg.dependencies() {
225+
build_dep(dep, config, ret, visited);
228226
}
229227
}
228+
229+
fn build_dep(dep: &Dependency,
230+
config: &Config,
231+
ret: &mut HashMap<String, SourceId>,
232+
visited: &mut HashSet<SourceId>) {
233+
let id = dep.source_id();
234+
if visited.contains(id) || !id.is_path() {
235+
return
236+
}
237+
let path = match id.url().to_file_path() {
238+
Ok(p) => p.join("Cargo.toml"),
239+
Err(_) => return,
240+
};
241+
let pkg = match Package::for_path(&path, config) {
242+
Ok(p) => p,
243+
Err(_) => return,
244+
};
245+
ret.insert(pkg.name().to_string(),
246+
pkg.package_id().source_id().clone());
247+
visited.insert(pkg.package_id().source_id().clone());
248+
build_pkg(&pkg, config, ret, visited);
249+
}
230250
}
231251

232252
impl Patch {

tests/patch.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,41 @@ Caused by:
742742
to different sources
743743
"));
744744
}
745+
746+
#[test]
747+
fn patch_in_virtual() {
748+
Package::new("foo", "0.1.0").publish();
749+
750+
let p = project("bar")
751+
.file("Cargo.toml", r#"
752+
[workspace]
753+
members = ["bar"]
754+
755+
[patch.crates-io]
756+
foo = { path = "foo" }
757+
"#)
758+
.file("foo/Cargo.toml", r#"
759+
[package]
760+
name = "foo"
761+
version = "0.1.0"
762+
authors = []
763+
"#)
764+
.file("foo/src/lib.rs", r#""#)
765+
.file("bar/Cargo.toml", r#"
766+
[package]
767+
name = "bar"
768+
version = "0.1.0"
769+
authors = []
770+
771+
[dependencies]
772+
foo = "0.1"
773+
"#)
774+
.file("bar/src/lib.rs", r#""#);
775+
776+
assert_that(p.cargo_process("build"),
777+
execs().with_status(0));
778+
assert_that(p.cargo("build"),
779+
execs().with_status(0).with_stderr("\
780+
[FINISHED] [..]
781+
"));
782+
}

0 commit comments

Comments
 (0)