Skip to content

Commit

Permalink
fix: correct check for required attribute
Browse files Browse the repository at this point in the history
### Description
Currently, in the current code, the `required` attribute does not work as expected.
On the code path where it should "continue if not required" actually just fallback into the error printing and process exiting.

Small reproducible example:
```rust
#[dotenvy::load(required = false)]
#[tokio::main]
async fn main() {
    let _ = env::var("SOME_VAR").unwrap();
}
```

Which errors out with:
```
Failed to load env file from path './.env': error reading './.env':, No such file or directory (os error 2)
```
  • Loading branch information
augustoccesar committed Sep 26, 2024
1 parent db0d6aa commit 474d974
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions dotenvy-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ pub fn load(attr: TokenStream, item: TokenStream) -> TokenStream {
let mut loader = EnvLoader::with_path(#path).sequence(seq);
if let Err(e) = unsafe { loader.load_and_modify() } {
if let Some(io_err) = e.source().and_then(|src| src.downcast_ref::<io::Error>()) {
if io_err.kind() == io::ErrorKind::NotFound && !#required {
// `required` is false and file not found, so continue
match (io_err.kind(), #required) {
(io::ErrorKind::NotFound, false) => (),
_ => {
eprintln!("Failed to load env file from path '{}': {e}", #path);
process::exit(1);
}
}
}
eprintln!("Failed to load env file from path '{}': {e}", #path);
process::exit(1);

}
};

Expand Down

0 comments on commit 474d974

Please sign in to comment.