{"record":{"id":"d5276b83965b4d66","repo":"rust-lang/cargo","slug":"unexpected-variable-variable-in-build-build-di","errorCode":null,"errorMessage":"unexpected variable `{variable}` in build.build-dir path `{raw_template}`{suggestion}","messagePattern":"unexpected variable `(.+?)` in build\\.build-dir path `(.+?)`(.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/context/mod.rs","lineNumber":799,"sourceCode":"\n        let template_variables = replacements\n            .iter()\n            .map(|(key, _)| key[1..key.len() - 1].to_string())\n            .collect_vec();\n\n        let path = val\n            .resolve_templated_path(self, replacements)\n            .map_err(|e| match e {\n                path::ResolveTemplateError::UnexpectedVariable {\n                    variable,\n                    raw_template,\n                } => {\n                    let mut suggestion = closest_msg(&variable, template_variables.iter(), |key| key, \"template variable\");\n                    if suggestion == \"\" {\n                        let variables = template_variables.iter().map(|v| format!(\"`{{{v}}}`\")).join(\", \");\n                        suggestion = format!(\"\\n\\nhelp: available template variables are {variables}\");\n                    }\n                    anyhow!(\n                            \"unexpected variable `{variable}` in build.build-dir path `{raw_template}`{suggestion}\"\n                        )\n                }\n                path::ResolveTemplateError::UnexpectedBracket { bracket_type, raw_template } => {\n                    let (btype, literal) = match bracket_type {\n                        path::BracketType::Opening => (\"opening\", \"{\"),\n                        path::BracketType::Closing => (\"closing\", \"}\"),\n                    };\n\n                    anyhow!(\n                            \"unexpected {btype} bracket `{literal}` in build.build-dir path `{raw_template}`\"\n                        )\n                }\n            })?;\n\n        // Check if the target directory is set to an empty string in the config.toml file.\n        if val.raw_value().is_empty() {\n            bail!(","sourceCodeStart":781,"sourceCodeEnd":817,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/context/mod.rs#L781-L817","documentation":"Thrown when resolving the build.build-dir configuration key and the template path contains a variable in curly braces that is not one of the supported template variables. Cargo enumerates available variables and provides them in the help text of the error message.","triggerScenarios":"Setting build.build-dir in config.toml (or CARGO_BUILD_BUILD_DIR env var) to a path containing an unsupported template variable, e.g. `{user}/target` when `{user}` is not a valid variable.","commonSituations":"Typo in a template variable name. Using a variable from documentation that was renamed or removed. Assuming a variable exists when it does not.","solutions":["Read the help text in the error message which lists all available template variables","Use only supported variables such as `{cwd}`, `{user}`, `{project}`, etc. as listed in the error output","If you do not need template substitution, use a plain absolute or relative path without curly braces"],"exampleFix":"# before (config.toml)\n[build]\nbuild-dir = \"{usr}/cargo-target\"\n\n# after — use a valid variable or literal path\n[build]\nbuild-dir = \"{user}/cargo-target\"","handlingStrategy":"validation","validationCode":"// Validate build-dir template variables before use\nconst VALID_VARS: &[&str] = &[\"cwd\", \"user\", \"project\"];\nfn validate_build_dir_template(template: &str) -> Result<(), String> {\n    let re = regex::Regex::new(r\"\\{([a-zA-Z_][a-zA-Z0-9_]*)\\}\").unwrap();\n    for cap in re.captures_iter(template) {\n        let var = &cap[1];\n        if !VALID_VARS.contains(&var) {\n            return Err(format!(\"unknown template variable: {{{var}}}\"));\n        }\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check the error message's help text for the list of valid template variables","Avoid using curly braces in build-dir paths unless intentionally using template variables","Test config.toml changes with a dry-run or in a throwaway project first"],"tags":["configuration","build-dir","template"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}