rust-lang/rust · error · anyhow::Error
missing environment variable {var}
Error message
missing environment variable {var} What it means
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.
Source
Thrown at src/tools/generate-copyright/src/main.rs:217
}
}
}
/// A License has an SPDX license name and a list of copyright holders.
#[derive(serde::Deserialize, Clone, Debug, PartialEq, Eq)]
struct License {
spdx: String,
copyright: Vec<String>,
}
/// Grab an environment variable as string, or fail nicely.
fn env_string(var: &str) -> Result<String, Error> {
match std::env::var(var) {
Ok(var) => Ok(var),
Err(std::env::VarError::NotUnicode(_)) => {
anyhow::bail!("environment variable {var} is not utf-8")
}
Err(std::env::VarError::NotPresent) => anyhow::bail!("missing environment variable {var}"),
}
}
/// Grab an environment variable as a PathBuf, or fail nicely.
fn env_path(var: &str) -> Result<PathBuf, Error> {
if let Some(var) = std::env::var_os(var) {
Ok(var.into())
} else {
anyhow::bail!("missing environment variable {var}")
}
}
View on GitHub (pinned to 7088e4b63a)
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.
Example fix
// before: hard requirement, no guidance
Err(std::env::VarError::NotPresent) =>
anyhow::bail!("missing environment variable {var}"),
// after: include a hint on how to set it
Err(std::env::VarError::NotPresent) => anyhow::bail!(
"missing environment variable {var}; set it via `{var}=<value>` \
or run through `./x.py run generate-copyright`"
) Defensive patterns
Strategy: validation
Validate before calling
// Pre-flight: assert required env vars exist before running generate-copyright:
const REQUIRED: &[&str] = &["LICENSES_DIR", "SRC_DIR"];
for v in REQUIRED {
if std::env::var_os(v).is_none() {
eprintln!("missing required env var {v}"); std::process::exit(1);
}
} Type guard
null
Try / catch
match env_string(var) {
Ok(s) => Ok(s),
Err(e) => {
eprintln!("{e}. Set it with: export {var}=<value>");
Err(e)
}
} Prevention
- 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.
When it happens
Trigger: Running generate-copyright without one of its required environment variables set (whatever var name is passed to env_string at the failing call site).
Common situations: 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.
Related errors
- environment variable {var} is not utf-8
- rust-analyzer Language Server is not available. Please, ensu
- Failed to execute ${path} --version.
- Unrecognized config profile '{}'. Check src/bootstrap/defaul
- Cannot determine the type of workflow that is being executed
AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10).
Data as JSON: /api/errors/3c59e47ce8b21eda.
Report an issue: GitHub.