{"record":{"id":"be31a45b7b8c2047","repo":"FoundationAgents/MetaGPT","slug":"file-file-name-not-found","errorCode":null,"errorMessage":"File {file_name} not found.","messagePattern":"File (.+?) not found\\.","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":546,"sourceCode":"\n        ERROR_MSG = f\"[Error editing file {file_name}. Please confirm the file is correct.]\"\n        ERROR_MSG_SUFFIX = (\n            \"Your changes have NOT been applied. Please fix your edit command and try again.\\n\"\n            \"You either need to 1) Open the correct file and try again or 2) Specify the correct line number arguments.\\n\"\n            \"DO NOT re-run the same failed edit command. Running it again will lead to the same error.\"\n        )\n\n        if not self._is_valid_filename(file_name.name):\n            raise FileNotFoundError(\"Invalid file name.\")\n\n        if not self._is_valid_path(file_name):\n            raise FileNotFoundError(\"Invalid path or file name.\")\n\n        if not self._create_paths(file_name):\n            raise PermissionError(\"Could not access or create directories.\")\n\n        if not file_name.is_file():\n            raise FileNotFoundError(f\"File {file_name} not found.\")\n\n        if is_insert and is_append:\n            raise ValueError(\"Cannot insert and append at the same time.\")\n\n        # Use a temporary file to write changes\n        content = str(content or \"\")\n        temp_file_path = \"\"\n        src_abs_path = file_name.resolve()\n        first_error_line = None\n        # The file to store previous content and will be removed automatically.\n        temp_backup_file = tempfile.NamedTemporaryFile(\"w\", delete=True)\n\n        try:\n            # lint the original file\n            # enable_auto_lint = os.getenv(\"ENABLE_AUTO_LINT\", \"false\").lower() == \"true\"\n            if self.enable_auto_lint:\n                original_lint_error, _ = self._lint_file(file_name)\n","sourceCodeStart":528,"sourceCodeEnd":564,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L528-L564","documentation":"Raised by Editor._edit_file_impl after name/path/permission checks pass but the target path is still not an existing regular file. Edit is strictly for existing files: it reads, transforms, and atomically rewrites content, so a missing file is an error rather than an implicit create.","triggerScenarios":"editor.edit('new_file.py', ...) where new_file.py was never created; the file was deleted between open_file and edit; a race where another process removed it; wrong extension/case in the path.","commonSituations":"Agents trying to 'edit' a file they only planned to write; stale references to deleted files; case-sensitivity mistakes on Linux.","solutions":["Use editor.create_file(path) first for new files, then edit","Verify with Path(p).is_file() right before editing","If the file was expected to exist, find where it went (renamed/deleted) and open the correct path"],"exampleFix":"// before\neditor.edit('src/new_module.py', start=1, end=1, content='x = 1\\n')  # FileNotFoundError\n\n// after\nif not Path('src/new_module.py').is_file():\n    editor.create_file('src/new_module.py')\neditor.edit('src/new_module.py', start=1, end=1, content='x = 1\\n')","handlingStrategy":"validation","validationCode":"p = Path(file_name)\nif not p.is_file():\n    editor.create_file(str(p))\neditor.edit(str(p), start=s, end=e, content=c)","typeGuard":"def editable_file(p: str) -> bool:\n    return Path(p).is_file()","tryCatchPattern":"try:\n    editor.edit(path, ...)\nexcept FileNotFoundError as e:\n    if 'not found' in str(e):\n        editor.create_file(path)\n        editor.edit(path, ...)\n    else:\n        raise","preventionTips":["create_file first, edit second — the edit API never creates","Recheck existence right before editing if other processes touch the tree","Watch for case-sensitivity errors in filenames on Linux"],"tags":["editor","filesystem","metagpt"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}