Figment icon indicating copy to clipboard operation
Figment copied to clipboard

Loading a file that does not exist fails silently

Open SRv6d opened this issue 1 year ago • 3 comments

Extracting a Figment containing a Toml provider pointing to a file that does not exist returns an Ok(_) value, provided that there aren't any mandatory fields missing.

use figment::{
    providers::{Format, Toml},
    Figment,
};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Example {
    field: Option<String>,
}

fn main() {
    let figment = Figment::from(Toml::file("/does/not/exist"));
    dbg!(figment.extract::<Example>());
}

Running the above with the debug profile returns the following:

[src/main.rs:14:5] figment.extract::<Example>() = Ok(
    Example {
        field: None,
    },
)

Is this intended figment behavior? I would have expected an IO Error and would argue that silently ignoring the fact that the given path does not exist can lead to subtle bugs and does not fall in line with rust conventions.

SRv6d avatar Jul 07 '24 15:07 SRv6d

From the docs:

[...] If the source is a file and the file is not present, an empty dictionary is emitted.

So yes, this is intended behaviour.

thorio avatar Jul 22 '24 20:07 thorio

We could make this configurable, but this feels like a sane default to me (...though I was the one that chose it). I would be open to a PR that makes this configurable via some setting in Data.

SergioBenitez avatar Jul 22 '24 20:07 SergioBenitez

If this is intended and documented behavior, that's fine by me, it just isn't what I would've expected, and I missed it in the docs.

SRv6d avatar Jul 24 '24 18:07 SRv6d

@SergioBenitez Could you release a version that includes https://github.com/SergioBenitez/Figment/commit/01a43d919ffaacd1bcf23303473001edf25c4955?

SRv6d avatar Oct 02 '24 11:10 SRv6d