{"record":{"id":"3c59e47ce8b21eda","repo":"rust-lang/rust","slug":"missing-environment-variable-var","errorCode":null,"errorMessage":"missing environment variable {var}","messagePattern":"missing environment variable (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/tools/generate-copyright/src/main.rs","lineNumber":217,"sourceCode":"        }\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":199,"sourceCodeEnd":229,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/src/tools/generate-copyright/src/main.rs#L199-L229","documentation":"Bailed by env_string when std::env::var returns VarError::NotPresent — the required environment variable is not set in the process environment at all. generate-copyright delegates some required inputs through env vars and treats them as mandatory.","triggerScenarios":"Running generate-copyright without one of its required environment variables set (whatever var name is passed to env_string at the failing call site).","commonSituations":"Running the tool manually instead of through its usual xtask/wrapper that sets the vars; a wrapper script that skips setting a var on a certain platform; CI environment that doesn't forward the var.","solutions":["Identify the required var from the error message and set it: export <VAR>=<value>.","Run the tool through its intended wrapper (e.g. the rustdoc/xtask entrypoint) rather than invoking the binary directly.","Check the tool's README/help for the list of required env vars."],"exampleFix":"// before: hard requirement, no guidance\nErr(std::env::VarError::NotPresent) =>\n    anyhow::bail!(\"missing environment variable {var}\"),\n\n// after: include a hint on how to set it\nErr(std::env::VarError::NotPresent) => anyhow::bail!(\n    \"missing environment variable {var}; set it via `{var}=<value>` \\\n     or run through `./x.py run generate-copyright`\"\n)","handlingStrategy":"validation","validationCode":"// Pre-flight: assert required env vars exist before running generate-copyright:\nconst REQUIRED: &[&str] = &[\"LICENSES_DIR\", \"SRC_DIR\"];\nfor v in REQUIRED {\n    if std::env::var_os(v).is_none() {\n        eprintln!(\"missing required env var {v}\"); std::process::exit(1);\n    }\n}","typeGuard":"null","tryCatchPattern":"match env_string(var) {\n    Ok(s) => Ok(s),\n    Err(e) => {\n        eprintln!(\"{e}. Set it with: export {var}=<value>\");\n        Err(e)\n    }\n}","preventionTips":["Run the tool through its xtask wrapper which sets every required var.","Document the required env vars in one place (README) and keep it in sync.","Add a CI check that asserts all required vars are present."],"tags":["environment","configuration","generate-copyright"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}