Cannot read env variables when field names have `_` in them
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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