{"record":{"id":"0f58aca9a10e499a","repo":"rust-lang/rust","slug":"environment-variable-var-is-not-utf-8","errorCode":null,"errorMessage":"environment variable {var} is not utf-8","messagePattern":"environment variable (.+?) is not utf-8","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/tools/generate-copyright/src/main.rs","lineNumber":215,"sourceCode":"                })\n            }\n        }\n    }\n}\n\n/// A License has an SPDX license name and a list of copyright holders.\n#[derive(serde::Deserialize, Clone, Debug, PartialEq, Eq)]\nstruct License {\n    spdx: String,\n    copyright: Vec<String>,\n}\n\n/// Grab an environment variable as string, or fail nicely.\nfn env_string(var: &str) -> Result<String, Error> {\n    match std::env::var(var) {\n        Ok(var) => Ok(var),\n        Err(std::env::VarError::NotUnicode(_)) => {\n            anyhow::bail!(\"environment variable {var} is not utf-8\")\n        }\n        Err(std::env::VarError::NotPresent) => anyhow::bail!(\"missing environment variable {var}\"),\n    }\n}\n\n/// Grab an environment variable as a PathBuf, or fail nicely.\nfn env_path(var: &str) -> Result<PathBuf, Error> {\n    if let Some(var) = std::env::var_os(var) {\n        Ok(var.into())\n    } else {\n        anyhow::bail!(\"missing environment variable {var}\")\n    }\n}\n","sourceCodeStart":197,"sourceCodeEnd":229,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/src/tools/generate-copyright/src/main.rs#L197-L229","documentation":"Bailed by env_string when std::env::var returns VarError::NotUnicode — the environment variable exists but its OS string content cannot be converted to valid UTF-8. The tool refuses to guess at a lossy conversion and aborts.","triggerScenarios":"Calling generate-copyright with an environment variable (e.g. a path or license config var) set to a value containing non-UTF-8 bytes. Common on Windows with legacy code pages, or when a var is populated from a binary blob.","commonSituations":"A PATH or config var containing locale-specific bytes on Windows; exporting a var from a script that wrote raw bytes; a var accidentally set to binary file contents.","solutions":["Re-set the variable with a valid UTF-8 value: export VAR='<utf8 value>'.","On Windows, ensure the system/code-page locale is UTF-8 (chcp 65001) or use PowerShell with $env:VAR set to a Unicode string.","Find which process set the non-UTF-8 value and fix the source."],"exampleFix":"// before: only accept strict UTF-8\nErr(std::env::VarError::NotUnicode(_)) =>\n    anyhow::bail!(\"environment variable {var} is not utf-8\"),\n\n// after: fall back to OsString->PathBuf for path-like vars\nErr(std::env::VarError::NotUnicode(os)) => {\n    tracing::warn!(\"environment variable {var} is not utf-8; using raw OS string\");\n    Ok(os.to_string_lossy().into_owned())\n}","handlingStrategy":"validation","validationCode":"// Validate env vars are UTF-8 before the tool runs, with a clear message:\nfn ensure_utf8_env(vars: &[&str]) -> Result<(), String> {\n    for v in vars {\n        if let Some(val) = std::env::var_os(v) {\n            if val.to_str().is_none() {\n                return Err(format!(\"env var {v} is not valid UTF-8\"));\n            }\n        }\n    }\n    Ok(())\n}","typeGuard":"null","tryCatchPattern":"// Wrap env_string callers when extending the tool:\nmatch env_string(var) {\n    Ok(s) => s,\n    Err(e) if e.to_string().contains(\"not utf-8\") => {\n        // last-resort: use the raw OS string lossily, or surface to user\n        return Err(e);\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Set env vars via UTF-8 sources (export from a UTF-8 script, or PowerShell $env:).","On Windows, prefer a UTF-8 system locale for build tooling.","Audit wrapper scripts that copy binary data into env vars."],"tags":["environment","utf8","encoding","generate-copyright"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}