Skip to content

Conversation

@chickencoding123
Copy link

This adds support for dotenv file format. It is accomplished by using dotenvy crate. There's an open issue asking for this feature here #16.

@coveralls
Copy link

coveralls commented Oct 22, 2025

Pull Request Test Coverage Report for Build 18750157020

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 64.846%

Totals Coverage Status
Change from base Build 18169995909: 0.0%
Covered Lines: 950
Relevant Lines: 1465

💛 - Coveralls

Comment on lines +63 to +66

/// Dotenv (parsed with `dotenvy`)
#[cfg(feature = "dotenv")]
Dotenv,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work as another FileFormat / Source? I suspect there is a lot of design overlap with Environment that we'd want to make this functionality on that.

Copy link
Author

@chickencoding123 chickencoding123 Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another FileFormat. You can see the usage from the tests (added snippet for ref. here). It is similar to existing file formats. Also dotenvy dependency will take care of the overlap, if any. However I ensured OS env take precedence as per existing file formats.

 let s = Config::builder()
        .add_source(config::File::from_str(
            r#"
                FOO=bar
                BAZ=qux
            "#,
            config::FileFormat::Dotenv,
        ))
        .build()
        .unwrap();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI resolving conversations that aren't ready for it can make PR reviews more difficult on maintainers. You should be pretty confident that you understood the concern and responded to it as the maintainer expected to resolve it. That was not the case here.

Some traits of dotenv are

  • Stringly typed (so is ini)
  • Unstructured

Both of these characteristics are shared with Environment which has a lot of functionality for solving these problems. In fact, this is another form of environment variables! The only difference is it also shares some of the file loading characteristics of file formats.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've raised this (and other questions) at #16 (comment) as I prefer to work out high level designs in Issues rather than PRs.

@epage
Copy link
Contributor

epage commented Oct 23, 2025

Feel free to modify your commits for how they should be merged

@chickencoding123 chickencoding123 changed the title feat: adding support for dotenv files feat(dotenv): Adding support for dotenv files Oct 23, 2025
for item in from_read_iter(cursor) {
let (key, mut value) = item?;

let os_env_vars = std::env::vars_os();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be doing manual layering, that is what the ConfigBuilder is for.

Copy link
Author

@chickencoding123 chickencoding123 Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dotenvy overrides OS var which is inconsistent with existing FileFormat in this crate. Manual layering is appealing here because file formats sit below ConfigBuilder in the hierarchy. Coupling a ConfigBuilder inside a file format will lead to difficult refactoring in the future.

I think for the purposes of this specific file format, simple env override should suffice?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manual layering is appealing here because file formats sit below ConfigBuilder in the hierarchy. Coupling a ConfigBuilder inside a file format will lead to difficult refactoring in the future.

Environment and each File are manually added as sources and the user has control over the precedence order. However we support DotEnv, it will be similar, so we don't need to do this here. If we did,. it would belong in Environment which is what is handling the OS vars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants