{"record":{"id":"b66a7bde23f18f41","repo":"oraios/serena","slug":"file-read-file-path-failed-file-does-not-exis","errorCode":null,"errorMessage":"File read '{file_path}' failed: File does not exist.","messagePattern":"File read '(.+?)' failed: File does not exist\\.","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"src/solidlsp/ls_utils.py","lineNumber":411,"sourceCode":"    Utility functions for file operations.\n    \"\"\"\n\n    ArchiveType = Literal[\"tar\", \"gztar\", \"bztar\", \"xztar\", \"zip\", \"zip.gz\", \"gz\", \"binary\"]\n\n    @staticmethod\n    def read_file(file_path: str, encoding: str) -> str:\n        \"\"\"\n        Reads the file at the given path using the given encoding and returns the contents as a string.\n        If decoding fails, tries to detect the encoding using charset_normalizer.\n\n        Line endings are normalized to LF (universal newlines), irrespective of the encoding\n        used to decode the file.\n\n        Raises FileNotFoundError if the file does not exist.\n        \"\"\"\n        if not os.path.exists(file_path):\n            log.error(f\"Failed to read '{file_path}': File does not exist.\")\n            raise FileNotFoundError(f\"File read '{file_path}' failed: File does not exist.\")\n        try:\n            try:\n                with open(file_path, encoding=encoding) as inp_file:\n                    return inp_file.read()\n            except UnicodeDecodeError as ude:\n                results = charset_normalizer.from_path(file_path)\n                match = results.best()\n                if match:\n                    log.warning(\n                        f\"Could not decode {file_path} with encoding='{encoding}'; using best match '{match.encoding}' instead\",\n                    )\n                    # Decoding the raw bytes bypasses the universal-newline translation that the\n                    # open() call above applies, so normalize explicitly to keep both paths equivalent.\n                    decoded = match.raw.decode(match.encoding)\n                    return decoded.replace(\"\\r\\n\", \"\\n\").replace(\"\\r\", \"\\n\")\n                raise ude\n        except Exception as exc:\n            log.error(f\"Failed to read '{file_path}' with encoding '{encoding}': {exc}\")","sourceCodeStart":393,"sourceCodeEnd":429,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/solidlsp/ls_utils.py#L393-L429","documentation":"FileNotFoundError raised by FileUtils.read_file (src/solidlsp/ls_utils.py:411) when the requested file_path does not exist on disk. The existence check happens before opening, so the message is explicit about the missing path.","triggerScenarios":"Calling read_file (or contents / request_containing_symbol which use it) with a path that was deleted, renamed, never created, or spelled with a wrong relative/absolute prefix.","commonSituations":"Stale file references after refactors; relative paths resolved against the wrong working directory; generated files not yet produced; case-sensitive filesystems with mismatched casing.","solutions":["Check os.path.exists(file_path) and print os.path.abspath to spot wrong working-directory assumptions.","Refresh the file list / recompute the path after renames or deletions.","Verify case matches exactly on case-sensitive filesystems."],"exampleFix":"// before\ncontent = FileUtils.read_file(\"src/Modle.py\")\n// after\npath = \"src/Model.py\"\nif not os.path.exists(path):\n    raise SkipFile(f\"missing: {os.path.abspath(path)}\")\ncontent = FileUtils.read_file(path)","handlingStrategy":"validation","validationCode":"def readable(path: str) -> bool:\n    return os.path.isfile(path) and os.access(path, os.R_OK)","typeGuard":"def resolve_existing(path: str) -> str | None:\n    return path if os.path.exists(path) else None","tryCatchPattern":"try:\n    content = FileUtils.read_file(path)\nexcept FileNotFoundError:\n    log.warning(\"missing file: %s\", os.path.abspath(path))\n    content = None","preventionTips":["Check os.path.exists with an absolute path before reads","Refresh cached file references after renames/deletes","Beware case-sensitivity differences across filesystems","Ensure generated files exist before consumers run"],"tags":["filesystem","file-not-found"],"backgroundTag":"file-not-found","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}