{"record":{"id":"d9e1876234d1da02","repo":"nautechsystems/nautilus_trader","slug":"environment-variable-key-must-be-set","errorCode":null,"errorMessage":"environment variable '{key}' must be set","messagePattern":"environment variable '(.+?)' must be set","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/core/src/env.rs","lineNumber":30,"sourceCode":"//  See the License for the specific language governing permissions and\n//  limitations under the License.\n// -------------------------------------------------------------------------------------------------\n\n//! Cross-platform environment variable utilities.\n//!\n//! This module provides functions for safely accessing environment variables\n//! with proper error handling.\n\n/// Returns the value of the environment variable for the given `key`.\n///\n/// # Errors\n///\n/// Returns an error if the environment variable is not set or is not valid Unicode.\npub fn get_env_var(key: &str) -> anyhow::Result<String> {\n    match std::env::var(key) {\n        Ok(var) => Ok(var),\n        Err(std::env::VarError::NotPresent) => {\n            anyhow::bail!(\"environment variable '{key}' must be set\")\n        }\n        Err(std::env::VarError::NotUnicode(_)) => {\n            anyhow::bail!(\"environment variable '{key}' is not valid Unicode\")\n        }\n    }\n}\n\n/// Returns the provided `value` if `Some`, otherwise falls back to reading\n/// the environment variable for the given `key`.\n///\n/// Only attempts to read the environment variable when `value` is `None`,\n/// avoiding unnecessary environment variable lookups and errors.\n///\n/// # Errors\n///\n/// Returns an error if `value` is `None` and the environment variable is not set.\npub fn get_or_env_var(value: Option<String>, key: &str) -> anyhow::Result<String> {\n    match value {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/core/src/env.rs#L12-L48","documentation":"`get_env_var` reads an environment variable and returns a descriptive error when it is not set at all (std::env::VarError::NotPresent). The library throws it because a required configuration value can only come from the environment and proceeding without it would be undefined.","triggerScenarios":"Calling `get_env_var(\"KEY\")` (directly, or via `from_env`/`main` config loading) when `KEY` was never exported in the process environment.","commonSituations":"Running the app without a .env file loaded; forgetting to export a variable in the shell or CI; typo in the variable name (e.g. NAUTILUS_PATH vs NAUTILUSPATH); variable defined in one environment (prod) but not another (local dev container).","solutions":["Export the environment variable before running: export KEY=value","Add it to your .env file / shell profile / CI secrets so it is always present","Check for typos in the variable name shown in the error message","Use `get_or_env_var` with a default if the value is genuinely optional"],"exampleFix":"// before\nlet path = get_env_var(\"NAUTILUS_PATH\")?; // panics-free error if unset\n// after\nlet path = get_or_env_var(\"NAUTILUS_PATH\", \"/tmp/nautilus\")?; // or ensure: export NAUTILUS_PATH=...","handlingStrategy":"validation","validationCode":"let val = std::env::var(\"MY_KEY\");\nif val.is_err() {\n    eprintln!(\"MY_KEY must be set; see deployment docs\");\n    std::process::exit(1);\n}","typeGuard":"fn env_var_set(key: &str) -> bool {\n    std::env::var_os(key).is_some()\n}","tryCatchPattern":"match get_env_var(\"MY_KEY\") {\n    Ok(v) => configure(v),\n    Err(e) => anyhow::bail!(\"startup config incomplete: {e}\"), // fail fast with context\n}","preventionTips":["Maintain a .env.example listing every required variable","Validate all env vars at startup, before doing any work","Use get_or_env_var with defaults for genuinely optional values","Check CI/deployment secrets match the variable names in code"],"tags":["rust","environment","configuration"],"backgroundTag":"missing-env-var","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}