{"record":{"id":"0e3965fafd9f22c4","repo":"BoundaryML/baml","slug":"watch-options-codegen-undefined-function-fn-name","errorCode":null,"errorMessage":"watch options codegen: undefined function: {fn_name}","messagePattern":"watch options codegen: undefined function: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/codegen.rs","lineNumber":989,"sourceCode":"\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)) => {\n                        if let Some(&index) = self.globals.get(fn_name.name()) {\n                            self.emit(Instruction::LoadGlobal(index));\n                        } else {\n                            panic!(\"watch options codegen: undefined function: {fn_name}\");\n                        }\n                    }\n\n                    None => {\n                        let index = self.add_constant(Value::Null);\n                        self.emit(Instruction::LoadConst(index));\n                    }\n                }\n\n                self.emit(Instruction::Watch(local_index));\n            }\n            thir::Statement::WatchNotify { variable, .. } => {\n                let Some(local_index) = self.locals.get(variable).copied() else {\n                    panic!(\"watch codegen error: undefined variable: {variable}\");\n                };\n\n                self.emit(Instruction::Notify(local_index));\n            }","sourceCodeStart":971,"sourceCodeEnd":1007,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/codegen.rs#L971-L1007","documentation":"This is a compiler-internal panic raised during bytecode generation for a `watch` statement whose callback was specified by function name. Before emitting `LoadGlobal`, the codegen looks the function name up in the current global symbol table; if the name is absent, the THIR passed name-checking but codegen cannot resolve it, which is treated as an unrecoverable compiler invariant violation rather than a user-facing diagnostic.","triggerScenarios":"Compiling a BAML function whose `watch ... when <fn_name>` option references a function name that is not present in `self.globals` — e.g. the watch callback names a function that was never defined, was renamed, or was not registered as a global during earlier compile phases.","commonSituations":"Typos in the watch callback function name; referencing a function defined in another file that was not imported/registered; refactoring a function name without updating `watch` options; compiler bugs where name resolution and globals registration disagree.","solutions":["Check the function name used in the `watch` options and correct any typo so it matches a defined function","Ensure the referenced function is defined in the same compilation unit and registered before the watch statement is compiled","Update stale references after renaming the function","If the name is legitimately defined, file a compiler bug: this indicates name resolution and codegen globals are out of sync"],"exampleFix":"// before\nwatch stream when myWtachFn(x)\n// after\nwatch stream when myWatchFn(x)","handlingStrategy":"validation","validationCode":"// Before compiling, verify every watch-callback name resolves\nfn validate_watch_fns(src: &str, defined_fns: &HashSet<&str>) -> Result<(), String> {\n    for name in extract_watch_fn_names(src) {\n        if !defined_fns.contains(name.as_str()) {\n            return Err(format!(\"watch callback `{name}` is not a defined function\"));\n        }\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Grep for `watch` option callbacks and diff them against your defined function names before compiling","Rename functions via tooling (LSP rename) so watch references update automatically","Keep watch callbacks in the same file/unit that registers globals"],"tags":["compiler","codegen","panic","watch"],"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-14T05:17:10.506Z"}