Skip to content

Commit 44dc49f

Browse files
committed
Auto merge of rust-lang#2450 - avrong:cargo-metadata, r=oli-obk
Use cargo_metadata in cargo-miri Closes rust-lang#2393 Added `cargo_metadata` to `cargo-miri` and changed metadata from manual parsing to `cargo_metadata` invocations. Thus, removed local `Metadata` struct too. Happy to fix if anything isn't right :)
2 parents a719c05 + 01a6109 commit 44dc49f

File tree

3 files changed

+69
-44
lines changed

3 files changed

+69
-44
lines changed

cargo-miri/Cargo.lock

+55-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-miri/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ doctest = false # and no doc tests
1717
directories = "3"
1818
rustc_version = "0.4"
1919
serde_json = "1.0.40"
20+
cargo_metadata = "0.15.0"
2021

2122
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
2223
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`

cargo-miri/bin.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::ops::Not;
1414
use std::path::{Path, PathBuf};
1515
use std::process::{self, Command};
1616

17+
use cargo_metadata::{Metadata, MetadataCommand};
1718
use rustc_version::VersionMeta;
1819
use serde::{Deserialize, Serialize};
1920

@@ -582,18 +583,13 @@ path = "lib.rs"
582583
}
583584
}
584585

585-
#[derive(Deserialize)]
586-
struct Metadata {
587-
target_directory: PathBuf,
588-
workspace_members: Vec<String>,
589-
}
590-
591586
fn get_cargo_metadata() -> Metadata {
592-
let mut cmd = cargo();
593-
// `-Zunstable-options` is required by `--config`.
594-
cmd.args(["metadata", "--no-deps", "--format-version=1", "-Zunstable-options"]);
595587
// The `build.target-dir` config can be passed by `--config` flags, so forward them to
596588
// `cargo metadata`.
589+
let mut additional_options = Vec::new();
590+
// `-Zunstable-options` is required by `--config`.
591+
additional_options.push("-Zunstable-options".to_string());
592+
597593
let config_flag = "--config";
598594
for arg in ArgSplitFlagValue::new(
599595
env::args().skip(3), // skip the program name, "miri" and "run" / "test"
@@ -602,21 +598,14 @@ fn get_cargo_metadata() -> Metadata {
602598
// Only look at `Ok`
603599
.flatten()
604600
{
605-
cmd.arg(config_flag).arg(arg);
606-
}
607-
let mut child = cmd
608-
.stdin(process::Stdio::null())
609-
.stdout(process::Stdio::piped())
610-
.spawn()
611-
.expect("failed ro run `cargo metadata`");
612-
// Check this `Result` after `status.success()` is checked, so we don't print the error
613-
// to stderr if `cargo metadata` is also printing to stderr.
614-
let metadata: Result<Metadata, _> = serde_json::from_reader(child.stdout.take().unwrap());
615-
let status = child.wait().expect("failed to wait for `cargo metadata` to exit");
616-
if !status.success() {
617-
std::process::exit(status.code().unwrap_or(-1));
601+
additional_options.push(config_flag.to_string());
602+
additional_options.push(arg);
618603
}
619-
metadata.unwrap_or_else(|e| show_error(format!("invalid `cargo metadata` output: {}", e)))
604+
605+
let metadata =
606+
MetadataCommand::new().no_deps().other_options(additional_options).exec().unwrap();
607+
608+
metadata
620609
}
621610

622611
/// Pulls all the crates in this workspace from the cargo metadata.
@@ -627,7 +616,7 @@ fn local_crates(metadata: &Metadata) -> String {
627616
assert!(!metadata.workspace_members.is_empty());
628617
let mut local_crates = String::new();
629618
for member in &metadata.workspace_members {
630-
let name = member.split(' ').next().unwrap();
619+
let name = member.repr.split(' ').next().unwrap();
631620
let name = name.replace('-', "_");
632621
local_crates.push_str(&name);
633622
local_crates.push(',');

0 commit comments

Comments
 (0)