Only works if the code changes
I've noticed this only appears to work when the code changes.
For example, if you have this:
#[const_env::from_env]
static SWITCH: bool = false;
fn main() {
...
}
On first compile, you can change the value to true with:
SWITCH=true cargo build
However, if you change the env var and rebuild like so:
SWITCH=false cargo build
The value is still set to true until the code is changed and recompiled as per last command.
Maybe try adding
println!("cargo:rerun-if-env-changed=SWITCH");
inside build.rs?
Thanks @haata. I'll give it a try if/when I use it (I can't recall what I was working on, but I'm sure I'll need this feature again).
The tracked feature of this crate is intended to solve this problem. Enabling it requires using an unstable Rust compiler due to the dependency on https://github.com/rust-lang/rust/issues/99515.
Going to close this as the unstable proc_macro::tracked_env::var is the best way to do this that I'm aware of, and the tracked feature of this crate allows users to opt-in to using that if they're on a nightly compiler.
If a proc-macro emits the tokens for a call to env! or option_env!, then the proc-macro's caller automatically gets an "env-dep" build dependency on that variable.
As far as I can tell this works as well as the unstable tracked_env feature. For example:
// Hack: when compiled by the proc-macro caller it will register a build dependency
// on this environment variable, and cargo will rebuild the crate when the var changes.
// This is otherwise unused.
static __ENV_DEP_HACK: Option<&str> = option_env!("SOME_VAR");
That's interesting, I'll reopen this to track adding that workaround to the code.
I've also tried using this, which I think should be optimized away to nothing (but keeps the env-dep side effect), and doesn't risk symbol name collisions if it gets emitted multiple times:
const _: () = {
let _ = option_env!("SOME_VAR");
};
I implemented your suggested approach and tested that it does allow tracking to work even when using a stable compiler. I've published version 0.1.5 with the improvement. Thanks for the suggestion @ericseppanen.