Skip to content

Commit

Permalink
Get target and profile from nextest archive
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Feb 22, 2024
1 parent 959921c commit 8c737ae
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ build.allow-build-scripts = [
{ name = "fs-err" },
{ name = "libc" },
{ name = "proc-macro2" },
{ name = "rustix" }, # via tar
{ name = "serde_json" },
{ name = "serde" },
{ name = "syn" }, # 1.0, via ruzstd
{ name = "winapi-i686-pc-windows-gnu" },
{ name = "winapi-x86_64-pc-windows-gnu" },
{ name = "winapi" },
Expand Down
1 change: 1 addition & 0 deletions .github/.cspell/project-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ profdata
profraw
rmeta
rustfilt
rustix
TESTNAME
trybuild
winapi
Expand Down
1 change: 1 addition & 0 deletions .github/.cspell/rust-dependencies.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ jobs:
rm a.tar.zst
cargo clean
cargo llvm-cov nextest-archive --archive-file a.tar.zst --release
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70 --release
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70
rm a.tar.zst
cargo clean
cargo llvm-cov nextest-archive --archive-file a.tar.zst --cargo-profile custom
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70 --cargo-profile custom
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70
rm a.tar.zst
cargo clean
host=$(rustc -Vv | grep host | sed 's/host: //')
cargo llvm-cov nextest-archive --archive-file a.tar.zst --target "${host}"
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70 --target "${host}"
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70
working-directory: tests/fixtures/crates/bin_crate
- run: |
set -eEuxo pipefail
Expand Down
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- Support `--release` and `--cargo-profile` options for `cargo llvm-cov nextest --archive-file`. ([#348](https://github.com/taiki-e/cargo-llvm-cov/pull/348))
- cargo-llvm-cov no longer needs to pass `--target`/`--release`/`--cargo-profile` to `cargo llvm-cov nextest --archive-file` in most cases. Options passed to `cargo llvm-cov nextest-archive` are now respected. ([#349](https://github.com/taiki-e/cargo-llvm-cov/pull/349))

- Support `--release` and `--cargo-profile` options for `cargo llvm-cov nextest-archive`. ([#348](https://github.com/taiki-e/cargo-llvm-cov/pull/348))

## [0.6.5] - 2024-02-07

Expand All @@ -30,7 +32,7 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [0.6.1] - 2024-01-13

- Support `--target` option for `cargo llvm-cov nextest --archive-file`. ([#334](https://github.com/taiki-e/cargo-llvm-cov/pull/334))
- Support `--target` option for `cargo llvm-cov nextest-archive`. ([#334](https://github.com/taiki-e/cargo-llvm-cov/pull/334))

- Support `--no-cfg-coverage` and `--no-cfg-coverage-nightly` flags in `cargo llvm-cov show-env`. ([#333](https://github.com/taiki-e/cargo-llvm-cov/pull/333))

Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ lexopt = "0.3"
opener = "0.6"
regex = { version = "1.3", default-features = false, features = ["perf", "std"] }
rustc-demangle = "0.1.21"
ruzstd = { version = "0.6", default-features = false, features = ["std"] }
serde = "1.0.103"
serde_derive = "1.0.103"
serde_json = "1"
shell-escape = "0.1.5"
tar = "0.4.40"
termcolor = "1.1.2"
walkdir = "2.2.3"

Expand Down
16 changes: 12 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ pub(crate) struct Args {

pub(crate) manifest: ManifestOptions,

pub(crate) archive_file: Option<String>,

pub(crate) cargo_args: Vec<String>,
/// Arguments for the test binary
pub(crate) rest: Vec<String>,
Expand Down Expand Up @@ -249,6 +251,9 @@ impl Args {
// show-env options
let mut export_prefix = false;

// nextest options
let mut archive_file = None;

let mut parser = lexopt::Parser::from_args(args.clone());
while let Some(arg) = parser.next()? {
macro_rules! parse_opt {
Expand Down Expand Up @@ -419,6 +424,10 @@ impl Args {
// show-env options
Long("export-prefix") => parse_flag!(export_prefix),

Long("archive-file") if matches!(subcommand, Subcommand::Nextest { .. }) => {
parse_opt_passthrough!(archive_file);
}

Short('v') | Long("verbose") => {
verbose += 1;
after_subcommand = false;
Expand Down Expand Up @@ -503,7 +512,7 @@ impl Args {
warn!(
"note that `{val}` is treated as test filter instead of subcommand \
because `cargo llvm-cov nextest` internally calls `cargo nextest \
run`"
run`; if you want to use `nextest archive`, please use `cargo llvm-cov nextest-archive`"
);
}
cargo_args.push(val);
Expand All @@ -517,9 +526,7 @@ impl Args {
term::set_coloring(&mut color);

if matches!(subcommand, Subcommand::Nextest { .. }) {
subcommand = Subcommand::Nextest {
archive_file: cargo_args.iter().any(|a| a == "--archive-file"),
};
subcommand = Subcommand::Nextest { archive_file: archive_file.is_some() };
}

// unexpected options
Expand Down Expand Up @@ -905,6 +912,7 @@ impl Args {
include_ffi,
no_clean,
manifest: ManifestOptions { manifest_path, frozen, locked, offline },
archive_file,
cargo_args,
rest,
})
Expand Down
2 changes: 1 addition & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub(crate) use std::fs::Metadata;
use std::{ffi::OsStr, io, path::Path};

pub(crate) use fs_err::{create_dir_all, read_dir, symlink_metadata, write};
pub(crate) use fs_err::{create_dir_all, read_dir, symlink_metadata, write, File};

/// Removes a file from the filesystem **if exists**.
pub(crate) fn remove_file(path: impl AsRef<Path>) -> io::Result<()> {
Expand Down
78 changes: 65 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use std::{
collections::{BTreeSet, HashMap},
ffi::{OsStr, OsString},
io::{self, BufRead, Write},
io::{self, BufRead, Read, Write},
path::Path,
time::SystemTime,
};
Expand All @@ -20,6 +20,8 @@ use camino::{Utf8Path, Utf8PathBuf};
use cargo_config2::Flags;
use cargo_llvm_cov::json::{CodeCovJsonExport, CoverageKind, LlvmCovJsonExport};
use regex::Regex;
use serde_derive::Deserialize;
use tar::Archive;
use walkdir::WalkDir;

use crate::{
Expand Down Expand Up @@ -729,21 +731,71 @@ fn object_files(cx: &Context) -> Result<Vec<OsString>> {
// This is not the ideal way, but the way unstable book says it is cannot support them.
// https://doc.rust-lang.org/nightly/rustc/instrument-coverage.html#tips-for-listing-the-binaries-automatically
let mut target_dir = cx.ws.target_dir.clone();
let mut auto_detect_profile = false;
if matches!(cx.args.subcommand, Subcommand::Nextest { archive_file: true }) {
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct BinariesMetadata {
rust_build_meta: RustBuildMeta,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct RustBuildMeta {
base_output_directories: Vec<String>,
}
target_dir.push("target");
let archive_file = cx.args.archive_file.as_ref().unwrap();
let decoder = ruzstd::StreamingDecoder::new(fs::File::open(archive_file)?)?;
let mut archive = Archive::new(decoder);
let mut binaries_metadata = vec![];
for entry in archive.entries()? {
let mut entry = entry?;
let path = entry.path()?;
if path.ends_with("target/nextest/binaries-metadata.json") {
entry.read_to_end(&mut binaries_metadata)?;
break;
}
}
if binaries_metadata.is_empty() {
warn!("not found binaries-metadata.json in nextest archive {archive_file:?}");
} else {
match serde_json::from_slice::<BinariesMetadata>(&binaries_metadata) {
// TODO: what multiple base_output_directories means?
Ok(binaries_metadata)
if binaries_metadata.rust_build_meta.base_output_directories.len() == 1 =>
{
if cx.args.target.is_some() {
info!("--target flag is no longer needed because detection from nextest archive is now supported");
}
if cx.args.release {
info!("--release flag is no longer needed because detection from nextest archive is now supported");
}
if cx.args.profile.is_some() {
info!("--cargo-profile flag is no longer needed because detection from nextest archive is now supported");
}
target_dir.push(&binaries_metadata.rust_build_meta.base_output_directories[0]);
auto_detect_profile = true;
}
res => {
warn!("found binaries-metadata.json in nextest archive {archive_file:?}, but has unsupported or incompatible format: {res:?}");
}
}
}
}
// https://doc.rust-lang.org/nightly/cargo/guide/build-cache.html
if let Some(target) = &cx.args.target {
target_dir.push(target);
if !auto_detect_profile {
// https://doc.rust-lang.org/nightly/cargo/guide/build-cache.html
if let Some(target) = &cx.args.target {
target_dir.push(target);
}
// https://doc.rust-lang.org/nightly/cargo/reference/profiles.html#custom-profiles
let profile = match cx.args.profile.as_deref() {
None if cx.args.release => "release",
Some("release" | "bench") => "release",
None | Some("dev" | "test") => "debug",
Some(p) => p,
};
target_dir.push(profile);
}
// https://doc.rust-lang.org/nightly/cargo/reference/profiles.html#custom-profiles
let profile = match cx.args.profile.as_deref() {
None if cx.args.release => "release",
Some("release" | "bench") => "release",
None | Some("dev" | "test") => "debug",
Some(p) => p,
};
target_dir.push(profile);
for f in walk_target_dir(cx, &target_dir) {
let f = f.path();
if is_object(cx, f) {
Expand Down Expand Up @@ -813,7 +865,7 @@ fn object_files(cx: &Context) -> Result<Vec<OsString>> {
warn!(
"not found object files (searched directories: {searched_dir}); this may occur if \
show-env subcommand is used incorrectly (see docs or other warnings), or unsupported \
commands such as nextest archive are used",
commands are used",
);
}
Ok(files)
Expand Down

0 comments on commit 8c737ae

Please sign in to comment.