allan2 / dotenvy

A well-maintained fork of the Rust dotenv crate
MIT License
625 stars 39 forks source link

Macro alternative to fallback to regular env #76

Open ShayBox opened 1 year ago

ShayBox commented 1 year ago

I use dotenvy! for development to load from .env and env! in production to load from regular env (GitHub Workflows), now I have a feature flag to switch between the two but it's very inconvenient and repetitive.

#[cfg(feature = "production")]
const EXAMPLE_ENV: &str = dotenv!("EXAMPLE_ENV");
#[cfg(not(feature = "production"))]
const EXAMPLE_ENV: &str = env!("EXAMPLE_ENV");

Having a macro that falls back to the regular env would be nice.

DaXcess commented 3 weeks ago

+1

I was initially under the impression that this was already how dotenvy operated, but unfortunately had to find out the hard way that environment variables in the build pipeline were not accepted by the dotenv! macro, as it throws a compiler error when no .env file is found, even when the environment variable is present.