{"record":{"id":"67d43937e17201d3","repo":"BoundaryML/baml","slug":"error","errorCode":null,"errorMessage":"{}: {error}","messagePattern":"\\{\\}: \\{error\\}","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bridge_wasm/src/wasm_vfs.rs","lineNumber":259,"sourceCode":"                    frontier.push(child);\n                } else if child.extension().is_some_and(|ext| ext == \"baml\") {\n                    files.push(child);\n                }\n            }\n        }\n        files.sort();\n        files\n    }\n}\n\nimpl ProjectFs for WasmProjectFs {\n    fn read_to_string(&self, path: &Path) -> std::io::Result<String> {\n        let bytes = self\n            .vfs()\n            .vfs_read_file(&path.to_string_lossy())\n            .map_err(|error| std::io::Error::other(format!(\"{}: {error:?}\", path.display())))?;\n        String::from_utf8(bytes.to_vec()).map_err(|error| {\n            std::io::Error::new(\n                std::io::ErrorKind::InvalidData,\n                format!(\"{}: {error}\", path.display()),\n            )\n        })\n    }\n\n    fn discover_roots(&self, folder: &Path) -> Vec<DiscoveredRoot> {\n        let mut roots = Vec::new();\n        // The folder itself may sit inside a project (the host opened\n        // `baml_src/`, or a subdirectory of it).\n        roots.extend(\n            baml_db::project_resolution::find_baml_project_root_from_ancestors(\n                folder.ancestors().map(Path::to_path_buf),\n                |dir| self.is_file(&dir.join(baml_db::project_resolution::BAML_TOML)),\n                |dir| self.is_dir(&dir.join(baml_db::project_resolution::BAML_SRC_DIR)),\n            ),\n        );\n        roots.extend(self.marked_roots(folder));","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bridge_wasm/src/wasm_vfs.rs#L241-L277","documentation":"read_to_string in the WASM VFS reads a file via vfs_read_file and maps any backend error to io::Error::other('{path}: {error:?}'); it then separately maps UTF-8 decode failures to InvalidData '{path}: {error}'. So this message means either the virtual file could not be read (backend error string appended) or its bytes are not valid UTF-8.","triggerScenarios":"Calling read_to_string on a path missing from the in-memory VFS, a backend I/O failure, or on binary (non-UTF-8) files that must not be read as strings.","commonSituations":"Loading BAML sources from a browser-injected VFS where the file was never registered, or accidentally reading .bmx/binary assets with read_to_string instead of a bytes API.","solutions":["Verify the path exists in the VFS (write/upload it) before reading","Use a bytes/read_file API for binary content and only call read_to_string for UTF-8 text","Log the mapped error's trailing backend message to identify the underlying vfs_read_file failure","Normalize the path (absolute, no duplicate slashes) as the VFS keys on exact strings"],"exampleFix":"// before\nconst text = vfs.read_to_string('/assets/logo.png'); // InvalidData\n// after\nconst bytes = vfs.read_file('/assets/logo.png');\nconst text = isTextual(path) ? new TextDecoder().decode(bytes) : null;","handlingStrategy":"try-catch","validationCode":"function fileExistsInVfs(vfs, path) {\n  return vfs.vfs_list_dir(dirname(path)).includes(basename(path));\n}","typeGuard":"function isVfsReadError(e) {\n  return e instanceof Error && /: /.test(e.message) && (e.name === 'Error' || e.name === 'NotFoundError' || e.name === 'InvalidData');\n}","tryCatchPattern":"try {\n  text = vfs.read_to_string(path);\n} catch (e) {\n  if (String(e.message).includes('stream did not contain valid UTF-8')) {\n    const bytes = vfs.vfs_read_file(path); // handle as binary\n  } else {\n    throw new Error(`VFS read failed for ${path}: ${e.message}`);\n  }\n}","preventionTips":["Register every file in the VFS before reading it","Only call read_to_string on known-textual paths","Use a bytes API for binary assets","Normalize paths to the exact string form the VFS keys on"],"tags":["wasm","vfs","io","encoding"],"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-14T05:17:10.506Z"}