Skip to content

Commit b958d79

Browse files
committed
Auto merge of #14504 - eopb:push-xknxowwslrpt, r=epage
Include public/private dependency status in `cargo metadata` fixes #14502 > [!TIP] > This change can be reviewed commit-by-commit. > Descriptions on individual commits are available to justify some decisions `@rustbot` label Command-metadata Z-public-dependency
2 parents d0cb869 + 3783494 commit b958d79

File tree

10 files changed

+292
-45
lines changed

10 files changed

+292
-45
lines changed

src/bin/cargo/commands/read_manifest.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Deprecated, use `<cyan,bold>cargo metadata --no-deps</>` instead.\
1515

1616
pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
1717
let ws = args.workspace(gctx)?;
18-
gctx.shell().print_json(&ws.current()?.serialized())?;
18+
gctx.shell().print_json(
19+
&ws.current()?
20+
.serialized(gctx.cli_unstable(), ws.unstable_features()),
21+
)?;
1922
Ok(())
2023
}

src/cargo/core/dependency.rs

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::Arc;
99
use tracing::trace;
1010

1111
use crate::core::compiler::{CompileKind, CompileTarget};
12-
use crate::core::{PackageId, SourceId, Summary};
12+
use crate::core::{CliUnstable, Feature, Features, PackageId, SourceId, Summary};
1313
use crate::util::errors::CargoResult;
1414
use crate::util::interning::InternedString;
1515
use crate::util::OptVersionReq;
@@ -52,50 +52,32 @@ struct Inner {
5252
}
5353

5454
#[derive(Serialize)]
55-
struct SerializedDependency<'a> {
56-
name: &'a str,
55+
pub struct SerializedDependency {
56+
name: InternedString,
5757
source: SourceId,
5858
req: String,
5959
kind: DepKind,
60-
rename: Option<&'a str>,
60+
rename: Option<InternedString>,
6161

6262
optional: bool,
6363
uses_default_features: bool,
64-
features: &'a [InternedString],
64+
features: Vec<InternedString>,
6565
#[serde(skip_serializing_if = "Option::is_none")]
66-
artifact: Option<&'a Artifact>,
67-
target: Option<&'a Platform>,
66+
artifact: Option<Artifact>,
67+
target: Option<Platform>,
6868
/// The registry URL this dependency is from.
6969
/// If None, then it comes from the default registry (crates.io).
70-
registry: Option<&'a str>,
70+
registry: Option<String>,
7171

7272
/// The file system path for a local path dependency.
7373
#[serde(skip_serializing_if = "Option::is_none")]
7474
path: Option<PathBuf>,
75-
}
7675

77-
impl ser::Serialize for Dependency {
78-
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
79-
where
80-
S: ser::Serializer,
81-
{
82-
let registry_id = self.registry_id();
83-
SerializedDependency {
84-
name: &*self.package_name(),
85-
source: self.source_id(),
86-
req: self.version_req().to_string(),
87-
kind: self.kind(),
88-
optional: self.is_optional(),
89-
uses_default_features: self.uses_default_features(),
90-
features: self.features(),
91-
target: self.platform(),
92-
rename: self.explicit_name_in_toml().map(|s| s.as_str()),
93-
registry: registry_id.as_ref().map(|sid| sid.url().as_str()),
94-
path: self.source_id().local_path(),
95-
artifact: self.artifact(),
96-
}
97-
.serialize(s)
98-
}
76+
/// `public` flag is unset if `-Zpublic-dependency` is not enabled
77+
///
78+
/// Once that feature is stabilized, `public` will not need to be `Option`
79+
#[serde(skip_serializing_if = "Option::is_none")]
80+
public: Option<bool>,
9981
}
10082

10183
#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug, Copy)]
@@ -182,6 +164,34 @@ impl Dependency {
182164
}
183165
}
184166

167+
pub fn serialized(
168+
&self,
169+
unstable_flags: &CliUnstable,
170+
features: &Features,
171+
) -> SerializedDependency {
172+
SerializedDependency {
173+
name: self.package_name(),
174+
source: self.source_id(),
175+
req: self.version_req().to_string(),
176+
kind: self.kind(),
177+
optional: self.is_optional(),
178+
uses_default_features: self.uses_default_features(),
179+
features: self.features().to_vec(),
180+
target: self.inner.platform.clone(),
181+
rename: self.explicit_name_in_toml(),
182+
registry: self.registry_id().as_ref().map(|sid| sid.url().to_string()),
183+
path: self.source_id().local_path(),
184+
artifact: self.inner.artifact.clone(),
185+
public: if unstable_flags.public_dependency
186+
|| features.is_enabled(Feature::public_dependency())
187+
{
188+
Some(self.inner.public)
189+
} else {
190+
None
191+
},
192+
}
193+
}
194+
185195
pub fn version_req(&self) -> &OptVersionReq {
186196
&self.inner.req
187197
}

src/cargo/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use self::dependency::Dependency;
1+
pub use self::dependency::{Dependency, SerializedDependency};
22
pub use self::features::{CliUnstable, Edition, Feature, Features};
33
pub use self::manifest::{EitherManifest, VirtualManifest};
44
pub use self::manifest::{Manifest, Target, TargetKind};

