Skip to content

Commit

Permalink
refactor(metadata): Switch opaque ID from PackageID to PackageIDSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 2, 2023
1 parent a9f6393 commit 59fda3f
Show file tree
Hide file tree
Showing 13 changed files with 332 additions and 312 deletions.
6 changes: 3 additions & 3 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::dependency::DepKind;
use crate::core::resolver::features::ForceAllTargets;
use crate::core::resolver::{HasDevUnits, Resolve};
use crate::core::{Dependency, Manifest, PackageId, SourceId, Target};
use crate::core::{Dependency, Manifest, PackageId, PackageIdSpec, SourceId, Target};
use crate::core::{Summary, Workspace};
use crate::sources::source::{MaybePackage, SourceMap};
use crate::util::cache_lock::{CacheLock, CacheLockMode};
Expand Down Expand Up @@ -81,7 +81,7 @@ impl PartialOrd for Package {
pub struct SerializedPackage {
name: InternedString,
version: Version,
id: PackageId,
id: PackageIdSpec,
license: Option<String>,
license_file: Option<String>,
description: Option<String>,
Expand Down Expand Up @@ -241,7 +241,7 @@ impl Package {
SerializedPackage {
name: package_id.name(),
version: package_id.version().clone(),
id: package_id,
id: PackageIdSpec::from_package_id(package_id),
license: manmeta.license.clone(),
license_file: manmeta.license_file.clone(),
description: manmeta.description.clone(),
Expand Down
48 changes: 31 additions & 17 deletions src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::dependency::DepKind;
use crate::core::package::SerializedPackage;
use crate::core::resolver::{features::CliFeatures, HasDevUnits, Resolve};
use crate::core::{Package, PackageId, Workspace};
use crate::core::{Package, PackageId, PackageIdSpec, Workspace};
use crate::ops::{self, Packages};
use crate::util::interning::InternedString;
use crate::util::CargoResult;
Expand Down Expand Up @@ -42,8 +42,14 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo

Ok(ExportInfo {
packages,
workspace_members: ws.members().map(|pkg| pkg.package_id()).collect(),
workspace_default_members: ws.default_members().map(|pkg| pkg.package_id()).collect(),
workspace_members: ws
.members()
.map(|pkg| PackageIdSpec::from_package_id(pkg.package_id()))
.collect(),
workspace_default_members: ws
.default_members()
.map(|pkg| PackageIdSpec::from_package_id(pkg.package_id()))
.collect(),
resolve,
target_directory: ws.target_dir().into_path_unlocked(),
version: VERSION,
Expand All @@ -58,8 +64,8 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo
#[derive(Serialize)]
pub struct ExportInfo {
packages: Vec<SerializedPackage>,
workspace_members: Vec<PackageId>,
workspace_default_members: Vec<PackageId>,
workspace_members: Vec<PackageIdSpec>,
workspace_default_members: Vec<PackageIdSpec>,
resolve: Option<MetadataResolve>,
target_directory: PathBuf,
version: u32,
Expand All @@ -70,13 +76,13 @@ pub struct ExportInfo {
#[derive(Serialize)]
struct MetadataResolve {
nodes: Vec<MetadataResolveNode>,
root: Option<PackageId>,
root: Option<PackageIdSpec>,
}

#[derive(Serialize)]
struct MetadataResolveNode {
id: PackageId,
dependencies: Vec<PackageId>,
id: PackageIdSpec,
dependencies: Vec<PackageIdSpec>,
deps: Vec<Dep>,
features: Vec<InternedString>,
}
Expand All @@ -86,7 +92,9 @@ struct Dep {
// TODO(bindeps): after -Zbindeps gets stabilized,
// mark this field as deprecated in the help manual of cargo-metadata
name: InternedString,
pkg: PackageId,
pkg: PackageIdSpec,
#[serde(skip)]
pkg_id: PackageId,
dep_kinds: Vec<DepKindInfo>,
}

Expand Down Expand Up @@ -179,7 +187,9 @@ fn build_resolve_graph(

let mr = MetadataResolve {
nodes: node_map.into_iter().map(|(_pkg_id, node)| node).collect(),
root: ws.current_opt().map(|pkg| pkg.package_id()),
root: ws
.current_opt()
.map(|pkg| PackageIdSpec::from_package_id(pkg.package_id())),
};
Ok((actual_packages, mr))
}
Expand Down Expand Up @@ -301,18 +311,20 @@ fn build_resolve_graph_r(

dep_kinds.sort();

let pkg = normalize_id(dep_id);
let pkg_id = normalize_id(dep_id);

let dep = match (lib_target, dep_kinds.len()) {
(Some(target), _) => Dep {
name: extern_name(target)?,
pkg,
pkg: PackageIdSpec::from_package_id(pkg_id.clone()),
pkg_id,
dep_kinds,
},
// No lib target exists but contains artifact deps.
(None, 1..) => Dep {
name: InternedString::new(""),
pkg,
pkg: PackageIdSpec::from_package_id(pkg_id.clone()),
pkg_id,
dep_kinds,
},
// No lib or artifact dep exists.
Expand All @@ -325,11 +337,13 @@ fn build_resolve_graph_r(
dep_metadatas
};

let dumb_deps: Vec<PackageId> = deps.iter().map(|dep| dep.pkg).collect();
let to_visit = dumb_deps.clone();
let to_visit: Vec<PackageId> = deps.iter().map(|dep| dep.pkg_id).collect();
let node = MetadataResolveNode {
id: normalize_id(pkg_id),
dependencies: dumb_deps,
id: PackageIdSpec::from_package_id(normalize_id(pkg_id)),
dependencies: to_visit
.iter()
.map(|id| PackageIdSpec::from_package_id(id.clone()))
.collect(),
deps,
features,
};
Expand Down
18 changes: 9 additions & 9 deletions src/doc/man/cargo-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ The JSON output has the following format:
"name": "my-package",
/* The version of the package. */
"version": "0.1.0",
/* The Package ID, an opaque and unique identifier for referring to the
package. See "Compatibility" above for the stability guarantee.
/* The Package ID for referring to the
package within the document and as the `--package` argument to many commands
*/
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
"id": "file:///path/to/my-package#0.1.0",
/* The license value from the manifest, or null. */
"license": "MIT/Apache-2.0",
/* The license-file value from the manifest, or null. */
Expand Down Expand Up @@ -242,13 +242,13 @@ The JSON output has the following format:
Each entry is the Package ID for the package.
*/
"workspace_members": [
"my-package 0.1.0 (path+file:///path/to/my-package)",
"file:///path/to/my-package#0.1.0",
],
/* Array of default members of the workspace.
Each entry is the Package ID for the package.
*/
"workspace_default_members": [
"my-package 0.1.0 (path+file:///path/to/my-package)",
"file:///path/to/my-package#0.1.0",
],
// The resolved dependency graph for the entire workspace. The enabled
// features are based on the enabled features for the "current" package.
Expand All @@ -266,10 +266,10 @@ The JSON output has the following format:
"nodes": [
{
/* The Package ID of this node. */
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
"id": "file:///path/to/my-package#0.1.0",
/* The dependencies of this package, an array of Package IDs. */
"dependencies": [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
"https://github.com/rust-lang/crates.io-index#bitflags@1.0.4"
],
/* The dependencies of this package. This is an alternative to
"dependencies" which contains additional information. In
Expand All @@ -283,7 +283,7 @@ The JSON output has the following format:
*/
"name": "bitflags",
/* The Package ID of the dependency. */
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg": "https://github.com/rust-lang/crates.io-index#bitflags@1.0.4"
/* Array of dependency kinds. Added in Cargo 1.40. */
"dep_kinds": [
{
Expand All @@ -309,7 +309,7 @@ The JSON output has the following format:
This is null if this is a virtual workspace. Otherwise it is
the Package ID of the root package.
*/
"root": "my-package 0.1.0 (path+file:///path/to/my-package)"
"root": "file:///path/to/my-package#0.1.0",
},
/* The absolute path to the build directory where Cargo places its output. */
"target_directory": "/path/to/my-package/target",
Expand Down
18 changes: 9 additions & 9 deletions src/doc/man/generated_txt/cargo-metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ OUTPUT FORMAT
"name": "my-package",
/* The version of the package. */
"version": "0.1.0",
/* The Package ID, an opaque and unique identifier for referring to the
package. See "Compatibility" above for the stability guarantee.
/* The Package ID for referring to the
package within the document and as the `--package` argument to many commands
*/
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
"id": "file:///path/to/my-package#0.1.0",
/* The license value from the manifest, or null. */
"license": "MIT/Apache-2.0",
/* The license-file value from the manifest, or null. */
Expand Down Expand Up @@ -238,13 +238,13 @@ OUTPUT FORMAT
Each entry is the Package ID for the package.
*/
"workspace_members": [
"my-package 0.1.0 (path+file:///path/to/my-package)",
"file:///path/to/my-package#0.1.0",
],
/* Array of default members of the workspace.
Each entry is the Package ID for the package.
*/
"workspace_default_members": [
"my-package 0.1.0 (path+file:///path/to/my-package)",
"file:///path/to/my-package#0.1.0",
],
// The resolved dependency graph for the entire workspace. The enabled
// features are based on the enabled features for the "current" package.
Expand All @@ -262,10 +262,10 @@ OUTPUT FORMAT
"nodes": [
{
/* The Package ID of this node. */
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
"id": "file:///path/to/my-package#0.1.0",
/* The dependencies of this package, an array of Package IDs. */
"dependencies": [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
"https://github.com/rust-lang/crates.io-index#bitflags@1.0.4"
],
/* The dependencies of this package. This is an alternative to
"dependencies" which contains additional information. In
Expand All @@ -279,7 +279,7 @@ OUTPUT FORMAT
*/
"name": "bitflags",
/* The Package ID of the dependency. */
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg": "https://github.com/rust-lang/crates.io-index#bitflags@1.0.4"
/* Array of dependency kinds. Added in Cargo 1.40. */
"dep_kinds": [
{
Expand All @@ -305,7 +305,7 @@ OUTPUT FORMAT
This is null if this is a virtual workspace. Otherwise it is
the Package ID of the root package.
*/
"root": "my-package 0.1.0 (path+file:///path/to/my-package)"
"root": "file:///path/to/my-package#0.1.0",
},
/* The absolute path to the build directory where Cargo places its output. */
"target_directory": "/path/to/my-package/target",
Expand Down
18 changes: 9 additions & 9 deletions src/doc/src/commands/cargo-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ The JSON output has the following format:
"name": "my-package",
/* The version of the package. */
"version": "0.1.0",
/* The Package ID, an opaque and unique identifier for referring to the
package. See "Compatibility" above for the stability guarantee.
/* The Package ID for referring to the
package within the document and as the `--package` argument to many commands
*/
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
"id": "file:///path/to/my-package#0.1.0",
/* The license value from the manifest, or null. */
"license": "MIT/Apache-2.0",
/* The license-file value from the manifest, or null. */
Expand Down Expand Up @@ -242,13 +242,13 @@ The JSON output has the following format:
Each entry is the Package ID for the package.
*/
"workspace_members": [
"my-package 0.1.0 (path+file:///path/to/my-package)",
"file:///path/to/my-package#0.1.0",
],
/* Array of default members of the workspace.
Each entry is the Package ID for the package.
*/
"workspace_default_members": [
"my-package 0.1.0 (path+file:///path/to/my-package)",
"file:///path/to/my-package#0.1.0",
],
// The resolved dependency graph for the entire workspace. The enabled
// features are based on the enabled features for the "current" package.
Expand All @@ -266,10 +266,10 @@ The JSON output has the following format:
"nodes": [
{
/* The Package ID of this node. */
"id": "my-package 0.1.0 (path+file:///path/to/my-package)",
"id": "file:///path/to/my-package#0.1.0",
/* The dependencies of this package, an array of Package IDs. */
"dependencies": [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
"https://github.com/rust-lang/crates.io-index#bitflags@1.0.4"
],
/* The dependencies of this package. This is an alternative to
"dependencies" which contains additional information. In
Expand All @@ -283,7 +283,7 @@ The JSON output has the following format:
*/
"name": "bitflags",
/* The Package ID of the dependency. */
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg": "https://github.com/rust-lang/crates.io-index#bitflags@1.0.4"
/* Array of dependency kinds. Added in Cargo 1.40. */
"dep_kinds": [
{
Expand All @@ -309,7 +309,7 @@ The JSON output has the following format:
This is null if this is a virtual workspace. Otherwise it is
the Package ID of the root package.
*/
"root": "my-package 0.1.0 (path+file:///path/to/my-package)"
"root": "file:///path/to/my-package#0.1.0",
},
/* The absolute path to the build directory where Cargo places its output. */
"target_directory": "/path/to/my-package/target",
Expand Down
18 changes: 9 additions & 9 deletions src/etc/man/cargo-metadata.1
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ The JSON output has the following format:
"name": "my\-package",
/* The version of the package. */
"version": "0.1.0",
/* The Package ID, an opaque and unique identifier for referring to the
package. See "Compatibility" above for the stability guarantee.
/* The Package ID for referring to the
package within the document and as the `\-\-package` argument to many commands
*/
"id": "my\-package 0.1.0 (path+file:///path/to/my\-package)",
"id": "file:///path/to/my\-package#0.1.0",
/* The license value from the manifest, or null. */
"license": "MIT/Apache\-2.0",
/* The license\-file value from the manifest, or null. */
Expand Down Expand Up @@ -244,13 +244,13 @@ The JSON output has the following format:
Each entry is the Package ID for the package.
*/
"workspace_members": [
"my\-package 0.1.0 (path+file:///path/to/my\-package)",
"file:///path/to/my\-package#0.1.0",
],
/* Array of default members of the workspace.
Each entry is the Package ID for the package.
*/
"workspace_default_members": [
"my\-package 0.1.0 (path+file:///path/to/my\-package)",
"file:///path/to/my\-package#0.1.0",
],
// The resolved dependency graph for the entire workspace. The enabled
// features are based on the enabled features for the "current" package.
Expand All @@ -268,10 +268,10 @@ The JSON output has the following format:
"nodes": [
{
/* The Package ID of this node. */
"id": "my\-package 0.1.0 (path+file:///path/to/my\-package)",
"id": "file:///path/to/my\-package#0.1.0",
/* The dependencies of this package, an array of Package IDs. */
"dependencies": [
"bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)"
"https://github.com/rust\-lang/crates.io\-index#bitflags@1.0.4"
],
/* The dependencies of this package. This is an alternative to
"dependencies" which contains additional information. In
Expand All @@ -285,7 +285,7 @@ The JSON output has the following format:
*/
"name": "bitflags",
/* The Package ID of the dependency. */
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)",
"pkg": "https://github.com/rust\-lang/crates.io\-index#bitflags@1.0.4"
/* Array of dependency kinds. Added in Cargo 1.40. */
"dep_kinds": [
{
Expand All @@ -311,7 +311,7 @@ The JSON output has the following format:
This is null if this is a virtual workspace. Otherwise it is
the Package ID of the root package.
*/
"root": "my\-package 0.1.0 (path+file:///path/to/my\-package)"
"root": "file:///path/to/my\-package#0.1.0",
},
/* The absolute path to the build directory where Cargo places its output. */
"target_directory": "/path/to/my\-package/target",
Expand Down
Loading

0 comments on commit 59fda3f

Please sign in to comment.