diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index d0748189b32..280abb19f2a 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -1444,7 +1444,7 @@ fn lines_match_works() { /// You can use `[..]` wildcard in strings (useful for OS-dependent things such /// as paths). You can use a `"{...}"` string literal as a wildcard for /// arbitrary nested JSON (useful for parts of object emitted by other programs -/// (e.g., rustc) rather than Cargo itself). Arrays are sorted before comparison. +/// (e.g., rustc) rather than Cargo itself). pub fn find_json_mismatch(expected: &Value, actual: &Value) -> Result<(), String> { match find_json_mismatch_r(expected, actual) { Some((expected_part, actual_part)) => Err(format!( @@ -1472,26 +1472,10 @@ fn find_json_mismatch_r<'a>( return Some((expected, actual)); } - let mut l = l.iter().collect::>(); - let mut r = r.iter().collect::>(); - - l.retain( - |l| match r.iter().position(|r| find_json_mismatch_r(l, r).is_none()) { - Some(i) => { - r.remove(i); - false - } - None => true, - }, - ); - - if !l.is_empty() { - assert!(!r.is_empty()); - Some((l[0], r[0])) - } else { - assert_eq!(r.len(), 0); - None - } + l.iter() + .zip(r.iter()) + .filter_map(|(l, r)| find_json_mismatch_r(l, r)) + .next() } (&Object(ref l), &Object(ref r)) => { let same_keys = l.len() == r.len() && l.keys().all(|k| r.contains_key(k)); diff --git a/src/cargo/ops/cargo_output_metadata.rs b/src/cargo/ops/cargo_output_metadata.rs index 56def031247..2dc17937843 100644 --- a/src/cargo/ops/cargo_output_metadata.rs +++ b/src/cargo/ops/cargo_output_metadata.rs @@ -6,7 +6,7 @@ use crate::ops::{self, Packages}; use crate::util::CargoResult; use cargo_platform::Platform; use serde::Serialize; -use std::collections::HashMap; +use std::collections::BTreeMap; use std::path::PathBuf; const VERSION: u32 = 1; @@ -130,7 +130,7 @@ fn build_resolve_graph( // Download all Packages. This is needed to serialize the information // for every package. In theory this could honor target filtering, // but that would be somewhat complex. - let mut package_map: HashMap = ws_resolve + let package_map: BTreeMap = ws_resolve .pkg_set .get_many(ws_resolve.pkg_set.package_ids())? .into_iter() @@ -140,7 +140,7 @@ fn build_resolve_graph( // Start from the workspace roots, and recurse through filling out the // map, filtering targets as necessary. - let mut node_map = HashMap::new(); + let mut node_map = BTreeMap::new(); for member_pkg in ws.members() { build_resolve_graph_r( &mut node_map, @@ -153,21 +153,22 @@ fn build_resolve_graph( } // Get a Vec of Packages. let actual_packages = package_map - .drain() + .into_iter() .filter_map(|(pkg_id, pkg)| node_map.get(&pkg_id).map(|_| pkg)) .collect(); + let mr = MetadataResolve { - nodes: node_map.drain().map(|(_pkg_id, node)| node).collect(), + nodes: node_map.into_iter().map(|(_pkg_id, node)| node).collect(), root: ws.current_opt().map(|pkg| pkg.package_id()), }; Ok((actual_packages, mr)) } fn build_resolve_graph_r( - node_map: &mut HashMap, + node_map: &mut BTreeMap, pkg_id: PackageId, resolve: &Resolve, - package_map: &HashMap, + package_map: &BTreeMap, target_data: &RustcTargetData, requested_kinds: &[CompileKind], ) { @@ -189,7 +190,8 @@ fn build_resolve_graph_r( } }) .filter_map(|(dep_id, deps)| { - let dep_kinds: Vec<_> = deps.iter().map(DepKindInfo::from).collect(); + let mut dep_kinds: Vec<_> = deps.iter().map(DepKindInfo::from).collect(); + dep_kinds.sort(); package_map .get(&dep_id) .and_then(|pkg| pkg.targets().iter().find(|t| t.is_lib())) diff --git a/tests/testsuite/alt_registry.rs b/tests/testsuite/alt_registry.rs index e328fef3eba..f9129e284b9 100644 --- a/tests/testsuite/alt_registry.rs +++ b/tests/testsuite/alt_registry.rs @@ -836,6 +836,41 @@ fn alt_reg_metadata() { r#" { "packages": [ + { + "name": "altdep", + "version": "0.0.1", + "id": "altdep 0.0.1 (registry+file:[..]/alternative-registry)", + "license": null, + "license_file": null, + "description": null, + "source": "registry+file:[..]/alternative-registry", + "dependencies": [ + { + "name": "bar", + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "^0.0.1", + "kind": null, + "rename": null, + "optional": false, + "uses_default_features": true, + "features": [], + "target": null, + "registry": null + } + ], + "targets": "{...}", + "features": {}, + "manifest_path": "[..]/altdep-0.0.1/Cargo.toml", + "metadata": null, + "publish": null, + "authors": [], + "categories": [], + "keywords": [], + "readme": null, + "repository": null, + "edition": "2015", + "links": null + }, { "name": "altdep2", "version": "0.0.1", @@ -859,30 +894,17 @@ fn alt_reg_metadata() { "links": null }, { - "name": "altdep", + "name": "bar", "version": "0.0.1", - "id": "altdep 0.0.1 (registry+file:[..]/alternative-registry)", + "id": "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "license": null, "license_file": null, "description": null, - "source": "registry+file:[..]/alternative-registry", - "dependencies": [ - { - "name": "bar", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "req": "^0.0.1", - "kind": null, - "rename": null, - "optional": false, - "uses_default_features": true, - "features": [], - "target": null, - "registry": null - } - ], + "source": "registry+https://github.com/rust-lang/crates.io-index", + "dependencies": [], "targets": "{...}", "features": {}, - "manifest_path": "[..]/altdep-0.0.1/Cargo.toml", + "manifest_path": "[..]/bar-0.0.1/Cargo.toml", "metadata": null, "publish": null, "authors": [], @@ -974,28 +996,6 @@ fn alt_reg_metadata() { "repository": null, "edition": "2015", "links": null - }, - { - "name": "bar", - "version": "0.0.1", - "id": "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "license": null, - "license_file": null, - "description": null, - "source": "registry+https://github.com/rust-lang/crates.io-index", - "dependencies": [], - "targets": "{...}", - "features": {}, - "manifest_path": "[..]/bar-0.0.1/Cargo.toml", - "metadata": null, - "publish": null, - "authors": [], - "categories": [], - "keywords": [], - "readme": null, - "repository": null, - "edition": "2015", - "links": null } ], "workspace_members": [ @@ -1056,14 +1056,27 @@ fn unknown_registry() { { "packages": [ { - "name": "baz", + "name": "bar", "version": "0.0.1", - "id": "baz 0.0.1 (registry+file://[..]/alternative-registry)", + "id": "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "license": null, "license_file": null, "description": null, - "source": "registry+file://[..]/alternative-registry", - "dependencies": [], + "source": "registry+https://github.com/rust-lang/crates.io-index", + "dependencies": [ + { + "name": "baz", + "source": "registry+file://[..]/alternative-registry", + "req": "^0.0.1", + "kind": null, + "rename": null, + "optional": false, + "uses_default_features": true, + "features": [], + "target": null, + "registry": "file:[..]/alternative-registry" + } + ], "targets": "{...}", "features": {}, "manifest_path": "[..]", @@ -1078,30 +1091,17 @@ fn unknown_registry() { "links": null }, { - "name": "foo", + "name": "baz", "version": "0.0.1", - "id": "foo 0.0.1 (path+file://[..]/foo)", + "id": "baz 0.0.1 (registry+file://[..]/alternative-registry)", "license": null, "license_file": null, "description": null, - "source": null, - "dependencies": [ - { - "name": "bar", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "req": "^0.0.1", - "kind": null, - "rename": null, - "optional": false, - "uses_default_features": true, - "features": [], - "target": null, - "registry": null - } - ], + "source": "registry+file://[..]/alternative-registry", + "dependencies": [], "targets": "{...}", "features": {}, - "manifest_path": "[..]/foo/Cargo.toml", + "manifest_path": "[..]", "metadata": null, "publish": null, "authors": [], @@ -1113,17 +1113,17 @@ fn unknown_registry() { "links": null }, { - "name": "bar", + "name": "foo", "version": "0.0.1", - "id": "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "id": "foo 0.0.1 (path+file://[..]/foo)", "license": null, "license_file": null, "description": null, - "source": "registry+https://github.com/rust-lang/crates.io-index", + "source": null, "dependencies": [ { - "name": "baz", - "source": "registry+file://[..]/alternative-registry", + "name": "bar", + "source": "registry+https://github.com/rust-lang/crates.io-index", "req": "^0.0.1", "kind": null, "rename": null, @@ -1131,12 +1131,12 @@ fn unknown_registry() { "uses_default_features": true, "features": [], "target": null, - "registry": "file:[..]/alternative-registry" + "registry": null } ], "targets": "{...}", "features": {}, - "manifest_path": "[..]", + "manifest_path": "[..]/foo/Cargo.toml", "metadata": null, "publish": null, "authors": [], diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index 44e4d6af7b8..22afcf6c826 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -290,6 +290,54 @@ fn cargo_metadata_with_deps_and_version() { r#" { "packages": [ + { + "authors": [], + "categories": [], + "dependencies": [ + { + "features": [], + "kind": null, + "name": "baz", + "optional": false, + "registry": null, + "rename": null, + "req": "^0.0.1", + "source": "registry+https://github.com/rust-lang/crates.io-index", + "target": null, + "uses_default_features": true + } + ], + "description": null, + "edition": "2015", + "features": {}, + "id": "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "keywords": [], + "license": null, + "license_file": null, + "links": null, + "manifest_path": "[..]Cargo.toml", + "metadata": null, + "publish": null, + "name": "bar", + "readme": null, + "repository": null, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "targets": [ + { + "crate_types": [ + "lib" + ], + "doctest": true, + "edition": "2015", + "kind": [ + "lib" + ], + "name": "bar", + "src_path": "[..]src/lib.rs" + } + ], + "version": "0.0.1" + }, { "authors": [], "categories": [], @@ -419,69 +467,34 @@ fn cargo_metadata_with_deps_and_version() { } ], "version": "0.0.1" - }, - { - "authors": [], - "categories": [], - "dependencies": [ - { - "features": [], - "kind": null, - "name": "baz", - "optional": false, - "registry": null, - "rename": null, - "req": "^0.0.1", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "target": null, - "uses_default_features": true - } - ], - "description": null, - "edition": "2015", - "features": {}, - "id": "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "keywords": [], - "license": null, - "license_file": null, - "links": null, - "manifest_path": "[..]Cargo.toml", - "metadata": null, - "publish": null, - "name": "bar", - "readme": null, - "repository": null, - "source": "registry+https://github.com/rust-lang/crates.io-index", - "targets": [ - { - "crate_types": [ - "lib" - ], - "doctest": true, - "edition": "2015", - "kind": [ - "lib" - ], - "name": "bar", - "src_path": "[..]src/lib.rs" - } - ], - "version": "0.0.1" } ], "resolve": { "nodes": [ { - "dependencies": [], - "deps": [], + "dependencies": [ + "baz 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" + ], + "deps": [ + { + "dep_kinds": [ + { + "kind": null, + "target": null + } + ], + "name": "baz", + "pkg": "baz 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" + } + ], "features": [], - "id": "baz 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" + "id": "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" }, { "dependencies": [], "deps": [], "features": [], - "id": "foobar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" + "id": "baz 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" }, { "dependencies": [ @@ -514,23 +527,10 @@ fn cargo_metadata_with_deps_and_version() { "id": "foo 0.5.0 (path+file:[..]foo)" }, { - "dependencies": [ - "baz 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" - ], - "deps": [ - { - "dep_kinds": [ - { - "kind": null, - "target": null - } - ], - "name": "baz", - "pkg": "baz 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" - } - ], + "dependencies": [], + "deps": [], "features": [], - "id": "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" + "id": "foobar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" } ], "root": "foo 0.5.0 (path+file:[..]foo)" @@ -816,20 +816,20 @@ fn workspace_metadata() { "publish": null } ], - "workspace_members": ["baz 0.5.0 (path+file:[..]baz)", "bar 0.5.0 (path+file:[..]bar)"], + "workspace_members": ["bar 0.5.0 (path+file:[..]bar)", "baz 0.5.0 (path+file:[..]baz)"], "resolve": { "nodes": [ { "dependencies": [], "deps": [], "features": [], - "id": "baz 0.5.0 (path+file:[..]baz)" + "id": "bar 0.5.0 (path+file:[..]bar)" }, { "dependencies": [], "deps": [], "features": [], - "id": "bar 0.5.0 (path+file:[..]bar)" + "id": "baz 0.5.0 (path+file:[..]baz)" } ], "root": null @@ -937,7 +937,7 @@ fn workspace_metadata_no_deps() { "publish": null } ], - "workspace_members": ["baz 0.5.0 (path+file:[..]baz)", "bar 0.5.0 (path+file:[..]bar)"], + "workspace_members": ["bar 0.5.0 (path+file:[..]bar)", "baz 0.5.0 (path+file:[..]baz)"], "resolve": null, "target_directory": "[..]foo/target", "version": 1, @@ -1560,36 +1560,11 @@ fn rename_dependency() { { "authors": [], "categories": [], - "dependencies": [ - { - "features": [], - "kind": null, - "name": "bar", - "optional": false, - "rename": null, - "registry": null, - "req": "^0.1.0", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "target": null, - "uses_default_features": true - }, - { - "features": [], - "kind": null, - "name": "bar", - "optional": false, - "rename": "baz", - "registry": null, - "req": "^0.2.0", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "target": null, - "uses_default_features": true - } - ], + "dependencies": [], "description": null, "edition": "2015", "features": {}, - "id": "foo 0.0.1[..]", + "id": "bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "keywords": [], "license": null, "license_file": null, @@ -1597,10 +1572,10 @@ fn rename_dependency() { "manifest_path": "[..]", "metadata": null, "publish": null, - "name": "foo", + "name": "bar", "readme": null, "repository": null, - "source": null, + "source": "registry+https://github.com/rust-lang/crates.io-index", "targets": [ { "crate_types": [ @@ -1611,11 +1586,11 @@ fn rename_dependency() { "kind": [ "lib" ], - "name": "foo", + "name": "bar", "src_path": "[..]" } ], - "version": "0.0.1" + "version": "0.1.0" }, { "authors": [], @@ -1624,7 +1599,7 @@ fn rename_dependency() { "description": null, "edition": "2015", "features": {}, - "id": "bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "id": "bar 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "keywords": [], "license": null, "license_file": null, @@ -1650,16 +1625,41 @@ fn rename_dependency() { "src_path": "[..]" } ], - "version": "0.1.0" + "version": "0.2.0" }, { "authors": [], "categories": [], - "dependencies": [], + "dependencies": [ + { + "features": [], + "kind": null, + "name": "bar", + "optional": false, + "rename": null, + "registry": null, + "req": "^0.1.0", + "source": "registry+https://github.com/rust-lang/crates.io-index", + "target": null, + "uses_default_features": true + }, + { + "features": [], + "kind": null, + "name": "bar", + "optional": false, + "rename": "baz", + "registry": null, + "req": "^0.2.0", + "source": "registry+https://github.com/rust-lang/crates.io-index", + "target": null, + "uses_default_features": true + } + ], "description": null, "edition": "2015", "features": {}, - "id": "bar 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "id": "foo 0.0.1[..]", "keywords": [], "license": null, "license_file": null, @@ -1667,10 +1667,10 @@ fn rename_dependency() { "manifest_path": "[..]", "metadata": null, "publish": null, - "name": "bar", + "name": "foo", "readme": null, "repository": null, - "source": "registry+https://github.com/rust-lang/crates.io-index", + "source": null, "targets": [ { "crate_types": [ @@ -1681,11 +1681,11 @@ fn rename_dependency() { "kind": [ "lib" ], - "name": "bar", + "name": "foo", "src_path": "[..]" } ], - "version": "0.2.0" + "version": "0.0.1" } ], "resolve": { @@ -1694,13 +1694,13 @@ fn rename_dependency() { "dependencies": [], "deps": [], "features": [], - "id": "bar 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" + "id": "bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" }, { "dependencies": [], "deps": [], "features": [], - "id": "bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" + "id": "bar 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" }, { "dependencies": [ @@ -2009,7 +2009,7 @@ fn filter_platform() { "repository": null, "edition": "2015", "links": null - }, + } "#; let cfg_dep = r#" @@ -2047,7 +2047,7 @@ fn filter_platform() { "repository": null, "edition": "2015", "links": null - }, + } "#; let host_dep = r#" @@ -2085,7 +2085,7 @@ fn filter_platform() { "repository": null, "edition": "2015", "links": null - }, + } "#; let normal_dep = r#" @@ -2123,7 +2123,7 @@ fn filter_platform() { "repository": null, "edition": "2015", "links": null - }, + } "#; let foo = r#" @@ -2221,11 +2221,11 @@ fn filter_platform() { &r#" { "packages": [ - $ALT_DEP - $CFG_DEP - $HOST_DEP + $ALT_DEP, + $CFG_DEP, + $FOO, + $HOST_DEP, $NORMAL_DEP - $FOO ], "workspace_members": [ "foo 0.1.0 (path+file:[..]foo)" @@ -2334,9 +2334,9 @@ fn filter_platform() { &r#" { "packages": [ - $ALT_DEP + $ALT_DEP, + $FOO, $NORMAL_DEP - $FOO ], "workspace_members": "{...}", "resolve": { @@ -2406,9 +2406,9 @@ fn filter_platform() { &r#" { "packages": [ - $HOST_DEP + $FOO, + $HOST_DEP, $NORMAL_DEP - $FOO ], "workspace_members": "{...}", "resolve": { @@ -2479,10 +2479,10 @@ fn filter_platform() { &r#" { "packages": [ - $CFG_DEP - $HOST_DEP + $CFG_DEP, + $FOO, + $HOST_DEP, $NORMAL_DEP - $FOO ], "workspace_members": "{...}", "resolve": { @@ -2720,12 +2720,6 @@ fn dep_kinds_workspace() { "metadata": null, "resolve": { "nodes": [ - { - "id": "dep 0.5.0 (path+file://[..]/foo/dep)", - "dependencies": [], - "deps": [], - "features": [] - }, { "id": "bar 0.1.0 (path+file://[..]/foo/bar)", "dependencies": [ @@ -2745,6 +2739,12 @@ fn dep_kinds_workspace() { ], "features": [] }, + { + "id": "dep 0.5.0 (path+file://[..]/foo/dep)", + "dependencies": [], + "deps": [], + "features": [] + }, { "id": "foo 0.1.0 (path+file://[..]/foo)", "dependencies": [ diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index dc5b7841586..5e3919cdc1d 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -433,22 +433,35 @@ fn update_precise_first_run() { { "authors": [], "categories": [], - "dependencies": [], + "dependencies": [ + { + "features": [], + "kind": null, + "name": "serde", + "optional": false, + "registry": null, + "rename": null, + "req": "^0.2", + "source": "registry+https://github.com/rust-lang/crates.io-index", + "target": null, + "uses_default_features": true + } + ], "description": null, "edition": "2015", "features": {}, - "id": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "id": "bar 0.0.1 (path+file://[..]/foo)", "keywords": [], "license": null, "license_file": null, "links": null, - "manifest_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/Cargo.toml", + "manifest_path": "[..]/foo/Cargo.toml", "metadata": null, "publish": null, - "name": "serde", + "name": "bar", "readme": null, "repository": null, - "source": "registry+https://github.com/rust-lang/crates.io-index", + "source": null, "targets": [ { "crate_types": [ @@ -459,44 +472,31 @@ fn update_precise_first_run() { "kind": [ "lib" ], - "name": "serde", - "src_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/src/lib.rs" + "name": "bar", + "src_path": "[..]/foo/src/lib.rs" } ], - "version": "0.2.0" + "version": "0.0.1" }, { "authors": [], "categories": [], - "dependencies": [ - { - "features": [], - "kind": null, - "name": "serde", - "optional": false, - "registry": null, - "rename": null, - "req": "^0.2", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "target": null, - "uses_default_features": true - } - ], + "dependencies": [], "description": null, "edition": "2015", "features": {}, - "id": "bar 0.0.1 (path+file://[..]/foo)", + "id": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "keywords": [], "license": null, "license_file": null, "links": null, - "manifest_path": "[..]/foo/Cargo.toml", + "manifest_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/Cargo.toml", "metadata": null, "publish": null, - "name": "bar", + "name": "serde", "readme": null, "repository": null, - "source": null, + "source": "registry+https://github.com/rust-lang/crates.io-index", "targets": [ { "crate_types": [ @@ -507,11 +507,11 @@ fn update_precise_first_run() { "kind": [ "lib" ], - "name": "bar", - "src_path": "[..]/foo/src/lib.rs" + "name": "serde", + "src_path": "[..]/home/.cargo/registry/src/-[..]/serde-0.2.0/src/lib.rs" } ], - "version": "0.0.1" + "version": "0.2.0" } ], "resolve": {