{"record":{"id":"97b84ec925d6e2a4","repo":"BoundaryML/baml","slug":"failed-to-read-file","errorCode":null,"errorMessage":"Failed to read file {}: {}","messagePattern":"Failed to read file (.+?): (.+?)","errorType":"error_code","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/language_server/src/baml_project/mod.rs","lineNumber":288,"sourceCode":"            anyhow::anyhow!(\n                \"Failed to gather files from directory {}: {}\",\n                self.root_dir_name.display(),\n                e\n            )\n        })?;\n        let workspace_files = workspace_file_paths\n            .into_iter()\n            .map(|file_path| {\n                let document_key = DocumentKey::from_path(&self.root_dir_name, &file_path)\n                    .map_err(|e| {\n                        anyhow::anyhow!(\n                            \"Failed to create document key for file {}: {}\",\n                            file_path.display(),\n                            e\n                        )\n                    })?;\n                let contents = std::fs::read_to_string(&file_path).map_err(|e| {\n                    anyhow::anyhow!(\"Failed to read file {}: {}\", file_path.display(), e)\n                })?;\n                let text_document = TextDocument::new(contents, 0);\n                Ok((document_key, text_document))\n            })\n            .collect::<anyhow::Result<HashMap<_, _>>>()?;\n\n        let project_files = workspace_files.clone();\n\n        self.files = project_files;\n        Ok(workspace_files)\n    }\n\n    pub fn list_generators(\n        &mut self,\n        feature_flags: &[String],\n    ) -> Result<Vec<BamlGeneratorConfig>, &str> {\n        let runtime = self.runtime(HashMap::new(), feature_flags);\n        if let Ok(runtime) = runtime {","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_server/src/baml_project/mod.rs#L270-L306","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\n-rw------- prompt.baml   # read fails for other users\n// after\n$ chmod 644 prompt.baml","handlingStrategy":"try-catch","validationCode":"// Pre-check before the server loads files:\nmatch std::fs::read_to_string(&path) {\n    Ok(_) => {},\n    Err(e) => eprintln!(\"Unreadable file {}: {e}\", path.display()),\n}","typeGuard":null,"tryCatchPattern":"match project.load_files() {\n    Err(e) if e.to_string().starts_with(\"Failed to read file\") => {\n        eprintln!(\"Fix permissions/encoding for the reported file, then retry: {e}\");\n    }\n    r => r,\n}","preventionTips":["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"],"tags":["rust","language-server","filesystem","file-read"],"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"}