{"record":{"id":"28e6e55431cec650","repo":"BoundaryML/baml","slug":"undefined-variable-name","errorCode":null,"errorMessage":"undefined variable: {name}","messagePattern":"undefined variable: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/codegen.rs","lineNumber":1213,"sourceCode":"                        };\n\n                        self.emit(Instruction::LoadField(field_index));\n                    }\n\n                    other => panic!(\n                        \"field access must be on classes, but expr `{}` got: {other:?}\",\n                        base.dump_str()\n                    ),\n                }\n            }\n\n            thir::Expr::Var(name, _) => {\n                if let Some(&index) = self.locals.get(name) {\n                    self.emit(Instruction::LoadVar(index));\n                } else if let Some(class) = self.globals.get(name) {\n                    self.emit(Instruction::LoadGlobal(*class));\n                } else {\n                    panic!(\"undefined variable: {name}\");\n                }\n            }\n\n            thir::Expr::List(elements, _) => {\n                for element in elements {\n                    self.compile_expression(element);\n                }\n                self.emit(Instruction::AllocArray(elements.len()));\n            }\n\n            thir::Expr::Map(pairs, _) => {\n                // Maps are not yet implemented in bytecode\n                // have N keys, N values.\n                // keys are popped first, so we first compute the values.\n\n                for (_, value) in pairs {\n                    self.compile_expression(value);\n                }","sourceCodeStart":1195,"sourceCodeEnd":1231,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/codegen.rs#L1195-L1231","documentation":"The BAML compiler's code generator failed to resolve a variable name while compiling an expression. Any name must be either a local variable (compiled into LoadVar) or a global (a class/function registered in globals); if neither lookup succeeds, the compiler panics because the semantic layer should have rejected unresolved names earlier. This represents an identifier that is not in scope at the point of use.","triggerScenarios":"Compiling a BAML expression that references an identifier never declared (typo, use-before-declaration, reference to a symbol from another scope, or a variable only defined inside a branch the compiler path skipped). The name is absent from both self.locals and self.globals in compile_expression.","commonSituations":"Typos in variable names inside expression blocks; referencing a function parameter that was removed or renamed; using a variable defined in a different block scope; mid-refactor states where a declaration was deleted but uses remain.","solutions":["Declare the variable before use (let or the block's parameter list) and fix any spelling mismatch with the declaration","Check that the variable is defined in the same scope where it is referenced; BAML block scoping is lexical","If the symbol should be a global (class or function), confirm it is defined at file scope and imported/visible in this file","If this occurs with valid code, it may be a compiler bug — report it with the .baml source that triggers the panic"],"exampleFix":"// before\nlet prompt = \"Hello \" ++ usermame\n// after\nlet prompt = \"Hello \" ++ username","handlingStrategy":"validation","validationCode":"fn validate_var_defined(name: &str, locals: &HashSet<&str>, globals: &HashSet<&str>) -> Result<(), String> {\n    if locals.contains(name) || globals.contains(name) { Ok(()) }\n    else { Err(format!(\"variable '{}' is not defined in scope\", name)) }\n}","typeGuard":"fn is_defined(name: &str, scope: &Scope) -> bool { scope.locals.contains_key(name) || scope.globals.contains_key(name) }","tryCatchPattern":null,"preventionTips":["Compile early and often so undefined references surface immediately","Keep variable declarations and uses in the same lexical block","Search the file for the identifier before renaming or deleting declarations","Enable editor/lint support for .baml to catch unresolved identifiers before compilation"],"tags":["compiler","codegen","panic","undefined-variable"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}