Skip to content

Commit

Permalink
Remove dependency on cargo_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 18, 2023
1 parent ad3a765 commit f214fed
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 42 deletions.
2 changes: 0 additions & 2 deletions .deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ build.allow-build-scripts = [
{ name = "libc" },
{ name = "proc-macro2" },
{ name = "rustix" }, # via is-terminal
{ name = "semver" }, # via cargo_metadata
{ name = "serde_json" },
{ name = "serde" },
{ name = "thiserror" }, # via cargo_metadata
{ name = "winapi-i686-pc-windows-gnu" },
{ name = "winapi-x86_64-pc-windows-gnu" },
{ name = "winapi" },
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ resolver = "2"

[dependencies]
anyhow = "1.0.47"
camino = "1.0.3"
cargo_metadata = "0.18"
camino = { version = "1.0.3", features = ["serde1"] }
cargo-config2 = "0.1.5"
duct = "0.13.1"
fs-err = "2.5"
Expand Down
18 changes: 3 additions & 15 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ use crate::{
cli::{ManifestOptions, Subcommand},
context::Context,
env,
metadata::Metadata,
process::ProcessBuilder,
};

pub(crate) struct Workspace {
pub(crate) name: String,
pub(crate) config: Config,
pub(crate) metadata: cargo_metadata::Metadata,
pub(crate) metadata: Metadata,
pub(crate) current_manifest: Utf8PathBuf,

pub(crate) target_dir: Utf8PathBuf,
Expand Down Expand Up @@ -44,7 +45,7 @@ impl Workspace {
// Metadata and config
let config = Config::load()?;
let current_manifest = package_root(config.cargo(), options.manifest_path.as_deref())?;
let metadata = metadata(config.cargo(), &current_manifest)?;
let metadata = Metadata::new(current_manifest.as_std_path(), config.cargo())?;
let mut target_for_config = config.build_target_for_config(target)?;
if target_for_config.len() != 1 {
bail!("cargo-llvm-cov doesn't currently supports multi-target builds: {target_for_config:?}");
Expand Down Expand Up @@ -188,19 +189,6 @@ fn locate_project(cargo: &OsStr) -> Result<String> {
cmd!(cargo, "locate-project", "--message-format", "plain").read()
}

// https://doc.rust-lang.org/nightly/cargo/commands/cargo-metadata.html
fn metadata(cargo: &OsStr, manifest_path: &Utf8Path) -> Result<cargo_metadata::Metadata> {
let mut cmd = cmd!(
cargo,
"metadata",
"--format-version=1",
"--no-deps",
"--manifest-path",
manifest_path
);
serde_json::from_str(&cmd.read()?).with_context(|| format!("failed to parse output from {cmd}"))
}

// https://doc.rust-lang.org/nightly/cargo/commands/cargo-test.html
// https://doc.rust-lang.org/nightly/cargo/commands/cargo-run.html
pub(crate) fn test_or_run_args(cx: &Context, cmd: &mut ProcessBuilder) {
Expand Down
8 changes: 4 additions & 4 deletions src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use std::path::Path;

use anyhow::Result;
use camino::Utf8Path;
use cargo_metadata::PackageId;
use walkdir::WalkDir;

use crate::{
cargo::{self, Workspace},
cli::{self, Args, ManifestOptions},
context::Context,
fs,
metadata::PackageId,
regex_vec::{RegexVec, RegexVecBuilder},
term,
};
Expand Down Expand Up @@ -56,7 +56,7 @@ pub(crate) fn clean_partial(cx: &Context) -> Result<()> {
.workspace_members
.included
.iter()
.flat_map(|id| ["--package", &cx.ws.metadata[id].name])
.flat_map(|id| ["--package", &cx.ws.metadata.packages[id].name])
.collect();
let mut cmd = cx.cargo();
cmd.arg("clean").args(package_args);
Expand All @@ -77,7 +77,7 @@ fn clean_ws(
clean_ws_inner(ws, pkg_ids, verbose != 0)?;

let package_args: Vec<_> =
pkg_ids.iter().flat_map(|id| ["--package", &ws.metadata[id].name]).collect();
pkg_ids.iter().flat_map(|id| ["--package", &ws.metadata.packages[id].name]).collect();
let mut args_set = vec![vec![]];
if ws.target_dir.join("release").exists() {
args_set.push(vec!["--release"]);
Expand Down Expand Up @@ -127,7 +127,7 @@ fn clean_ws_inner(ws: &Workspace, pkg_ids: &[PackageId], verbose: bool) -> Resul
fn pkg_hash_re(ws: &Workspace, pkg_ids: &[PackageId]) -> RegexVec {
let mut re = RegexVecBuilder::new("^(lib)?(", ")(-[0-9a-f]{7,})?$");
for id in pkg_ids {
re.or(&ws.metadata[id].name.replace('-', "(-|_)"));
re.or(&ws.metadata.packages[id].name.replace('-', "(-|_)"));
}
re.build().unwrap()
}
Expand Down
14 changes: 5 additions & 9 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use std::{

use anyhow::{bail, Result};
use camino::Utf8PathBuf;
use cargo_metadata::PackageId;

use crate::{
cargo::Workspace,
cli::{self, Args, Subcommand},
env,
metadata::{Metadata, PackageId},
process::ProcessBuilder,
regex_vec::{RegexVec, RegexVecBuilder},
term,
Expand Down Expand Up @@ -221,7 +221,7 @@ impl Context {
fn pkg_hash_re(ws: &Workspace, pkg_ids: &[PackageId]) -> RegexVec {
let mut re = RegexVecBuilder::new("^(", ")-[0-9a-f]+$");
for id in pkg_ids {
re.or(&ws.metadata[id].name);
re.or(&ws.metadata.packages[id].name);
}
re.build().unwrap()
}
Expand All @@ -232,18 +232,14 @@ pub(crate) struct WorkspaceMembers {
}

impl WorkspaceMembers {
fn new(
exclude: &[String],
exclude_from_report: &[String],
metadata: &cargo_metadata::Metadata,
) -> Self {
fn new(exclude: &[String], exclude_from_report: &[String], metadata: &Metadata) -> Self {
let mut excluded = vec![];
let mut included = vec![];
if !exclude.is_empty() || !exclude_from_report.is_empty() {
for id in &metadata.workspace_members {
// --exclude flag doesn't handle `name:version` format
if exclude.contains(&metadata[id].name)
|| exclude_from_report.contains(&metadata[id].name)
if exclude.contains(&metadata.packages[id].name)
|| exclude_from_report.contains(&metadata.packages[id].name)
{
excluded.push(id.clone());
} else {
Expand Down
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
cargo::Workspace,
cli::{Args, ShowEnvOptions, Subcommand},
context::Context,
metadata::Metadata,
process::ProcessBuilder,
regex_vec::{RegexVec, RegexVecBuilder},
term::Coloring,
Expand All @@ -46,6 +47,7 @@ mod context;
mod demangle;
mod env;
mod fs;
mod metadata;
mod regex_vec;

fn main() {
Expand Down Expand Up @@ -720,8 +722,8 @@ fn object_files(cx: &Context) -> Result<Vec<OsString>> {
trybuild_target_dir.push("debug");
if trybuild_target_dir.is_dir() {
let mut trybuild_targets = vec![];
for metadata in trybuild_metadata(&cx.ws.metadata.target_directory)? {
for package in metadata.packages {
for metadata in trybuild_metadata(&cx.ws, &cx.ws.metadata.target_directory)? {
for package in metadata.packages.into_values() {
for target in package.targets {
trybuild_targets.push(target.name);
}
Expand Down Expand Up @@ -769,7 +771,7 @@ impl Targets {
let mut packages = BTreeSet::new();
let mut targets = BTreeSet::new();
for id in &ws.metadata.workspace_members {
let pkg = &ws.metadata[id];
let pkg = &ws.metadata.packages[id];
packages.insert(pkg.name.clone());
for t in &pkg.targets {
targets.insert(t.name.clone());
Expand All @@ -792,7 +794,7 @@ impl Targets {

/// Collects metadata for packages generated by trybuild. If the trybuild test
/// directory is not found, it returns an empty vector.
fn trybuild_metadata(target_dir: &Utf8Path) -> Result<Vec<cargo_metadata::Metadata>> {
fn trybuild_metadata(ws: &Workspace, target_dir: &Utf8Path) -> Result<Vec<Metadata>> {
// https://github.com/dtolnay/trybuild/pull/219
let mut trybuild_dir = target_dir.join("tests").join("trybuild");
if !trybuild_dir.is_dir() {
Expand All @@ -803,13 +805,11 @@ fn trybuild_metadata(target_dir: &Utf8Path) -> Result<Vec<cargo_metadata::Metada
}
let mut metadata = vec![];
for entry in fs::read_dir(trybuild_dir)?.filter_map(Result::ok) {
let manifest_path = entry.path().join("Cargo.toml");
let manifest_path = &entry.path().join("Cargo.toml");
if !manifest_path.is_file() {
continue;
}
metadata.push(
cargo_metadata::MetadataCommand::new().manifest_path(manifest_path).no_deps().exec()?,
);
metadata.push(Metadata::new(manifest_path, ws.config.cargo())?);
}
Ok(metadata)
}
Expand Down Expand Up @@ -1123,13 +1123,13 @@ fn resolve_excluded_paths(cx: &Context) -> Vec<Utf8PathBuf> {
.workspace_members
.excluded
.iter()
.map(|id| cx.ws.metadata[id].manifest_path.parent().unwrap())
.map(|id| cx.ws.metadata.packages[id].manifest_path.parent().unwrap())
.collect();
let included = cx
.workspace_members
.included
.iter()
.map(|id| cx.ws.metadata[id].manifest_path.parent().unwrap());
.map(|id| cx.ws.metadata.packages[id].manifest_path.parent().unwrap());
let mut excluded_path = vec![];
let mut contains: HashMap<&Utf8Path, Vec<_>> = HashMap::new();
for included in included {
Expand Down
Loading

0 comments on commit f214fed

Please sign in to comment.