Skip to content

Commit

Permalink
Include public/private dependency status in cargo metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
eopb committed Sep 6, 2024
1 parent 1e2e019 commit 6a4f355
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
3 changes: 2 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,7 @@ 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()))?;
Ok(())
}
13 changes: 11 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, 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,7 @@ impl Dependency {
}
}

pub fn serialized(&self) -> SerializedDependency {
pub fn serialized(&self, unstable_flags: &CliUnstable) -> SerializedDependency {
SerializedDependency {
name: self.package_name(),
source: self.source_id(),
Expand All @@ -172,6 +178,9 @@ 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: unstable_flags
.public_dependency
.then_some(self.inner.public),
}
}

Expand Down
7 changes: 4 additions & 3 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, Manifest, PackageId, PackageIdSpec, SerializedDependency, SourceId,
Target,
};
use crate::core::{Summary, Workspace};
use crate::sources::source::{MaybePackage, SourceMap};
Expand Down Expand Up @@ -190,7 +191,7 @@ 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) -> SerializedPackage {
let summary = self.manifest().summary();
let package_id = summary.package_id();
let manmeta = self.manifest().metadata();
Expand Down Expand Up @@ -229,7 +230,7 @@ impl Package {
dependencies: summary
.dependencies()
.iter()
.map(Dependency::serialized)
.map(|dep| dep.serialized(unstable_flags))
.collect(),
targets,
features,
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()))
.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()))
.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 @@ -928,6 +928,7 @@ fn cargo_metadata_public_private_dependencies_enabled() {
"kind": null,
"name": "bar",
"optional": false,
"public": false,
"registry": null,
"rename": null,
"req": "*",
Expand All @@ -940,6 +941,7 @@ fn cargo_metadata_public_private_dependencies_enabled() {
"kind": null,
"name": "baz",
"optional": false,
"public": false,
"registry": null,
"rename": null,
"req": "*",
Expand All @@ -952,6 +954,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 6a4f355

Please sign in to comment.