allan2 / dotenvy

A well-maintained fork of the dotenv crate
MIT License
664 stars 40 forks source link

Notify when dotenv file is improperly formatted #7

Closed mpmoran closed 1 year ago

mpmoran commented 2 years ago

When the library is given a dotenv file that is in INI format (by mistake in my case), it will not create environment variables. I can't find where users are notified of this. Should they be and how?

Here is stuff to reproduce.

.env

[secrets]
DB_PATH=securepath
DB_NAME=perfectname

main.rs

use std::env;
use dotenvy::dotenv;

fn main() {
    env_logger::init();

    dotenv().ok();
    for (key, value) in env::vars() {
        println!("{key}: {value}");
    }
}

dependencies

dotenvy = "0.15.1"
env_logger = "0.9.0"
log = "0.4.17"

Run with $ RUST_LOG=debug cargo run | grep DB_

d4h0 commented 2 years ago

@mpmoran: The problem is most likely the dotenv().ok();.

That code turns the Result (that dotenv returns) into an Option (see Result::ok). So you are basically just ignoring any error that dotenv might return.

The example of the dotenv function (which is just dotenvy::dotenv().ok();) is probably not the best example that could be shown.

allan2 commented 2 years ago

@d4h0 good comment. I've removed the ok() examples to make this more clear.

@mpmoran dotenvy does inform you of parse errors. Using the same .env file as your example:

fn main() {
    let e = dotenvy::dotenv().unwrap_err();
    print!("{}", e);
}

Error parsing line: '[secrets]', error at line index: 0

mpmoran commented 1 year ago

I'm going to politician this and say that I can't affirm or deny the previous comments. I'm no longer using this library. Please deal with this issue as it deserves.

allan2 commented 1 year ago

This is working when the line is changed to dotenvy::dotenv()

@mpmoran feel free to reopen if you still have issues.