FoundationAgents/MetaGPT · error · FileNotFoundError

File {file_path} not found

Error message

File {file_path} not found

What it means

Raised by Editor.search_file when the resolved path exists in the string sense but is not a regular file (or the resolved value points nowhere). Before this check, file_path goes through _try_fix_path, which normalizes/repairs relative paths (e.g. adding a leading '/' or resolving against the workspace). If the fixed path still does not satisfy Path.is_file(), the search aborts.

Source

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

            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
        self.resource.report(file_path, "path", extra=extra)
        return "\n".join(res_list)

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Verify the path with Path(file_path).is_file() before calling
  2. Use editor.find_file(name) to locate the real path when unsure
  3. Pass an absolute path to avoid _try_fix_path guessing on relative input

Example fix

# before
editor.search_file("foo", file_path="src/utils.py")  # file actually at src/common/utils.py
# after
loc = editor.find_file("utils.py")
# parse a hit from loc, then:
editor.search_file("foo", file_path="/abs/repo/src/common/utils.py")
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
p = Path(file_path) if file_path else editor.current_file
if p is None or not p.is_file():
    raise SystemExit(f"no such file: {p}")

Type guard

def is_searchable_file(editor, file_path: str | None) -> bool:
    p = Path(file_path) if file_path else editor.current_file
    return p is not None and p.is_file()

Try / catch

try:
    out = editor.search_file(term, str(p))
except FileNotFoundError:
    p = editor.find_file(p.name).splitlines()[1] if p else None  # recover via find_file

Prevention

When it happens

Trigger: Passing file_path that does not exist on disk; passing a directory path instead of a file; passing a relative path that _try_fix_path resolves against the wrong root; passing a path with a typo or wrong extension.

Common situations: Agent hallucinates a plausible-looking path that was never created; file was renamed/deleted between planning and execution; cwd changed so a relative path no longer resolves; path points to a symlink whose target is gone.

Related errors


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