Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show-env doesn't work in PowerShell #395

Open
asvrada opened this issue Nov 11, 2024 · 5 comments
Open

show-env doesn't work in PowerShell #395

asvrada opened this issue Nov 11, 2024 · 5 comments
Labels
C-enhancement Category: A new feature or an improvement for an existing one O-windows Operating system: Windows

Comments

@asvrada
Copy link

asvrada commented Nov 11, 2024

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 taiki-e added the O-windows Operating system: Windows label Nov 11, 2024
@taiki-e
Copy link
Owner

taiki-e commented Nov 11, 2024

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.

@taiki-e taiki-e added the C-enhancement Category: A new feature or an improvement for an existing one label Nov 11, 2024
@asvrada
Copy link
Author

asvrada commented Nov 11, 2024

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
Copy link
Owner

taiki-e commented Nov 17, 2024

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

@asvrada
Copy link
Author

asvrada commented Nov 24, 2024

I tried to add an option that prepends $env:, but for it to work for PowerShell, we also need to surround path with double-quote.

Take one variable as example

CARGO_LLVM_COV_TARGET_DIR=/rust/project/xxx/target

For PowerShell to read it, the path also needs to be double-quote escaped.

$env:CARGO_LLVM_COV_TARGET_DIR="/rust/project/xxx/target"

@taiki-e
Copy link
Owner

taiki-e commented Dec 4, 2024

I guess you can use "$env:{prefix}{key}=\"{}\"" for powershell instead of "{prefix}{key}={}".

writeln!(self.writer, "{prefix}{key}={}", shell_escape::escape(value.into()))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: A new feature or an improvement for an existing one O-windows Operating system: Windows
Projects
None yet
Development

No branches or pull requests

2 participants