{"record":{"id":"ed111f7321288829","repo":"BoundaryML/baml","slug":"watch-codegen-error-undefined-variable-variable","errorCode":null,"errorMessage":"watch codegen error: undefined variable: {variable}","messagePattern":"watch codegen error: undefined variable: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/codegen.rs","lineNumber":967,"sourceCode":"                    self.emit(Instruction::Jump(loop_start - self.next_insn_index()));\n\n                    for loc in break_locs {\n                        self.patch_jump(loc);\n                    }\n                }\n            },\n            thir::Statement::Assert { condition, .. } => {\n                self.compile_expression(condition);\n                self.emit(Instruction::Assert);\n            }\n            thir::Statement::WatchOptions {\n                variable,\n                channel,\n                when,\n                ..\n            } => {\n                let Some(local_index) = self.locals.get(variable).copied() else {\n                    panic!(\"watch codegen error: undefined variable: {variable}\");\n                };\n\n                self.emit_string_literal(channel.as_ref().unwrap_or(variable).as_str()); // This adds LoadConst\n\n                match when.as_ref() {\n                    Some(WatchWhen::Manual) => {\n                        self.emit_string_literal(\"manual\");\n                    }\n\n                    Some(WatchWhen::Never) => {\n                        self.emit_string_literal(\"never\");\n                    }\n\n                    Some(WatchWhen::Auto) => {\n                        // No action needed.\n                    }\n\n                    Some(WatchWhen::FunctionName(fn_name)) => {","sourceCodeStart":949,"sourceCodeEnd":985,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/codegen.rs#L949-L985","documentation":"A panic during codegen of a Watch statement: the watched variable must be present in the compiler's locals map to get a slot index. The panic fires when the variable named in the watch statement has no corresponding local.","triggerScenarios":"Watching a variable that was never declared, is out of scope at the watch point, or whose name is misspelled in the watch statement.","commonSituations":"Typo'd variable names in watch directives; watching a variable declared in a different block/scope; declarations removed by refactoring while watch statements remained.","solutions":["Ensure the watched variable is declared before the watch statement in the same scope","Fix the variable name spelling in the watch statement","Remove the stale watch statement if the variable no longer exists"],"exampleFix":"// before\nwatch counter via \"ui\"   // 'counter' not declared in scope\n// after\nlet counter = 0\nwatch counter via \"ui\"","handlingStrategy":"validation","validationCode":"fn validate_watch_var(locals: &[&str], var: &str) -> Result<(), String> {\n    if !locals.contains(&var) {\n        return Err(format!(\"watched variable '{}' is not declared in scope\", var));\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Declare watched variables before the watch statement in the same scope","Check spelling of watched variable names","Remove watch statements for variables that were deleted"],"tags":["rust","compiler","codegen","panic","watch"],"backgroundTag":"resource-not-found","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}