rust-cli / rust-cli/config-rs

Cannot read env variables when field names have `_` in them

Open
#391 18 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-env
Dominant language
Rust
Stars
3.2k
Forks
265
Avg merge
2h 42m
Merged PRs (30d)
4

Description

Getting the following error when trying to get an environmental variable from config-rs:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: configuration property "DEV_SERVER_ADDRESS" not found', src/app/config.rs:14:27 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


use config::Config;
use lazy_static;

lazy_static! {
    #[derive(Debug)]
    pub static ref CONFIG: Config = Config::builder()
        .add_source(config::Environment::with_prefix("APP_NAME").separator("_"))
        .add_source(config::File::with_name("AppName"))
        .build()
        .unwrap();
}

pub fn get<'a, T: serde::Deserialize<'a>>(key: &str) -> T {
    CONFIG.get::<T>(&key).unwrap()
}

ENV variable:

export APP_NAME_DEV_SERVER_ADDRESS="testtt dev addr"

Trying to get the variable in main:

#[actix_web::main]
async fn main() -> result::Result<(), io::Error> {

    let dev_server_address: String = app::config::get("DEV_SERVER_ADDRESS");
    println!("DEV_SERVER_ADDRESS: {}", dev_server_address);
...

What am I doing wrong?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in src/app/config.rs:14, especially Environment::with_prefix("APP_NAME").separator("_") and the get("DEV_SERVER_ADDRESS") entry point. Run it with APP_NAME_DEV_SERVER_ADDRESS set, then determine whether the underscore handling matches the intended environment-source behavior; done means the reported lookup either works or its expected limitation is clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.