{"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":null,"httpStatus":null,"severity":"error","filePath":"src/context/mod.rs","lineNumber":810,"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":792,"sourceCodeEnd":828,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/context/mod.rs#L792-L828","documentation":"custom_build_dir (mod.rs:762-824) resolves the `build.build-dir` config value, which supports a limited set of template variables: `{workspace-root}`, `{cargo-cache-home}`, `{workspace-path-hash}`. If the template contains any other `{...}` token, resolve_templated_path returns UnexpectedVariable and Cargo bails 'unexpected variable `<var>` in build.build-dir path `<template>`<suggestion>', appending either a closest-match hint or a list of available variables.","triggerScenarios":"Setting `build.build-dir = \"/some/{name}/target\"` in config.toml where `{name}` is not one of the supported template variables. Also a literal `{` intended as a path char interpreted as a variable start.","commonSituations":"Trying to template build-dir on the package name or a custom variable; typo in a supported variable name like `{workspace-root}` misspelled.","solutions":["Use only the supported variables: `{workspace-root}`, `{cargo-cache-home}`, `{workspace-path-hash}` (see the help line in the error).","If you need a literal path with braces, avoid the `{...}` shape or escape per the docs.","Fix typos based on the closest-match suggestion in the message."],"exampleFix":"# before (.cargo/config.toml)\n[build]\nbuild-dir = \"/cache/{name}/target\"   # {name} unsupported\n# after\n[build]\nbuild-dir = \"/cache/{workspace-path-hash}/target\"","handlingStrategy":"validation","validationCode":"# Validate build-dir uses only supported template variables:\npython3 - <<'EOF'\nimport tomllib, re, sys\ntry:\n    d = tomllib.load(open('.cargo/config.toml','rb'))\nexcept FileNotFoundError: sys.exit(0)\nbd = d.get('build',{}).get('build-dir')\nif bd:\n    allowed = {'{workspace-root}','{cargo-cache-home}','{workspace-path-hash}'}\n    found = set(re.findall(r'\\{[^}]*\\}', bd))\n    bad = found - allowed\n    assert not bad, f'unsupported build-dir variables: {bad}'\nEOF","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Restrict build-dir template variables to the documented set.","Lint .cargo/config.toml in CI for unsupported variables.","Read the suggestion line in the error; it lists valid variables."],"tags":["config","build-dir","template","path"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}