I have a procedural macro crate Proc
and binary crate Bin
. Bin
has a dependency on Proc
. Proc
needs a filled environment variable to function properly.
This is some code inside my build.rs
in Bin
. Proc
can successfully find the env value when using the following code:
fn main() {
println!("cargo:rustc-env=SOME_ENV_VALUE=somevalue");
}
However, Proc
fails to find the environment variable when using this code inside my build.rs
in Bin
(note: when checking the existence right after the dotenv
call, I can verify the key is actually present):
fn main() {
dotenv::dotenv().unwrap();
}
This is my Proc
crate:
use proc_macro::TokenStream;
#[proc_macro_derive(MyProcMacro)]
pub fn my_proc_macro(input: TokenStream) -> TokenStream {
if std::env::var("SOME_ENV_VALUE").is_err() {
panic!("Failed to retrieve env value")
}
TokenStream::new()
}
Why won't it fail with the println!
command? Can it work with dotenv
? Else I need to write some code that copies the keys from my env
file to the println!
command...
All the code is in my minimal reproduction project.