{"record":{"id":"8e32af4b5941a864","repo":"BoundaryML/baml","slug":"undefined-global-item-name-str","errorCode":null,"errorMessage":"undefined global item: {name_str}","messagePattern":"undefined global item: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_compiler2_emit/src/emit.rs","lineNumber":2185,"sourceCode":"            Constant::Function(item_ref) => {\n                // A plain function reference as a VALUE. Pooled exactly like\n                // `Constant::GenericFunction`, with EMPTY type args: every\n                // function-pointer value on the heap is a wrapper object\n                // (`GenericFunction`/`Closure`/`BoundMethod`/`HostClosure`),\n                // and a raw `Object::Function` is never a data value — the\n                // invariant `value_concrete_ty` / `callable_signature` rely\n                // on. Interning keeps `greet === greet` pointer-stable, as a\n                // direct `LoadGlobal` of the function object did before.\n                self.emit_pooled_function_value(item_ref, &[]);\n            }\n            Constant::GlobalItem(item_ref) => {\n                // A non-function global item (a client, a top-level `let`,\n                // ...): read the value `$init` stored in its slot, unwrapped.\n                let name_str = item_ref.to_string();\n                let global_idx = *self\n                    .globals\n                    .get(&name_str)\n                    .unwrap_or_else(|| panic!(\"undefined global item: {name_str}\"));\n                self.references.record(&name_str);\n                let inst = self.emit(Instruction::LoadGlobal(GlobalIndex::from_raw(global_idx)));\n                self.set_operand(inst, OperandMeta::Global(name_str));\n            }\n            Constant::GenericFunction { item, type_args } => {\n                // `foo<int>` as a value: the same pooled wrapper, carrying its\n                // concrete type arguments so calling it seeds `frame.type_args`.\n                self.emit_pooled_function_value(item, type_args);\n            }\n            Constant::EnumVariant { enum_ref, variant } => {\n                let enum_name_str = enum_ref.to_string();\n                // Gracefully handle undefined enum references (e.g. cross-package\n                // references that aren't registered in this compilation context).\n                // Emit a Null constant so tests don't panic; runtime will fail\n                // if the code path is actually executed.\n                let enum_obj_idx = self.enum_object_indices.get(&enum_name_str).copied();\n                if enum_obj_idx.is_some() {\n                    self.references.record(&enum_name_str);","sourceCodeStart":2167,"sourceCodeEnd":2203,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_compiler2_emit/src/emit.rs#L2167-L2203","documentation":"When emitting a `Constant::GlobalItem` (a non-function global such as a client or a top-level `let`), the compiler reads the global slot assigned by the `$init` pass from the `globals` map keyed by the item's rendered name. This panic fires when the name is absent — the global was never assigned a slot in the current compilation unit. It is effectively an \"undefined global\" compiler-internal error, analogous to an unresolved symbol at link time.","triggerScenarios":"Referencing a top-level `let` or client from code being emitted when that item's slot is missing — e.g. the global lives in a package not included in this compilation context, the global was removed but a stale incremental fragment still references it, or the rendered name key diverges between slot assignment and lookup.","commonSituations":"Cross-package references to clients/top-level values not registered in this compile; stale incremental state after deleting or renaming a client or top-level `let`; compiler refactors changing `ItemRef::to_string` so the two maps use different key spellings.","solutions":["Ensure the referenced client/top-level `let` is defined in a package included in the current compilation","Clear incremental caches and do a full rebuild so `$init` re-slots all globals","Check for renames/typos of the global name in the source","If the name comes from another package, confirm cross-package registration/exports are set up","Report a compiler bug if a defined global still triggers the panic (key-mismatch regression)"],"exampleFix":"// before (BAML referencing a client that isn't in the compiled set)\nclient MyClient { ... } // defined in another, uncompiled package\n// after\n// include the package defining MyClient in the compilation, or define it locally:\nclient MyClient { provider \"openai\" options { model \"gpt-4o\" } }","handlingStrategy":"validation","validationCode":"// pre-flight: every referenced global item must have a slot\nfor name in referenced_global_names {\n    assert!(globals.contains_key(&name), \"global not in compilation unit: {name}\");\n}","typeGuard":null,"tryCatchPattern":"match std::panic::catch_unwind(|| compile(inputs)) {\n    Ok(out) => out,\n    Err(_) => {\n        clean_incremental_cache();\n        compile(inputs) // full rebuild after stale-state panic\n    }\n}","preventionTips":["Define referenced clients and top-level lets inside the compiled package set","Rebuild fully after deleting or renaming globals","Verify cross-package registrations before referencing external clients","Keep ItemRef::to_string rendering stable between slot assignment and lookup"],"tags":["compiler","undefined-global","emit","symbol-resolution"],"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"}