Skip to content

Commit

Permalink
feat: include public/private dependency status in cargo metadata ou…
Browse files Browse the repository at this point in the history
…tput
  • Loading branch information
eopb committed Sep 7, 2024
1 parent ac7d8c1 commit fe96006
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/bin/cargo/commands/read_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Deprecated, use `<cyan,bold>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(())
}
21 changes: 19 additions & 2 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PathBuf>,

/// `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<bool>,
}

#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug, Copy)]
Expand Down Expand Up @@ -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(),
Expand All @@ -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
},
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand All @@ -205,7 +210,7 @@ impl Package {
.cloned()
.collect();
// Convert Vec<FeatureValue> to Vec<InternedString>
let features = summary
let crate_features = summary
.features()
.iter()
.map(|(k, v)| {
Expand All @@ -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(),
Expand Down
7 changes: 5 additions & 2 deletions src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ fn cargo_metadata_public_private_dependencies_enabled() {
"kind": null,
"name": "bar",
"optional": false,
"public": false,
"registry": null,
"rename": null,
"req": "*",
Expand All @@ -789,6 +790,7 @@ fn cargo_metadata_public_private_dependencies_enabled() {
"kind": null,
"name": "baz",
"optional": false,
"public": false,
"registry": null,
"rename": null,
"req": "*",
Expand All @@ -801,6 +803,7 @@ fn cargo_metadata_public_private_dependencies_enabled() {
"kind": null,
"name": "foobar",
"optional": false,
"public": true,
"registry": null,
"rename": null,
"req": "*",
Expand Down

0 comments on commit fe96006

Please sign in to comment.