Skip to content

Commit 0e79808

Browse files
committed
refactor(metadata): Switch opaque ID from PackageID to PackageIDSpec
1 parent 9787229 commit 0e79808

File tree

13 files changed

+324
-304
lines changed

13 files changed

+324
-304
lines changed

src/cargo/core/package.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
2121
use crate::core::dependency::DepKind;
2222
use crate::core::resolver::features::ForceAllTargets;
2323
use crate::core::resolver::{HasDevUnits, Resolve};
24-
use crate::core::{Dependency, Manifest, PackageId, SourceId, Target};
24+
use crate::core::{Dependency, Manifest, PackageId, PackageIdSpec, SourceId, Target};
2525
use crate::core::{Summary, Workspace};
2626
use crate::sources::source::{MaybePackage, SourceMap};
2727
use crate::util::cache_lock::{CacheLock, CacheLockMode};
@@ -82,7 +82,7 @@ impl PartialOrd for Package {
8282
pub struct SerializedPackage {
8383
name: InternedString,
8484
version: Version,
85-
id: PackageId,
85+
id: PackageIdSpec,
8686
license: Option<String>,
8787
license_file: Option<String>,
8888
description: Option<String>,
@@ -239,7 +239,7 @@ impl Package {
239239
SerializedPackage {
240240
name: package_id.name(),
241241
version: package_id.version().clone(),
242-
id: package_id,
242+
id: PackageIdSpec::from_package_id(package_id),
243243
license: manmeta.license.clone(),
244244
license_file: manmeta.license_file.clone(),
245245
description: manmeta.description.clone(),

src/cargo/ops/cargo_output_metadata.rs

+31-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
33
use crate::core::dependency::DepKind;
44
use crate::core::package::SerializedPackage;
55
use crate::core::resolver::{features::CliFeatures, HasDevUnits, Resolve};
6-
use crate::core::{Package, PackageId, Workspace};
6+
use crate::core::{Package, PackageId, PackageIdSpec, Workspace};
77
use crate::ops::{self, Packages};
88
use crate::util::interning::InternedString;
99
use crate::util::CargoResult;
@@ -42,8 +42,14 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo
4242

4343
Ok(ExportInfo {
4444
packages,
45-
workspace_members: ws.members().map(|pkg| pkg.package_id()).collect(),
46-
workspace_default_members: ws.default_members().map(|pkg| pkg.package_id()).collect(),
45+
workspace_members: ws
46+
.members()
47+
.map(|pkg| PackageIdSpec::from_package_id(pkg.package_id()))
48+
.collect(),
49+
workspace_default_members: ws
50+
.default_members()
51+
.map(|pkg| PackageIdSpec::from_package_id(pkg.package_id()))
52+
.collect(),
4753
resolve,
4854
target_directory: ws.target_dir().into_path_unlocked(),
4955
version: VERSION,
@@ -58,8 +64,8 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo
5864
#[derive(Serialize)]
5965
pub struct ExportInfo {
6066
packages: Vec<SerializedPackage>,
61-
workspace_members: Vec<PackageId>,
62-
workspace_default_members: Vec<PackageId>,
67+
workspace_members: Vec<PackageIdSpec>,
68+
workspace_default_members: Vec<PackageIdSpec>,
6369
resolve: Option<MetadataResolve>,
6470
target_directory: PathBuf,
6571
version: u32,
@@ -70,13 +76,13 @@ pub struct ExportInfo {
7076
#[derive(Serialize)]
7177
struct MetadataResolve {
7278
nodes: Vec<MetadataResolveNode>,
73-
root: Option<PackageId>,
79+
root: Option<PackageIdSpec>,
7480
}
7581

7682
#[derive(Serialize)]
7783
struct MetadataResolveNode {
78-
id: PackageId,
79-
dependencies: Vec<PackageId>,
84+
id: PackageIdSpec,
85+
dependencies: Vec<PackageIdSpec>,
8086
deps: Vec<Dep>,
8187
features: Vec<InternedString>,
8288
}
@@ -86,7 +92,9 @@ struct Dep {
8692
// TODO(bindeps): after -Zbindeps gets stabilized,
8793
// mark this field as deprecated in the help manual of cargo-metadata
8894
name: InternedString,
89-
pkg: PackageId,
95+
pkg: PackageIdSpec,
96+
#[serde(skip)]
97+
pkg_id: PackageId,
9098
dep_kinds: Vec<DepKindInfo>,
9199
}
92100

@@ -179,7 +187,9 @@ fn build_resolve_graph(
179187

180188
let mr = MetadataResolve {
181189
nodes: node_map.into_iter().map(|(_pkg_id, node)| node).collect(),
182-
root: ws.current_opt().map(|pkg| pkg.package_id()),
190+
root: ws
191+
.current_opt()
192+
.map(|pkg| PackageIdSpec::from_package_id(pkg.package_id())),
183193
};
184194
Ok((actual_packages, mr))
185195
}
@@ -301,18 +311,20 @@ fn build_resolve_graph_r(
301311

302312
dep_kinds.sort();
303313

304-
let pkg = normalize_id(dep_id);
314+
let pkg_id = normalize_id(dep_id);
305315

306316
let dep = match (lib_target, dep_kinds.len()) {
307317
(Some(target), _) => Dep {
308318
name: extern_name(target)?,
309-
pkg,
319+
pkg: PackageIdSpec::from_package_id(pkg_id.clone()),
320+
pkg_id,
310321
dep_kinds,
311322
},
312323
// No lib target exists but contains artifact deps.
313324
(None, 1..) => Dep {
314325
name: InternedString::new(""),
315-
pkg,
326+
pkg: PackageIdSpec::from_package_id(pkg_id.clone()),
327+
pkg_id,
316328
dep_kinds,
317329
},
318330
// No lib or artifact dep exists.
@@ -325,11 +337,13 @@ fn build_resolve_graph_r(
325337
dep_metadatas
326338
};
327339

328-
let dumb_deps: Vec<PackageId> = deps.iter().map(|dep| dep.pkg).collect();
329-
let to_visit = dumb_deps.clone();
340+
let to_visit: Vec<PackageId> = deps.iter().map(|dep| dep.pkg_id).collect();
330341
let node = MetadataResolveNode {
331-
id: normalize_id(pkg_id),
332-
dependencies: dumb_deps,
342+
id: PackageIdSpec::from_package_id(normalize_id(pkg_id)),
343+
dependencies: to_visit
344+
.iter()
345+
.map(|id| PackageIdSpec::from_package_id(id.clone()))
346+
.collect(),
333347
deps,
334348
features,
335349
};

src/doc/man/cargo-metadata.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ The JSON output has the following format:
5656
/* The Package ID, an opaque and unique identifier for referring to the
5757
package. See "Compatibility" above for the stability guarantee.
5858
*/
59-
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
59+
"id": "file:///path/to/my-package#0.1.0",
6060
/* The license value from the manifest, or null. */
6161
"license": "MIT/Apache-2.0",
6262
/* The license-file value from the manifest, or null. */
@@ -242,13 +242,13 @@ The JSON output has the following format:
242242
Each entry is the Package ID for the package.
243243
*/
244244
"workspace_members": [
245-
"my-package 0.1.0 (path+file:///path/to/my-package)",
245+
"file:///path/to/my-package#0.1.0",
246246
],
247247
/* Array of default members of the workspace.
248248
Each entry is the Package ID for the package.
249249
*/
250250
"workspace_default_members": [
251-
"my-package 0.1.0 (path+file:///path/to/my-package)",
251+
"file:///path/to/my-package#0.1.0",
252252
],
253253
// The resolved dependency graph for the entire workspace. The enabled
254254
// features are based on the enabled features for the "current" package.
@@ -266,10 +266,10 @@ The JSON output has the following format:
266266
"nodes": [
267267
{
268268
/* The Package ID of this node. */
269-
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
269+
"id": "file:///path/to/my-package#0.1.0",
270270
/* The dependencies of this package, an array of Package IDs. */
271271
"dependencies": [
272-
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
272+
"https://github.com/rust-lang/crates.io-index#[email protected]"
273273
],
274274
/* The dependencies of this package. This is an alternative to
275275
"dependencies" which contains additional information. In
@@ -283,7 +283,7 @@ The JSON output has the following format:
283283
*/
284284
"name": "bitflags",
285285
/* The Package ID of the dependency. */
286-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
286+
"pkg": "https://github.com/rust-lang/crates.io-index#[email protected]"
287287
/* Array of dependency kinds. Added in Cargo 1.40. */
288288
"dep_kinds": [
289289
{
@@ -309,7 +309,7 @@ The JSON output has the following format:
309309
This is null if this is a virtual workspace. Otherwise it is
310310
the Package ID of the root package.
311311
*/
312-
"root": "my-package 0.1.0 (path+file:///path/to/my-package)"
312+
"root": "file:///path/to/my-package#0.1.0",
313313
},
314314
/* The absolute path to the build directory where Cargo places its output. */
315315
"target_directory": "/path/to/my-package/target",

src/doc/man/generated_txt/cargo-metadata.txt

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ OUTPUT FORMAT
5252
/* The Package ID, an opaque and unique identifier for referring to the
5353
package. See "Compatibility" above for the stability guarantee.
5454
*/
55-
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
55+
"id": "file:///path/to/my-package#0.1.0",
5656
/* The license value from the manifest, or null. */
5757
"license": "MIT/Apache-2.0",
5858
/* The license-file value from the manifest, or null. */
@@ -238,13 +238,13 @@ OUTPUT FORMAT
238238
Each entry is the Package ID for the package.
239239
*/
240240
"workspace_members": [
241-
"my-package 0.1.0 (path+file:///path/to/my-package)",
241+
"file:///path/to/my-package#0.1.0",
242242
],
243243
/* Array of default members of the workspace.
244244
Each entry is the Package ID for the package.
245245
*/
246246
"workspace_default_members": [
247-
"my-package 0.1.0 (path+file:///path/to/my-package)",
247+
"file:///path/to/my-package#0.1.0",
248248
],
249249
// The resolved dependency graph for the entire workspace. The enabled
250250
// features are based on the enabled features for the "current" package.
@@ -262,10 +262,10 @@ OUTPUT FORMAT
262262
"nodes": [
263263
{
264264
/* The Package ID of this node. */
265-
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
265+
"id": "file:///path/to/my-package#0.1.0",
266266
/* The dependencies of this package, an array of Package IDs. */
267267
"dependencies": [
268-
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
268+
"https://github.com/rust-lang/crates.io-index#[email protected]"
269269
],
270270
/* The dependencies of this package. This is an alternative to
271271
"dependencies" which contains additional information. In
@@ -279,7 +279,7 @@ OUTPUT FORMAT
279279
*/
280280
"name": "bitflags",
281281
/* The Package ID of the dependency. */
282-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
282+
"pkg": "https://github.com/rust-lang/crates.io-index#[email protected]"
283283
/* Array of dependency kinds. Added in Cargo 1.40. */
284284
"dep_kinds": [
285285
{
@@ -305,7 +305,7 @@ OUTPUT FORMAT
305305
This is null if this is a virtual workspace. Otherwise it is
306306
the Package ID of the root package.
307307
*/
308-
"root": "my-package 0.1.0 (path+file:///path/to/my-package)"
308+
"root": "file:///path/to/my-package#0.1.0",
309309
},
310310
/* The absolute path to the build directory where Cargo places its output. */
311311
"target_directory": "/path/to/my-package/target",

src/doc/src/commands/cargo-metadata.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ The JSON output has the following format:
5656
/* The Package ID, an opaque and unique identifier for referring to the
5757
package. See "Compatibility" above for the stability guarantee.
5858
*/
59-
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
59+
"id": "file:///path/to/my-package#0.1.0",
6060
/* The license value from the manifest, or null. */
6161
"license": "MIT/Apache-2.0",
6262
/* The license-file value from the manifest, or null. */
@@ -242,13 +242,13 @@ The JSON output has the following format:
242242
Each entry is the Package ID for the package.
243243
*/
244244
"workspace_members": [
245-
"my-package 0.1.0 (path+file:///path/to/my-package)",
245+
"file:///path/to/my-package#0.1.0",
246246
],
247247
/* Array of default members of the workspace.
248248
Each entry is the Package ID for the package.
249249
*/
250250
"workspace_default_members": [
251-
"my-package 0.1.0 (path+file:///path/to/my-package)",
251+
"file:///path/to/my-package#0.1.0",
252252
],
253253
// The resolved dependency graph for the entire workspace. The enabled
254254
// features are based on the enabled features for the "current" package.
@@ -266,10 +266,10 @@ The JSON output has the following format:
266266
"nodes": [
267267
{
268268
/* The Package ID of this node. */
269-
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
269+
"id": "file:///path/to/my-package#0.1.0",
270270
/* The dependencies of this package, an array of Package IDs. */
271271
"dependencies": [
272-
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
272+
"https://github.com/rust-lang/crates.io-index#[email protected]"
273273
],
274274
/* The dependencies of this package. This is an alternative to
275275
"dependencies" which contains additional information. In
@@ -283,7 +283,7 @@ The JSON output has the following format:
283283
*/
284284
"name": "bitflags",
285285
/* The Package ID of the dependency. */
286-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
286+
"pkg": "https://github.com/rust-lang/crates.io-index#[email protected]"
287287
/* Array of dependency kinds. Added in Cargo 1.40. */
288288
"dep_kinds": [
289289
{
@@ -309,7 +309,7 @@ The JSON output has the following format:
309309
This is null if this is a virtual workspace. Otherwise it is
310310
the Package ID of the root package.
311311
*/
312-
"root": "my-package 0.1.0 (path+file:///path/to/my-package)"
312+
"root": "file:///path/to/my-package#0.1.0",
313313
},
314314
/* The absolute path to the build directory where Cargo places its output. */
315315
"target_directory": "/path/to/my-package/target",

src/etc/man/cargo-metadata.1

+7-7
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The JSON output has the following format:
5858
/* The Package ID, an opaque and unique identifier for referring to the
5959
package. See "Compatibility" above for the stability guarantee.
6060
*/
61-
"id": "my\-package 0.1.0 (path+file:///path/to/my\-package)",
61+
"id": "file:///path/to/my\-package#0.1.0",
6262
/* The license value from the manifest, or null. */
6363
"license": "MIT/Apache\-2.0",
6464
/* The license\-file value from the manifest, or null. */
@@ -244,13 +244,13 @@ The JSON output has the following format:
244244
Each entry is the Package ID for the package.
245245
*/
246246
"workspace_members": [
247-
"my\-package 0.1.0 (path+file:///path/to/my\-package)",
247+
"file:///path/to/my\-package#0.1.0",
248248
],
249249
/* Array of default members of the workspace.
250250
Each entry is the Package ID for the package.
251251
*/
252252
"workspace_default_members": [
253-
"my\-package 0.1.0 (path+file:///path/to/my\-package)",
253+
"file:///path/to/my\-package#0.1.0",
254254
],
255255
// The resolved dependency graph for the entire workspace. The enabled
256256
// features are based on the enabled features for the "current" package.
@@ -268,10 +268,10 @@ The JSON output has the following format:
268268
"nodes": [
269269
{
270270
/* The Package ID of this node. */
271-
"id": "my\-package 0.1.0 (path+file:///path/to/my\-package)",
271+
"id": "file:///path/to/my\-package#0.1.0",
272272
/* The dependencies of this package, an array of Package IDs. */
273273
"dependencies": [
274-
"bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)"
274+
"https://github.com/rust\-lang/crates.io\-index#[email protected]"
275275
],
276276
/* The dependencies of this package. This is an alternative to
277277
"dependencies" which contains additional information. In
@@ -285,7 +285,7 @@ The JSON output has the following format:
285285
*/
286286
"name": "bitflags",
287287
/* The Package ID of the dependency. */
288-
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)",
288+
"pkg": "https://github.com/rust\-lang/crates.io\-index#[email protected]"
289289
/* Array of dependency kinds. Added in Cargo 1.40. */
290290
"dep_kinds": [
291291
{
@@ -311,7 +311,7 @@ The JSON output has the following format:
311311
This is null if this is a virtual workspace. Otherwise it is
312312
the Package ID of the root package.
313313
*/
314-
"root": "my\-package 0.1.0 (path+file:///path/to/my\-package)"
314+
"root": "file:///path/to/my\-package#0.1.0",
315315
},
316316
/* The absolute path to the build directory where Cargo places its output. */
317317
"target_directory": "/path/to/my\-package/target",

0 commit comments

Comments
 (0)