taiki-e / cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Apache License 2.0
948 stars 57 forks source link

show-env doesn't work in PowerShell #395

Open asvrada opened 6 days ago

asvrada commented 6 days ago

What's the issue cargo llvm-cov show-env would print non-printable characters (0x1f ASCII Unit Separator), and PowerShell can't handle them.

For example I am building a DLL on Windows that needs to follow Get coverage of external tests.

And I am using below PowerShell script to set the env variable

$input = cargo llvm-cov show-env

# Split the input into lines
$lines = $input -split "`n"

foreach ($line in $lines) {
    $parts = $line -split "="
    $key = $parts[0]
    $val = $parts[1]

    if ($key -eq "CARGO_ENCODED_RUSTFLAGS") {
        $env:CARGO_ENCODED_RUSTFLAGS = $val
    } elseif ($key -eq "LLVM_PROFILE_FILE") {
        $env:LLVM_PROFILE_FILE = $val
    } elseif ($key -eq "CARGO_LLVM_COV") {
        $env:CARGO_LLVM_COV = $val
    } elseif ($key -eq "CARGO_LLVM_COV_SHOW_ENV") {
        $env:CARGO_LLVM_COV_SHOW_ENV = $val
    } elseif ($key -eq "CARGO_LLVM_COV_TARGET_DIR") {
        $env:CARGO_LLVM_COV_TARGET_DIR = $val
    } else {
        Write-Error "Unexpected env variable! $key=$val"
    }
}

If I ran this script and check $env:CARGO_ENCODED_RUSTFLAGS, I would get

-Ccontrol-flow-guard▼-Ctarget-feature

If I directly run cargo llvm-cov show-env, I would get

CARGO_ENCODED_RUSTFLAGS="-Ccontrol-flow-guard▼-Ctarget-feature=+crt-static▼-Clink-args=/DYNAMICBASE /CETCOMPAT /force:guardehcont▼-Cehcont_guard▼-C▼instrument-coverage▼--cfg=coverage▼--cfg=trybuild_no_target"
<ignored other vars>

The ▼ is supposed to be 0x1f ASCII Unit Separator, as indicated by cargo doc here(search for CARGO_ENCODED_RUSTFLAGS). But according to this stackoverflow answer, PowerShell can't handle binary data

taiki-e commented 6 days ago

We do not guarantee output in any particular shell-compatible format except for --export-prefix (which is for the UNIX shell), so I think the solution here is to add a flag for PowerShell equivalent to --export-prefix.

asvrada commented 6 days ago

Hi @taiki-e I want to take a look at this issue this weekend, probably by adding a new option like --ps-env-prefix. Could you point me to the relevant file as a starting point?

taiki-e commented 18 hours ago

Main related files are cli.rs, which parses the flags, and ShowEnvWriter in main.rs, which does the actual output.

A search of the existing code with export[_-]prefix should show all related locations: https://github.com/search?q=repo%3Ataiki-e%2Fcargo-llvm-cov+%2Fexport%5B_-%5Dprefix%2F&type=code