{"record":{"id":"4235944acf738665","repo":"BoundaryML/baml","slug":"variable-not-found-for-watch-notify","errorCode":null,"errorMessage":"Variable '{}' not found for $watch.notify()","messagePattern":"Variable '(.+?)' not found for \\$watch\\.notify\\(\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":158,"sourceCode":"                .watch_variables\n                .iter()\n                .find(|wv| Arc::ptr_eq(&wv.value_ref, value_ref))\n                .map(|wv| wv.spec.name.clone())\n                .unwrap_or_else(|| var_name.to_string());\n\n            let current_value = value_ref.lock().unwrap();\n            let watch_value = expr_value_to_watch_value(current_value.clone());\n            let notification = crate::watch::WatchNotification::new_var(\n                var_name.to_string(), // variable name\n                channel_name,         // current channel name from WatchSpec\n                watch_value,\n                function_name.to_string(),\n            );\n            watch_handler.lock().unwrap().notify(notification);\n            return Ok(());\n        }\n    }\n    bail!(\"Variable '{}' not found for $watch.notify()\", var_name)\n}\n\nenum EvalValue {\n    Value(BamlValueWithMeta<ExprMetadata>),\n    Reference(Arc<Mutex<BamlValueWithMeta<ExprMetadata>>>),\n    Function(usize, Arc<Block<ExprMetadata>>, ExprMetadata),\n}\n\n#[derive(Debug)]\nenum ControlFlow {\n    Normal(BamlValueWithMeta<ExprMetadata>),\n    Break,\n    Continue,\n    Return(BamlValueWithMeta<ExprMetadata>),\n}\n\n/// Check all @watch variables for changes and fire notifications\n///","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L140-L176","documentation":"The BAML interpreter's $watch.notify() mechanism looks up the named variable in the current scope chain to fire a watch notification. If the variable is not found in any scope, the interpreter returns an error instead of notifying. This means the watch target referenced in the notify call no longer exists (or never existed) in the executing scope.","triggerScenarios":"Executing $watch.notify(some_var) (or equivalent watch handling in handle_statement) where some_var is not registered in the active scopes — e.g. notifying inside a block where the variable was never declared, after it went out of scope, or with a misspelled name.","commonSituations":"Typo in the variable passed to $watch.notify; calling notify from a helper block where the watched variable is out of scope; renaming a watched variable without updating notify calls.","solutions":["Correct the variable name passed to $watch.notify to match a variable in the current scope","Move the notify call into the same block/scope where the variable is declared","Declare the variable before the notify call executes","Verify @watch registration happened for that variable (register_watch_variable) before notifying"],"exampleFix":"// before\n$watch.notify(watchedVale)\n// after\n$watch.notify(watchedValue)","handlingStrategy":"validation","validationCode":"fn validate_watch_target(var: &str, scopes: &[Scope]) -> Result<(), String> {\n    if scopes.iter().rev().any(|s| s.contains(var)) { Ok(()) }\n    else { Err(format!(\"'{}' not in scope for $watch.notify\", var)) }\n}","typeGuard":"fn is_in_scope(name: &str, scopes: &[Scope]) -> bool { scopes.iter().rev().any(|s| s.contains(name)) }","tryCatchPattern":null,"preventionTips":["Only call $watch.notify on variables declared in the same or an enclosing live scope","Keep watch targets and notify calls in sync during renames","Verify @watch registration exists for the variable before notifying"],"tags":["interpreter","watch","scope","undefined-variable"],"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-23T08:17:48.524Z"}