FoundationAgents/MetaGPT · error · FileNotFoundError

No file specified or open. Use the open_file function first.

Error message

No file specified or open. Use the open_file function first.

What it means

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.

Source

Thrown at metagpt/tools/libs/editor.py:1046

        res_list = [f'[Found {num_matches} matches for "{search_term}" in {dir_path}]']
        for file_path, line_num, line in matches:
            res_list.append(f"{file_path} (Line {line_num}): {line}")
        res_list.append(f'[End of matches for "{search_term}" in {dir_path}]')
        return "\n".join(res_list)

    def search_file(self, search_term: str, file_path: Optional[str] = None) -> str:
        """Searches for search_term in file. If file is not provided, searches in the current open file.

        Args:
            search_term: str: The term to search for.
            file_path: str | None: The path to the file to search.
        """
        if file_path is None:
            file_path = self.current_file
        else:
            file_path = self._try_fix_path(file_path)
        if file_path is None:
            raise FileNotFoundError("No file specified or open. Use the open_file function first.")
        if not file_path.is_file():
            raise FileNotFoundError(f"File {file_path} not found")

        matches = []
        with file_path.open() as file:
            for i, line in enumerate(file, 1):
                if search_term in line:
                    matches.append((i, line.strip()))
        res_list = []
        if matches:
            res_list.append(f'[Found {len(matches)} matches for "{search_term}" in {file_path}]')
            for match in matches:
                res_list.append(f"Line {match[0]}: {match[1]}")
            res_list.append(f'[End of matches for "{search_term}" in {file_path}]')
        else:
            res_list.append(f'[No matches found for "{search_term}" in {file_path}]')

        extra = {"type": "search", "symbol": search_term, "lines": [i[0] - 1 for i in matches]} if matches else None

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Call editor.open_file(path) first, then search_file(term) without a path
  2. Or pass the path explicitly: editor.search_file(term, file_path='path/to/file.py')
  3. If writing an automated flow, always seed state with open_file at the start of the session

Example fix

# before
editor.search_file("def main")
# after
editor.open_file("src/app.py")
editor.search_file("def main")
# or
editor.search_file("def main", file_path="src/app.py")
Defensive patterns

Strategy: validation

Validate before calling

if editor.current_file is None and file_path is None:
    editor.open_file("src/app.py")  # or raise your own guidance error

Try / catch

try:
    out = editor.search_file(term, file_path)
except FileNotFoundError as e:
    # message distinguishes 'no file open' vs 'File ... not found'
    handle(e)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/9efa35b373c9db7d. Report an issue: GitHub.