Skip to content

Commit 253eee4

Browse files
authored
Added build_directory field to cargo metadata output (#15377)
<!-- Thanks for submitting a pull request 🎉! Here are some tips for you: * If this is your first contribution, read "Cargo Contribution Guide" first: https://doc.crates.io/contrib/ * Run `cargo fmt --all` to format your code changes. * Small commits and pull requests are always preferable and easy to review. * If your idea is large and needs feedback from the community, read how: https://doc.crates.io/contrib/process/#working-on-large-features * Cargo takes care of compatibility. Read our design principles: https://doc.crates.io/contrib/design.html * When changing help text of cargo commands, follow the steps to generate docs: https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages * If your PR is not finished, set it as "draft" PR or add "WIP" in its title. * It's ok to use the CI resources to test your PR, but please don't abuse them. ### What does this PR try to resolve? Explain the motivation behind this change. A clear overview along with an in-depth explanation are helpful. You can use `Fixes #<issue number>` to associate this PR to an existing issue. ### How should we test and review this PR? Demonstrate how you test this change and guide reviewers through your PR. With a smooth review process, a pull request usually gets reviewed quicker. If you don't know how to write and run your tests, please read the guide: https://doc.crates.io/contrib/tests ### Additional information Other information you want to mention in this PR, such as prior arts, future extensions, an unresolved problem, or a TODO list. --> ### What does this PR try to resolve? This PR continues on the build-dir work laid in #14125. (see [this](#14125 (comment)) comment) We add a new `build_directory` field to the `cargo metadata` output when the `-Z build-dir` feature flag is enabled. ### How should we test and review this PR? I added a test for metadata output when build-dir is set. There are already many existing tests that verify that build-dir is not included if `-Z build-dir` is not passed. ### Additional information NOTE: I use `build_directory` instead of `build-dir` to match the existing `target_directory` field in the metadata output r? @epage
2 parents 2a5f670 + 54e5369 commit 253eee4

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/cargo/ops/cargo_output_metadata.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo
5252
.collect(),
5353
resolve,
5454
target_directory: ws.target_dir().into_path_unlocked(),
55+
build_directory: ws
56+
.gctx()
57+
.cli_unstable()
58+
.build_dir
59+
.then(|| ws.build_dir().into_path_unlocked()),
5560
version: VERSION,
5661
workspace_root: ws.root().to_path_buf(),
5762
metadata: ws.custom_metadata().cloned(),
@@ -68,6 +73,8 @@ pub struct ExportInfo {
6873
workspace_default_members: Vec<PackageIdSpec>,
6974
resolve: Option<MetadataResolve>,
7075
target_directory: PathBuf,
76+
#[serde(skip_serializing_if = "Option::is_none")]
77+
build_directory: Option<PathBuf>,
7178
version: u32,
7279
workspace_root: PathBuf,
7380
metadata: Option<toml::Value>,

tests/testsuite/metadata.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4294,6 +4294,44 @@ fn dep_kinds_workspace() {
42944294
.run();
42954295
}
42964296

4297+
#[cargo_test]
4298+
fn build_dir() {
4299+
let p = project()
4300+
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
4301+
.file(
4302+
".cargo/config.toml",
4303+
r#"
4304+
[build]
4305+
build-dir = "build-dir"
4306+
"#,
4307+
)
4308+
.build();
4309+
4310+
p.cargo("metadata -Z build-dir")
4311+
.masquerade_as_nightly_cargo(&["build-dir"])
4312+
.with_stdout_data(
4313+
str![[r#"
4314+
{
4315+
"build_directory": "[ROOT]/foo/build-dir",
4316+
"metadata": null,
4317+
"packages": "{...}",
4318+
"resolve": "{...}",
4319+
"target_directory": "[ROOT]/foo/target",
4320+
"version": 1,
4321+
"workspace_default_members": [
4322+
"path+[ROOTURL]/foo#0.0.1"
4323+
],
4324+
"workspace_members": [
4325+
"path+[ROOTURL]/foo#0.0.1"
4326+
],
4327+
"workspace_root": "[ROOT]/foo"
4328+
}
4329+
"#]]
4330+
.is_json(),
4331+
)
4332+
.run();
4333+
}
4334+
42974335
// Creating non-utf8 path is an OS-specific pain, so let's run this only on
42984336
// linux, where arbitrary bytes work.
42994337
#[cfg(target_os = "linux")]

0 commit comments

Comments
 (0)