Skip to content

Commit

Permalink
Merge #121
Browse files Browse the repository at this point in the history
121: Fix show-env subcommand bug r=taiki-e a=taiki-e

This fixes the problem that the readme example does not work.
This probably fixes the remaining problem reported in #115.

cc #93
cc `@davidhewitt` 


Co-authored-by: Taiki Endo <te316e89@gmail.com>
  • Loading branch information
bors[bot] and taiki-e committed Jan 6, 2022
2 parents 681f574 + 80f7b32 commit 78f981c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
4 changes: 3 additions & 1 deletion 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]

## [0.1.14] - 2022-01-07
- Fix bug in `show-env` subcommand.

## [0.1.14] - 2022-01-06

- Add `show-env` subcommand. ([#115](https://github.com/taiki-e/cargo-llvm-cov/pull/115), thanks @davidhewitt)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ cargo llvm-cov --no-run --lcov
In combination with the `show-env` subcommand, coverage can also be produced from arbitrary binaries:

```sh
cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
source <(cargo llvm-cov show-env --export-prefix)
cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
cargo build # build rust binaries
# commands using binaries in target/debug/*, including `cargo test`
# ...
Expand Down
18 changes: 14 additions & 4 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ pub(crate) struct Workspace {
}

impl Workspace {
pub(crate) fn new(options: &ManifestOptions, target: Option<&str>) -> Result<Self> {
pub(crate) fn new(
options: &ManifestOptions,
target: Option<&str>,
show_env: bool,
) -> Result<Self> {
let cargo = env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
let rustc = rustc_path(&cargo);
let (nightly, ref host) = rustc_version(&rustc)?;
Expand All @@ -46,9 +50,15 @@ impl Workspace {
Some(host),
)?;

// If we change RUSTFLAGS, all dependencies will be recompiled. Therefore,
// use a subdirectory of the target directory as the actual target directory.
let target_dir = metadata.target_directory.join("llvm-cov-target");
let target_dir = if let Some(path) = env::var("CARGO_LLVM_COV_TARGET_DIR")? {
path.into()
} else if show_env {
metadata.target_directory.clone()
} else {
// If we change RUSTFLAGS, all dependencies will be recompiled. Therefore,
// use a subdirectory of the target directory as the actual target directory.
metadata.target_directory.join("llvm-cov-target")
};
let output_dir = metadata.target_directory.join("llvm-cov");
let doctests_dir = target_dir.join("doctestbins");

Expand Down
2 changes: 1 addition & 1 deletion src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
};

pub(crate) fn run(mut options: CleanOptions) -> Result<()> {
let ws = Workspace::new(&options.manifest, None)?;
let ws = Workspace::new(&options.manifest, None, false)?;
ws.config.merge_to_args(&mut None, &mut options.verbose, &mut options.color);
term::set_coloring(&mut options.color);

Expand Down
3 changes: 2 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ impl Context {
package: &[String],
doctests: bool,
no_run: bool,
show_env: bool,
) -> Result<Self> {
let ws = Workspace::new(&manifest, build.target.as_deref())?;
let ws = Workspace::new(&manifest, build.target.as_deref(), show_env)?;
ws.config.merge_to_args(&mut build.target, &mut build.verbose, &mut build.color);
term::set_coloring(&mut build.color);
term::verbose::set(build.verbose != 0);
Expand Down
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fn try_main() -> Result<()> {
args.package.as_ref().map(slice::from_ref).unwrap_or_default(),
false,
false,
false,
)?;

clean::clean_partial(cx)?;
Expand All @@ -97,12 +98,14 @@ fn try_main() -> Result<()> {
}

Some(Subcommand::ShowEnv(options)) => {
let cx = &context_from_args(&mut args)?;
set_env(cx, &mut ShowEnvWriter { target: std::io::stdout(), options });
let cx = &context_from_args(&mut args, true)?;
let writer = &mut ShowEnvWriter { target: std::io::stdout(), options };
set_env(cx, writer);
writer.set("CARGO_LLVM_COV_TARGET_DIR", cx.ws.metadata.target_directory.as_str());
}

None => {
let cx = &context_from_args(&mut args)?;
let cx = &context_from_args(&mut args, false)?;
let tmp = term::warn(); // The following warnings should not be promoted to an error.
if args.doctests {
warn!("--doctests option is unstable");
Expand Down Expand Up @@ -133,7 +136,7 @@ fn try_main() -> Result<()> {
Ok(())
}

fn context_from_args(args: &mut Args) -> Result<Context> {
fn context_from_args(args: &mut Args, show_env: bool) -> Result<Context> {
Context::new(
args.build(),
args.manifest(),
Expand All @@ -143,6 +146,7 @@ fn context_from_args(args: &mut Args) -> Result<Context> {
&args.package,
args.doctests,
args.no_run,
show_env,
)
}

Expand Down

0 comments on commit 78f981c

Please sign in to comment.