{"record":{"id":"a56c02a3d447e814","repo":"BoundaryML/baml","slug":"readfileref-did-not-return-a-promise","errorCode":null,"errorMessage":"readFileRef did not return a promise","messagePattern":"readFileRef did not return a promise","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-schema-wasm/src/runtime_wasm/mod.rs","lineNumber":2375,"sourceCode":"                    if is_windows && (path.starts_with(\"../\") || path.starts_with(\"./\")) {\n                        let result = format!(\"baml_src/{path}\");\n                        web_sys::console::log_1(&wasm_bindgen::JsValue::from_str(&format!(\n                            \"WASM Windows path fix applied: '{path}' → '{result}'\"\n                        )));\n                        result\n                    } else {\n                        web_sys::console::log_1(&wasm_bindgen::JsValue::from_str(&format!(\n                            \"WASM path unchanged: '{}' (windows={}, relative={})\",\n                            path,\n                            is_windows,\n                            path.starts_with(\"../\") || path.starts_with(\"./\")\n                        )));\n                        path.clone()\n                    };\n\n                let null = JsValue::NULL;\n                let Ok(read) = get_baml_src_cb.call1(&null, &JsValue::from(adjusted_path)) else {\n                    anyhow::bail!(\"readFileRef did not return a promise\");\n                };\n\n                let read = JsFuture::from(Promise::unchecked_from_js(read)).await;\n\n                let read = match read {\n                    Ok(read) => read,\n                    Err(err) => {\n                        if let Some(e) = err.dyn_ref::<js_sys::Error>() {\n                            if let Some(e_str) = e.message().as_string() {\n                                anyhow::bail!(\"readFileRef failure: {}\", e_str);\n                            }\n                        }\n\n                        anyhow::bail!(\"readFileRef rejected: {:?}\", err);\n                    }\n                };\n\n                // TODO: how does JsValue -> Uint8Array work without try_from?","sourceCodeStart":2357,"sourceCodeEnd":2393,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-schema-wasm/src/runtime_wasm/mod.rs#L2357-L2393","documentation":"The readFileRef callback injected from JS (get_baml_src_cb) was expected to return a Promise for the given adjusted path, but calling it returned null/undefined or threw synchronously. The Rust WASM runtime bails so it cannot read the BAML source file asynchronously.","triggerScenarios":"invoke get_baml_src_cb.call1(...) in the wasm runtime when the JS host's readFileRef is not a function returning a Promise, is undefined, or throws before returning (e.g. invalid path rejected synchronously).","commonSituations":"Using BAML in a JS/TS environment (Next.js, Vite, browser) where the readFileRef option is misconfigured — passed a sync function, an async wrapper mismatch, or omitted so the callback misbehaves.","solutions":["Ensure the readFileRef option is an async function returning a Promise<ArrayBuffer|Uint8Array>","Confirm readFileRef is registered/defined before BAML runtime initialization","Validate the path passed to readFileRef resolves in the host environment","Check that the callback doesn't throw synchronously before returning the promise"],"exampleFix":"// before\nb.init({ readFileRef: (path) => fs.readFileSync(path) })\n// after\nb.init({ readFileRef: async (path) => fs.promises.readFile(path) })","handlingStrategy":"validation","validationCode":"// validate readFileRef before initializing BAML\nif (typeof opts.readFileRef !== 'function') throw new Error('readFileRef must be a function');\nconst probe = opts.readFileRef('probe.baml');\nif (!(probe instanceof Promise)) throw new Error('readFileRef must return a Promise (make it async)');","typeGuard":"const isReadFileRef = (f) => typeof f === 'function' && f('x') instanceof Promise;","tryCatchPattern":"try {\n  await b.init(opts);\n} catch (e) {\n  if (String(e).includes('readFileRef did not return a promise')) {\n    console.error('readFileRef must be async and return a Promise');\n  }\n  throw e;\n}","preventionTips":["Always declare readFileRef as an async function returning a Promise<Uint8Array|ArrayBuffer>","Register readFileRef before any BAML runtime call","Smoke-test readFileRef with one real path at startup"],"tags":["wasm","javascript","file-read","async"],"backgroundTag":"file-read-failed","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"}