Skip to content

option_env does not get updated when rebuilding and value of the key changed. #8306

Closed
@jschwe

Description

@jschwe

Problem
I expected to see this happen:
Recompiling with cargo build / cargo run should update the value received from option_env to the current value of the key in the environment. Setting CARGO_INCREMENTAL=0 doesn't seem to affect this. It only causes a recompile the first time around since incremental compilation is turned on by default.

Instead, this happened:
The value received from option_env only changes when running cargo clean before.

Steps

This is a minimum example.

fn main() {
    let var = option_env!("TEST_OPT_ENV");
    match var{
        Some(str) => println!("{}", str),
        None => println!("Hello, World!"),
    }
}
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.36s
     Running `target\debug\hello_world.exe`
Hello, World!
$ TEST_OPT_ENV="Using Option Env" cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target\debug\hello_world.exe`
Hello, World!
$ cargo clean
$ TEST_OPT_ENV="Using Option Env" cargo run
   Compiling hello_world v0.1.0 (C:\Users\jschw\CLionProjects\hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target\debug\hello_world.exe`
Using Option Env
$ CARGO_INCREMENTAL=0 TEST_OPT_ENV="Using Option Env Test 2" cargo run
   Compiling hello_world v0.1.0 (C:\Users\jschw\CLionProjects\hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.28s
     Running `target\debug\hello_world.exe`
Using Option Env Test 2
$ CARGO_INCREMENTAL=0 TEST_OPT_ENV="Using Option Env Test 3" cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target\debug\hello_world.exe`
Using Option Env Test 2

Possible Solution(s)

rust-lang/rust#71858 should provide the necessary information once it is merged.

Why I think I need this:
I'm compiling (and linking to) a rust library from a build script (different target) and want the option_env to get updated when I rebuild with a different value set for the key specified in the option_env.

Notes

Output of cargo version:
cargo 1.45.0-nightly (9fcb8c1d2 2020-05-25)

rustc --version --verbose:

rustc 1.45.0-nightly (ad4bc3323 2020-06-01)
binary: rustc
commit-hash: ad4bc3323b9299d867697e9653dcea1b5e1ad283
commit-date: 2020-06-01
host: x86_64-pc-windows-msvc
release: 1.45.0-nightly
LLVM version: 10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions