{"record":{"id":"39ff0f953071a394","repo":"FoundationAgents/MetaGPT","slug":"no-file-open-use-the-open-file-function-first","errorCode":null,"errorMessage":"No file open. Use the open_file function first.","messagePattern":"No file open\\. Use the open_file function first\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":195,"sourceCode":"        try:\n            return path.exists()\n        except PermissionError:\n            return False\n\n    @staticmethod\n    def _create_paths(file_path: Path) -> bool:\n        try:\n            if file_path.parent:\n                file_path.parent.mkdir(parents=True, exist_ok=True)\n            return True\n        except PermissionError:\n            return False\n\n    def _check_current_file(self, file_path: Optional[Path] = None) -> bool:\n        if file_path is None:\n            file_path = self.current_file\n        if not file_path or not file_path.is_file():\n            raise ValueError(\"No file open. Use the open_file function first.\")\n        return True\n\n    @staticmethod\n    def _clamp(value, min_value, max_value):\n        return max(min_value, min(value, max_value))\n\n    def _lint_file(self, file_path: Path) -> tuple[Optional[str], Optional[int]]:\n        \"\"\"Lint the file at the given path and return a tuple with a boolean indicating if there are errors,\n        and the line number of the first error, if any.\n\n        Returns:\n            tuple[str | None, int | None]: (lint_error, first_error_line_number)\n        \"\"\"\n\n        linter = Linter(root=self.working_dir)\n        lint_error = linter.lint(str(file_path))\n        if not lint_error:\n            # Linting successful. No issues found.","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L177-L213","documentation":"Raised by Editor._check_current_file when a stateful editor operation (goto_line, scroll_down, scroll_up, read, edit, etc.) is called before any file has been opened with open_file or created with create_file. The editor tracks self.current_file, and this guard rejects the None/missing state. It is a usage-order error, not a filesystem error.","triggerScenarios":"Calling editor.goto_line(10), editor.scroll_down(), editor.read(...), or any method that starts with self._check_current_file() on a fresh Editor() instance, or after the previously opened file was deleted/moved so current_file.is_file() is False.","commonSituations":"Agent pipelines or scripts that instantiate a new Editor per step and forget the open_file call; resuming a session where the file was deleted externally; concurrent runs where another process removed the file between open and edit.","solutions":["Call editor.open_file(path) (or create_file for new files) before goto_line/scroll/read/edit operations","If the file should exist but does not, check the path and recreate/relocate it, then open it again","Persist and restore self.current_file across steps if you reuse the editor in a multi-turn agent loop"],"exampleFix":"// before\neditor = Editor()\neditor.goto_line(5)  # ValueError: No file open\n\n// after\neditor = Editor()\neditor.open_file('src/main.py')\neditor.goto_line(5)","handlingStrategy":"validation","validationCode":"if editor.current_file is None or not editor.current_file.is_file():\n    editor.open_file(str(path))\neditor.goto_line(n)","typeGuard":null,"tryCatchPattern":"try:\n    editor.goto_line(n)\nexcept ValueError as e:\n    if 'No file open' in str(e):\n        editor.open_file(str(path))  # recover and retry once\n    else:\n        raise","preventionTips":["Always pair Editor() construction with an immediate open_file/create_file","In multi-turn agent loops, restore current_file from persisted state before operating","Wrap stateful editor calls in a helper that asserts current_file is set"],"tags":["editor","state","usage-order","metagpt"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}