Wulf/create-rust-app

release mode can't recognize .env file

Opened this issue · 2 comments

cargo run --release
......
thread 'main' panicked at 'No SECRET_KEY environment variable set!', /home/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/create-rust-app-8.0.2/src/lib.rs:65:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Although the .env does exist and has SECRET_KEY set and working fine with cargo run in dev mode.

Wulf commented

hey @zhenpingfeng thanks for reporting this. The .env file is only used in development. If you want this, add the following to your main.rs (you'll need the dotenv crate):

    // load .env in production
    #[cfg(not(debug_assertions))]
    {
        if dotenv::dotenv().is_err() {
            panic!("ERROR: Could not load environment variables from dotenv file");
        }
    }

I don't think we should add this as a default to all newly created projects because it leads to bad practice. For your use-case, it may be fine, but if you disagree and think it should be default, I'm willing to listen to your arguments!

Wulf commented

Closing for now, feel free to re-open :)