From fe9600675eefef88c19d2ee1e1835c598c0299d3 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Fri, 6 Sep 2024 08:22:23 +0100 Subject: [PATCH] feat: include public/private dependency status in `cargo metadata` output --- src/bin/cargo/commands/read_manifest.rs | 5 ++++- src/cargo/core/dependency.rs | 21 +++++++++++++++++++-- src/cargo/core/package.rs | 15 ++++++++++----- src/cargo/ops/cargo_output_metadata.rs | 7 +++++-- tests/testsuite/metadata.rs | 3 +++ 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/bin/cargo/commands/read_manifest.rs b/src/bin/cargo/commands/read_manifest.rs index b86bbf795bc..692b79d1cbb 100644 --- a/src/bin/cargo/commands/read_manifest.rs +++ b/src/bin/cargo/commands/read_manifest.rs @@ -15,6 +15,9 @@ Deprecated, use `cargo metadata --no-deps` instead.\ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { let ws = args.workspace(gctx)?; - gctx.shell().print_json(&ws.current()?.serialized())?; + gctx.shell().print_json( + &ws.current()? + .serialized(gctx.cli_unstable(), ws.unstable_features()), + )?; Ok(()) } diff --git a/src/cargo/core/dependency.rs b/src/cargo/core/dependency.rs index be999c59a64..2ec2aa1a328 100644 --- a/src/cargo/core/dependency.rs +++ b/src/cargo/core/dependency.rs @@ -9,7 +9,7 @@ use std::sync::Arc; use tracing::trace; use crate::core::compiler::{CompileKind, CompileTarget}; -use crate::core::{PackageId, SourceId, Summary}; +use crate::core::{CliUnstable, Feature, Features, PackageId, SourceId, Summary}; use crate::util::errors::CargoResult; use crate::util::interning::InternedString; use crate::util::OptVersionReq; @@ -72,6 +72,12 @@ pub struct SerializedDependency { /// The file system path for a local path dependency. #[serde(skip_serializing_if = "Option::is_none")] path: Option, + + /// `public` flag is unset if `-Zpublic-dependency` is not enabled + /// + /// Once that feature is stabilized, `public` will not need to be `Option` + #[serde(skip_serializing_if = "Option::is_none")] + public: Option, } #[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug, Copy)] @@ -158,7 +164,11 @@ impl Dependency { } } - pub fn serialized(&self) -> SerializedDependency { + pub fn serialized( + &self, + unstable_flags: &CliUnstable, + features: &Features, + ) -> SerializedDependency { SerializedDependency { name: self.package_name(), source: self.source_id(), @@ -172,6 +182,13 @@ impl Dependency { registry: self.registry_id().as_ref().map(|sid| sid.url().to_string()), path: self.source_id().local_path(), artifact: self.inner.artifact.clone(), + public: if unstable_flags.public_dependency + || features.is_enabled(Feature::public_dependency()) + { + Some(self.inner.public) + } else { + None + }, } } diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 4495aa245ec..ac1bcdc5cb7 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -23,7 +23,8 @@ use crate::core::dependency::DepKind; use crate::core::resolver::features::ForceAllTargets; use crate::core::resolver::{HasDevUnits, Resolve}; use crate::core::{ - Dependency, Manifest, PackageId, PackageIdSpec, SerializedDependency, SourceId, Target, + CliUnstable, Dependency, Features, Manifest, PackageId, PackageIdSpec, SerializedDependency, + SourceId, Target, }; use crate::core::{Summary, Workspace}; use crate::sources::source::{MaybePackage, SourceMap}; @@ -190,7 +191,11 @@ impl Package { self.targets().iter().any(|t| t.is_example() || t.is_bin()) } - pub fn serialized(&self) -> SerializedPackage { + pub fn serialized( + &self, + unstable_flags: &CliUnstable, + cargo_features: &Features, + ) -> SerializedPackage { let summary = self.manifest().summary(); let package_id = summary.package_id(); let manmeta = self.manifest().metadata(); @@ -205,7 +210,7 @@ impl Package { .cloned() .collect(); // Convert Vec to Vec - let features = summary + let crate_features = summary .features() .iter() .map(|(k, v)| { @@ -229,10 +234,10 @@ impl Package { dependencies: summary .dependencies() .iter() - .map(Dependency::serialized) + .map(|dep| dep.serialized(unstable_flags, cargo_features)) .collect(), targets, - features, + features: crate_features, manifest_path: self.manifest_path().to_path_buf(), metadata: self.manifest().custom_metadata().cloned(), authors: manmeta.authors.clone(), diff --git a/src/cargo/ops/cargo_output_metadata.rs b/src/cargo/ops/cargo_output_metadata.rs index 1aadc05d77a..246636d1f60 100644 --- a/src/cargo/ops/cargo_output_metadata.rs +++ b/src/cargo/ops/cargo_output_metadata.rs @@ -33,7 +33,10 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo ); } let (packages, resolve) = if opt.no_deps { - let packages = ws.members().map(|pkg| pkg.serialized()).collect(); + let packages = ws + .members() + .map(|pkg| pkg.serialized(ws.gctx().cli_unstable(), ws.unstable_features())) + .collect(); (packages, None) } else { let (packages, resolve) = build_resolve_graph(ws, opt)?; @@ -178,7 +181,7 @@ fn build_resolve_graph( let actual_packages = package_map .into_iter() .filter_map(|(pkg_id, pkg)| node_map.get(&pkg_id).map(|_| pkg)) - .map(|pkg| pkg.serialized()) + .map(|pkg| pkg.serialized(ws.gctx().cli_unstable(), ws.unstable_features())) .collect(); let mr = MetadataResolve { diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index d8187a907b4..c7bf2af5040 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -777,6 +777,7 @@ fn cargo_metadata_public_private_dependencies_enabled() { "kind": null, "name": "bar", "optional": false, + "public": false, "registry": null, "rename": null, "req": "*", @@ -789,6 +790,7 @@ fn cargo_metadata_public_private_dependencies_enabled() { "kind": null, "name": "baz", "optional": false, + "public": false, "registry": null, "rename": null, "req": "*", @@ -801,6 +803,7 @@ fn cargo_metadata_public_private_dependencies_enabled() { "kind": null, "name": "foobar", "optional": false, + "public": true, "registry": null, "rename": null, "req": "*",