BoundaryML/baml · error
watch codegen error: undefined variable: {variable}
Error message
watch codegen error: undefined variable: {variable} What it means
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.
Source
Thrown at engine/baml-compiler/src/codegen.rs:967
self.emit(Instruction::Jump(loop_start - self.next_insn_index()));
for loc in break_locs {
self.patch_jump(loc);
}
}
},
thir::Statement::Assert { condition, .. } => {
self.compile_expression(condition);
self.emit(Instruction::Assert);
}
thir::Statement::WatchOptions {
variable,
channel,
when,
..
} => {
let Some(local_index) = self.locals.get(variable).copied() else {
panic!("watch codegen error: undefined variable: {variable}");
};
self.emit_string_literal(channel.as_ref().unwrap_or(variable).as_str()); // This adds LoadConst
match when.as_ref() {
Some(WatchWhen::Manual) => {
self.emit_string_literal("manual");
}
Some(WatchWhen::Never) => {
self.emit_string_literal("never");
}
Some(WatchWhen::Auto) => {
// No action needed.
}
Some(WatchWhen::FunctionName(fn_name)) => {View on GitHub (pinned to bd85ce9dee)
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
Example fix
// before watch counter via "ui" // 'counter' not declared in scope // after let counter = 0 watch counter via "ui"
Defensive patterns
Strategy: validation
Validate before calling
fn validate_watch_var(locals: &[&str], var: &str) -> Result<(), String> {
if !locals.contains(&var) {
return Err(format!("watched variable '{}' is not declared in scope", var));
}
Ok(())
} Prevention
- 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
When it happens
Trigger: Watching a variable that was never declared, is out of scope at the watch point, or whose name is misspelled in the watch statement.
Common situations: Typo'd variable names in watch directives; watching a variable declared in a different block/scope; declarations removed by refactoring while watch statements remained.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- undefined function: {name}
- undefined field: {class_name}.{field}
- array access should be either map or array.
- watch options codegen: undefined function: {fn_name}
- sys_op callee must resolve to a statically-known global func
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/ed111f7321288829.
Report an issue: GitHub.