BoundaryML/baml · error · anyhow::Error
Failed to read file
Error message
Failed to read file {}: {} What it means
BamlProject.load_files throws this when std::fs::read_to_string fails for a workspace file, wrapping the path and the OS error. The file was found by the directory walk but could not be read as UTF-8 text — usually due to permissions, the file being deleted between listing and reading, or binary/invalid-UTF-8 content.
Solutions
- Restore read permission on the file reported in the error (chmod +r or fix ownership)
- Re-run the operation after concurrent writers (git checkout, build) finish — the file may have disappeared mid-scan
- Convert or exclude non-UTF-8/binary files from the workspace directory
- Check the file still exists; recreate it from version control if deleted
Example fix
// before -rw------- prompt.baml # read fails for other users // after $ chmod 644 prompt.baml
Defensive patterns
Strategy: try-catch
Validate before calling
// Pre-check before the server loads files:
match std::fs::read_to_string(&path) {
Ok(_) => {},
Err(e) => eprintln!("Unreadable file {}: {e}", path.display()),
} Try / catch
match project.load_files() {
Err(e) if e.to_string().starts_with("Failed to read file") => {
eprintln!("Fix permissions/encoding for the reported file, then retry: {e}");
}
r => r,
} Prevention
- Ensure all workspace files are UTF-8 text with read permission for the LSP user
- Avoid editing files while bulk operations (git checkout, codegen) are running
- Exclude binary and non-source artifacts from the watched BAML directory
When it happens
Trigger: load_files() reading a gathered file that has no read permission, vanished after gather_files ran (race with git checkout/build tools), or contains invalid UTF-8 (read_to_string rejects it).
Common situations: Editor watching a repo while a branch switch deletes files mid-scan; files with restrictive permissions (root-owned, 000 modes); binary files or files in non-UTF-8 encodings inside the watched directory.
Understand the failure class
Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.
Related errors
- Failed to gather files from directory
- a source root already exists at this path
- BAML Generate failed - Project has errors.
- blob path has no parent
- cannot determine directory of current executable
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/97b84ec925d6e2a4.
Report an issue: GitHub.
Appendix: source
Thrown at engine/language_server/src/baml_project/mod.rs:288
anyhow::anyhow!(
"Failed to gather files from directory {}: {}",
self.root_dir_name.display(),
e
)
})?;
let workspace_files = workspace_file_paths
.into_iter()
.map(|file_path| {
let document_key = DocumentKey::from_path(&self.root_dir_name, &file_path)
.map_err(|e| {
anyhow::anyhow!(
"Failed to create document key for file {}: {}",
file_path.display(),
e
)
})?;
let contents = std::fs::read_to_string(&file_path).map_err(|e| {
anyhow::anyhow!("Failed to read file {}: {}", file_path.display(), e)
})?;
let text_document = TextDocument::new(contents, 0);
Ok((document_key, text_document))
})
.collect::<anyhow::Result<HashMap<_, _>>>()?;
let project_files = workspace_files.clone();
self.files = project_files;
Ok(workspace_files)
}
pub fn list_generators(
&mut self,
feature_flags: &[String],
) -> Result<Vec<BamlGeneratorConfig>, &str> {
let runtime = self.runtime(HashMap::new(), feature_flags);
if let Ok(runtime) = runtime {View on GitHub (pinned to bd85ce9dee)