cross-rs / cross

“Zero setup” cross compilation and “cross testing” of Rust crates
Apache License 2.0
6.21k stars 354 forks source link

Build script cannot set custom env by println rustc-env #1503

Closed lazystitan closed 1 month ago

lazystitan commented 1 month ago

Checklist

Describe your issue

build.rs

fn main() {
    let version = get_git_version();
    println!("cargo:rustc-env=INNER_API_RS_V={}", version)
}

main.rs

let version = std::env::var("INNER_API_RS_V").unwrap().to_string();

error

called `Result::unwrap()` on an `Err` value: NotPresent

What target(s) are you cross-compiling for?

x86_64-unknown-linux-gnu

Which operating system is the host (e.g computer cross is on) running?

What architecture is the host?

What container engine is cross using?

cross version

cross 0.2.5 (53a45db 2024-05-11)

Example

No response

Additional information / notes

No response

Emilgardis commented 1 month ago

This is working as expected, rustc-env only sets the var during compilation

https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-env

As such, its retrievable with the compile time env! macro but not in runtime

If you have the envvar in your current shell/session you need to tell Docker to import it.

https://github.com/cross-rs/cross/blob/main/docs/config_file.md#buildenv

Im going to close this issue, if you feel like this is not correct or if you want some clarification please tell me! 😄