{"record":{"id":"46bdbd7d23eef5dc","repo":"nautechsystems/nautilus_trader","slug":"environment-variable-key-is-not-valid-unicode","errorCode":null,"errorMessage":"environment variable '{key}' is not valid Unicode","messagePattern":"environment variable '(.+?)' is not valid Unicode","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/core/src/env.rs","lineNumber":33,"sourceCode":"\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 {\n        Some(v) => Ok(v),\n        None => get_env_var(key),\n    }","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/core/src/env.rs#L15-L51","documentation":"`get_env_var` errors when the environment variable exists but contains bytes that are not valid Unicode (std::env::VarError::NotUnicode). Rust environment values are OsStrings and may hold arbitrary bytes; this API only returns String, so a non-UTF-8 value is unusable and rejected.","triggerScenarios":"Calling `get_env_var(key)` when the variable was set with non-UTF-8 bytes (e.g. via a low-level setenv, binary data, or a corrupted shell/launcher environment).","commonSituations":"Values pasted from Windows or legacy encodings (Latin-1, Shift-JIS) into a Unix environment; a script exporting binary/blob data; container images or process supervisors setting garbled values.","solutions":["Re-set the variable with a valid UTF-8 value: export KEY='clean value'","Find what is setting the variable non-Unicode (scripts, supervisor configs, OS locale) and fix the encoding","If raw bytes are expected, read via std::env::var_os yourself and decode explicitly instead of get_env_var"],"exampleFix":"// before\nlet val = get_env_var(\"MY_KEY\")?; // fails if MY_KEY has non-UTF8 bytes\n// after\nlet val = match std::env::var_os(\"MY_KEY\") {\n    Some(v) => String::from_utf8_lossy(v.as_encoded_bytes()).into_owned(),\n    None => anyhow::bail!(\"environment variable 'MY_KEY' must be set\"),\n};","handlingStrategy":"validation","validationCode":"if let Some(raw) = std::env::var_os(\"MY_KEY\") {\n    if raw.to_str().is_none() {\n        eprintln!(\"MY_KEY contains non-UTF-8 bytes\");\n    }\n}","typeGuard":"fn is_utf8_env(key: &str) -> bool {\n    std::env::var_os(key)\n        .map(|v| v.to_str().is_some())\n        .unwrap_or(false)\n}","tryCatchPattern":"match get_env_var(\"MY_KEY\") {\n    Ok(v) => use(v),\n    Err(e) if e.to_string().contains(\"not valid Unicode\") => {\n        // re-set the variable or read via var_os and lossy-decode\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Ensure shell/locale environments export UTF-8 values","Avoid setting env vars from binary data or legacy-encoded sources","Sanitize values written by scripts and supervisors"],"tags":["rust","environment","unicode","encoding"],"backgroundTag":"invalid-env-var-value","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"}