BoundaryML/baml · error · anyhow::Error

readFileRef failure

Error message

readFileRef failure: {}

What it means

The Promise returned by readFileRef rejected, and the rejection was a js_sys::Error whose message is surfaced as "readFileRef failure: {message}". This is the file-read implementation on the JS side failing for the requested BAML source path.

Solutions

  1. Read the embedded JS error message (e.g. ENOENT path) and fix the underlying file issue
  2. Ensure .baml source files are included in the deployment/bundle output
  3. Correct the baml_src root path passed to BAML initialization
  4. Test readFileRef directly in JS with the failing path

Example fix

// before
b.init({}) // relies on default path
// after
b.init({
  bamlSrc: './baml_src',
  readFileRef: async (path) => fs.promises.readFile(path),
})
Defensive patterns

Strategy: try-catch

Validate before calling

// verify the file is readable through the same callback before BAML uses it
try { await readFileRef('baml_src/main.baml'); } catch (e) { throw new Error(`baml source unreadable: ${e.message}`); }

Type guard

const isJsError = (e) => e instanceof Error && typeof e.message === 'string';

Try / catch

try {
  await b.test.run(name);
} catch (e) {
  const m = String(e);
  if (m.includes('readFileRef failure:')) {
    if (m.includes('ENOENT')) console.error('baml file missing from bundle');
    if (m.includes('EACCES')) console.error('permission denied on baml file');
  }
  throw e;
}

Prevention

When it happens

Trigger: JsFuture from the readFileRef promise resolves to Err(err); err.dyn_ref::<js_sys::Error>() succeeds and its .message() converts to a string, e.g. ENOENT from Node fs, permission errors, or browser fetch failures.

Common situations: BAML source path doesn't exist in the deployed bundle (missing .baml files in the build output), wrong root directory configured, or Node fs error like EACCES/EISDIR surfacing through the callback.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/4654f57d77ad061e. Report an issue: GitHub.

Appendix: source

Thrown at engine/baml-schema-wasm/src/runtime_wasm/mod.rs:2385

                            is_windows,
                            path.starts_with("../") || path.starts_with("./")
                        )));
                        path.clone()
                    };

                let null = JsValue::NULL;
                let Ok(read) = get_baml_src_cb.call1(&null, &JsValue::from(adjusted_path)) else {
                    anyhow::bail!("readFileRef did not return a promise");
                };

                let read = JsFuture::from(Promise::unchecked_from_js(read)).await;

                let read = match read {
                    Ok(read) => read,
                    Err(err) => {
                        if let Some(e) = err.dyn_ref::<js_sys::Error>() {
                            if let Some(e_str) = e.message().as_string() {
                                anyhow::bail!("readFileRef failure: {}", e_str);
                            }
                        }

                        anyhow::bail!("readFileRef rejected: {:?}", err);
                    }
                };

                // TODO: how does JsValue -> Uint8Array work without try_from?
                Ok(Uint8Array::from(read).to_vec())
            }
        })
    }))
}

#[cfg(not(target_arch = "wasm32"))]
fn js_fn_to_baml_src_reader(get_baml_src_cb: js_sys::Function) -> BamlSrcReader {
    None
}

View on GitHub (pinned to bd85ce9dee)