{"record":{"id":"9efa35b373c9db7d","repo":"FoundationAgents/MetaGPT","slug":"no-file-specified-or-open-use-the-open-file-funct","errorCode":null,"errorMessage":"No file specified or open. Use the open_file function first.","messagePattern":"No file specified or open\\. Use the open_file function first\\.","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":1046,"sourceCode":"        res_list = [f'[Found {num_matches} matches for \"{search_term}\" in {dir_path}]']\n        for file_path, line_num, line in matches:\n            res_list.append(f\"{file_path} (Line {line_num}): {line}\")\n        res_list.append(f'[End of matches for \"{search_term}\" in {dir_path}]')\n        return \"\\n\".join(res_list)\n\n    def search_file(self, search_term: str, file_path: Optional[str] = None) -> str:\n        \"\"\"Searches for search_term in file. If file is not provided, searches in the current open file.\n\n        Args:\n            search_term: str: The term to search for.\n            file_path: str | None: The path to the file to search.\n        \"\"\"\n        if file_path is None:\n            file_path = self.current_file\n        else:\n            file_path = self._try_fix_path(file_path)\n        if file_path is None:\n            raise FileNotFoundError(\"No file specified or open. Use the open_file function first.\")\n        if not file_path.is_file():\n            raise FileNotFoundError(f\"File {file_path} not found\")\n\n        matches = []\n        with file_path.open() as file:\n            for i, line in enumerate(file, 1):\n                if search_term in line:\n                    matches.append((i, line.strip()))\n        res_list = []\n        if matches:\n            res_list.append(f'[Found {len(matches)} matches for \"{search_term}\" in {file_path}]')\n            for match in matches:\n                res_list.append(f\"Line {match[0]}: {match[1]}\")\n            res_list.append(f'[End of matches for \"{search_term}\" in {file_path}]')\n        else:\n            res_list.append(f'[No matches found for \"{search_term}\" in {file_path}]')\n\n        extra = {\"type\": \"search\", \"symbol\": search_term, \"lines\": [i[0] - 1 for i in matches]} if matches else None","sourceCodeStart":1028,"sourceCodeEnd":1064,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L1028-L1064","documentation":"Raised by Editor.search_file when no file_path argument is given and the editor has no currently open file (self.current_file is None). The editor tool is stateful: it remembers the last file opened via open_file, and search_file falls back to that file only if the caller omits file_path. With neither source available, there is nothing to search, so the call is rejected.","triggerScenarios":"Calling editor.search_file(\"some_term\") as the very first editor operation (no prior open_file), or after a state reset/new Editor instance, without passing the optional file_path argument.","commonSituations":"An LLM agent or script issues search_file before open_file; a new session reuses a stale plan written against a previous editor instance; concurrent uses where another consumer never opened a file.","solutions":["Call editor.open_file(path) first, then search_file(term) without a path","Or pass the path explicitly: editor.search_file(term, file_path='path/to/file.py')","If writing an automated flow, always seed state with open_file at the start of the session"],"exampleFix":"# before\neditor.search_file(\"def main\")\n# after\neditor.open_file(\"src/app.py\")\neditor.search_file(\"def main\")\n# or\neditor.search_file(\"def main\", file_path=\"src/app.py\")","handlingStrategy":"validation","validationCode":"if editor.current_file is None and file_path is None:\n    editor.open_file(\"src/app.py\")  # or raise your own guidance error","typeGuard":null,"tryCatchPattern":"try:\n    out = editor.search_file(term, file_path)\nexcept FileNotFoundError as e:\n    # message distinguishes 'no file open' vs 'File ... not found'\n    handle(e)","preventionTips":["Always open_file at the start of an editor session before any path-less calls","Pass file_path explicitly in scripted (non-interactive) flows to avoid hidden state dependence","Treat current_file as session-scoped: do not assume it survives across Editor instances"],"tags":["editor","file","stateful-api","api-misuse"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}