src/cargo/core/package.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
2222
use crate::core::dependency::DepKind;
2323
use crate::core::resolver::features::ForceAllTargets;
2424
use crate::core::resolver::{HasDevUnits, Resolve};
25-
use crate::core::{Dependency, Manifest, PackageId, PackageIdSpec, SourceId, Target};
25+
use crate::core::{
26+
CliUnstable, Dependency, Features, Manifest, PackageId, PackageIdSpec, SerializedDependency,
27+
SourceId, Target,
28+
};
2629
use crate::core::{Summary, Workspace};
2730
use crate::sources::source::{MaybePackage, SourceMap};
2831
use crate::util::cache_lock::{CacheLock, CacheLockMode};
@@ -73,7 +76,7 @@ pub struct SerializedPackage {
7376
license_file: Option<String>,
7477
description: Option<String>,
7578
source: SourceId,
76-
dependencies: Vec<Dependency>,
79+
dependencies: Vec<SerializedDependency>,
7780
targets: Vec<Target>,
7881
features: BTreeMap<InternedString, Vec<InternedString>>,
7982
manifest_path: PathBuf,
@@ -188,7 +191,11 @@ impl Package {
188191
self.targets().iter().any(|t| t.is_example() || t.is_bin())
189192
}
190193

191-
pub fn serialized(&self) -> SerializedPackage {
194+
pub fn serialized(
195+
&self,
196+
unstable_flags: &CliUnstable,
197+
cargo_features: &Features,
198+
) -> SerializedPackage {
192199
let summary = self.manifest().summary();
193200
let package_id = summary.package_id();
194201
let manmeta = self.manifest().metadata();
@@ -203,7 +210,7 @@ impl Package {
203210
.cloned()
204211
.collect();
205212
// Convert Vec<FeatureValue> to Vec<InternedString>
206-
let features = summary
213+
let crate_features = summary
207214
.features()
208215
.iter()
209216
.map(|(k, v)| {
@@ -224,9 +231,13 @@ impl Package {
224231
license_file: manmeta.license_file.clone(),
225232
description: manmeta.description.clone(),
226233
source: summary.source_id(),
227-
dependencies: summary.dependencies().to_vec(),
234+
dependencies: summary
235+
.dependencies()
236+
.iter()
237+
.map(|dep| dep.serialized(unstable_flags, cargo_features))
238+
.collect(),
228239
targets,
229-
features,
240+
features: crate_features,
230241
manifest_path: self.manifest_path().to_path_buf(),
231242
metadata: self.manifest().custom_metadata().cloned(),
232243
authors: manmeta.authors.clone(),

src/cargo/ops/cargo_output_metadata.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo
3333
);
3434
}
3535
let (packages, resolve) = if opt.no_deps {
36-
let packages = ws.members().map(|pkg| pkg.serialized()).collect();
36+
let packages = ws
37+
.members()
38+
.map(|pkg| pkg.serialized(ws.gctx().cli_unstable(), ws.unstable_features()))
39+
.collect();
3740
(packages, None)
3841
} else {
3942
let (packages, resolve) = build_resolve_graph(ws, opt)?;
@@ -178,7 +181,7 @@ fn build_resolve_graph(
178181
let actual_packages = package_map
179182
.into_iter()
180183
.filter_map(|(pkg_id, pkg)| node_map.get(&pkg_id).map(|_| pkg))
181-
.map(|pkg| pkg.serialized())
184+
.map(|pkg| pkg.serialized(ws.gctx().cli_unstable(), ws.unstable_features()))
182185
.collect();
183186

184187
let mr = MetadataResolve {

src/doc/man/cargo-metadata.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ The JSON output has the following format:
123123
If not specified or null, the dependency is from the default
124124
registry (crates.io).
125125
*/
126-
"registry": null
126+
"registry": null,
127+
/* (unstable) Boolean flag of whether or not this is a pulbic
128+
dependency. This field is only present when
129+
`-Zpublic-dependency` is enabled.
130+
*/
131+
"public": false
127132
}
128133
],
129134
/* Array of Cargo targets. */

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ OUTPUT FORMAT
119119
If not specified or null, the dependency is from the default
120120
registry (crates.io).
121121
*/
122-
"registry": null
122+
"registry": null,
123+
/* (unstable) Boolean flag of whether or not this is a pulbic
124+
dependency. This field is only present when
125+
`-Zpublic-dependency` is enabled.
126+
*/
127+
"public": false
123128
}
124129
],
125130
/* Array of Cargo targets. */

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ The JSON output has the following format:
123123
If not specified or null, the dependency is from the default
124124
registry (crates.io).
125125
*/
126-
"registry": null
126+
"registry": null,
127+
/* (unstable) Boolean flag of whether or not this is a pulbic
128+
dependency. This field is only present when
129+
`-Zpublic-dependency` is enabled.
130+
*/
131+
"public": false
127132
}
128133
],
129134
/* Array of Cargo targets. */

src/etc/man/cargo-metadata.1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ The JSON output has the following format:
125125
If not specified or null, the dependency is from the default
126126
registry (crates.io).
127127
*/
128-
"registry": null
128+
"registry": null,
129+
/* (unstable) Boolean flag of whether or not this is a pulbic
130+
dependency. This field is only present when
131+
`\-Zpublic\-dependency` is enabled.
132+
*/
133+
"public": false
129134
}
130135
],
131136
/* Array of Cargo targets. */

0 commit comments

Comments
 (0)