{"record":{"id":"3e983c75311902e6","repo":"BoundaryML/baml","slug":"failed-to-create-document-key-for-file","errorCode":null,"errorMessage":"Failed to create document key for file {}: {}","messagePattern":"Failed to create document key for file (.+?): (.+?)","errorType":"error_code","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/language_server/src/baml_project/mod.rs","lineNumber":281,"sourceCode":"        }\n        self.cached_runtime = None;\n    }\n\n    /// Load files into the current state. Also return the newly loaded files.\n    pub fn load_files(&mut self) -> anyhow::Result<HashMap<DocumentKey, TextDocument>> {\n        let workspace_file_paths = gather_files(&self.root_dir_name, false).map_err(|e| {\n            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    }","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_server/src/baml_project/mod.rs#L263-L299","documentation":"BamlProject.load_files throws this when DocumentKey::from_path fails for a gathered file, i.e. a file found under the workspace root cannot be converted into a valid document key (typically because its path cannot be resolved/normalized relative to the root or has an invalid format). The underlying cause is embedded in the message.","triggerScenarios":"load_files() iterating gathered file paths where a path is relative or malformed, escapes the root, or from_path rejects it (invalid URI/path conversion for that file).","commonSituations":"Weird filenames (spaces, unicode, control characters) or unusual symlinks in the project; files generated by tools with pathological names; platform path mismatches (Windows vs Unix separators) in synced workspaces.","solutions":["Identify the offending file from the path embedded in the error message and rename it to a normal, valid path","Remove or fix broken symlinks under the workspace root","Ensure files are on a native filesystem (avoid problematic network mounts) and re-run","Re-gather the project cleanly (delete generated/temp files with unusual names)"],"exampleFix":"// before\n$ mv 'my file?.baml' baml_src/  # invalid chars break DocumentKey::from_path\n// after\n$ mv 'my file?.baml' baml_src/my_file.baml","handlingStrategy":"validation","validationCode":"// Skip files that cannot form a valid document key before load:\nlet valid = DocumentKey::from_path(&root, &path).is_ok();\nif !valid { eprintln!(\"Skipping unresolvable path: {}\", path.display()); }","typeGuard":null,"tryCatchPattern":"match project.load_files() {\n    Err(e) if e.to_string().contains(\"Failed to create document key\") => {\n        eprintln!(\"Bad path in workspace: {e}; rename or remove the file.\");\n    }\n    r => r,\n}","preventionTips":["Use plain, portable filenames (ASCII, no control or reserved characters) in BAML projects","Remove broken symlinks and generated files with pathological names from the workspace","Keep the workspace on a native filesystem with consistent path semantics"],"tags":["rust","language-server","path","document-key"],"backgroundTag":"invalid-argument-format","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"}