diff --git a/engine/baml-lib/baml/tests/validation_files/class/invalid_type_aliases.baml b/engine/baml-lib/baml/tests/validation_files/class/invalid_type_aliases.baml index 0a13664ba..e1d584c98 100644 --- a/engine/baml-lib/baml/tests/validation_files/class/invalid_type_aliases.baml +++ b/engine/baml-lib/baml/tests/validation_files/class/invalid_type_aliases.baml @@ -11,6 +11,9 @@ typpe Two = float // Unknown identifier. type Three = i +// Unknown identifier in union. +type Four = int | string | b + // error: Error validating: Unexpected keyword used in assignment: typpe // --> class/invalid_type_aliases.baml:9 // | @@ -29,3 +32,9 @@ type Three = i // 11 | // Unknown identifier. // 12 | type Three = i // | +// error: Error validating: Type alias points to unknown identifier `b` +// --> class/invalid_type_aliases.baml:15 +// | +// 14 | // Unknown identifier in union. +// 15 | type Four = int | string | b +// | diff --git a/engine/baml-lib/parser-database/src/types/mod.rs b/engine/baml-lib/parser-database/src/types/mod.rs index 1978fd078..59f2e73e7 100644 --- a/engine/baml-lib/parser-database/src/types/mod.rs +++ b/engine/baml-lib/parser-database/src/types/mod.rs @@ -518,7 +518,11 @@ fn visit_type_alias<'db>( }; let Some(top_id) = ctx.names.tops.get(&string_id) else { - unreachable!("Alias name `{ident}` is not registered in the context"); + ctx.push_error(DatamodelError::new_validation_error( + &format!("Type alias points to unknown identifier `{ident}`"), + item.span().clone(), + )); + return; }; // Add alias to the graph. diff --git a/engine/baml-schema-wasm/tests/test_file_manager.rs b/engine/baml-schema-wasm/tests/test_file_manager.rs index bdcb841c2..6069e6091 100644 --- a/engine/baml-schema-wasm/tests/test_file_manager.rs +++ b/engine/baml-schema-wasm/tests/test_file_manager.rs @@ -231,7 +231,7 @@ test Two { } #[wasm_bindgen_test] - fn test_type_alias() { + fn test_type_alias_pointing_to_unknown_identifier() { wasm_logger::init(wasm_logger::Config::new(log::Level::Info)); let sample_baml_content = r##" type Foo = i @@ -265,4 +265,76 @@ test Two { // .unwrap() // ); } + + #[wasm_bindgen_test] + fn test_type_alias_pointing_to_union_with_unknown_identifier() { + wasm_logger::init(wasm_logger::Config::new(log::Level::Info)); + let sample_baml_content = r##" + type Foo = int | i + "##; + let mut files = HashMap::new(); + files.insert("error.baml".to_string(), sample_baml_content.to_string()); + let files_js = to_value(&files).unwrap(); + let project = WasmProject::new("baml_src", files_js) + .map_err(JsValue::from) + .unwrap(); + + let env_vars = [("OPENAI_API_KEY", "12345")] + .iter() + .cloned() + .collect::>(); + let env_vars_js = to_value(&env_vars).unwrap(); + + let Err(js_error) = project.runtime(env_vars_js) else { + panic!("Expected error, got Ok"); + }; + + assert!(js_error.is_object()); + + // TODO: Don't know how to build Object + // assert_eq!( + // js_error, + // serde_wasm_bindgen::to_value::>>(&HashMap::from_iter([( + // "all_files".to_string(), + // vec!["error.baml".to_string()] + // )])) + // .unwrap() + // ); + } + + #[wasm_bindgen_test] + fn test_type_alias_pointing_to_union_with_unknown_identifier_in_union() { + wasm_logger::init(wasm_logger::Config::new(log::Level::Info)); + let sample_baml_content = r##" + type Four = int | string | b + "##; + let mut files = HashMap::new(); + files.insert("error.baml".to_string(), sample_baml_content.to_string()); + let files_js = to_value(&files).unwrap(); + let project = WasmProject::new("baml_src", files_js) + .map_err(JsValue::from) + .unwrap(); + + let env_vars = [("OPENAI_API_KEY", "12345")] + .iter() + .cloned() + .collect::>(); + let env_vars_js = to_value(&env_vars).unwrap(); + + let Err(js_error) = project.runtime(env_vars_js) else { + panic!("Expected error, got Ok"); + }; + + assert!(js_error.is_object()); + + // TODO: Don't know how to build Object + // assert_eq!( + // js_error, + // serde_wasm_bindgen::to_value::>>(&HashMap::from_iter([( + // "all_files".to_string(), + // vec!["error.baml".to_string()] + // )])) + // .unwrap() + // ); + } }