FoundationAgents/MetaGPT · error · FileNotFoundError

Directory {dir_path} not found

Error message

Directory {dir_path} not found

What it means

Raised by Editor.search_dir after path normalization when dir_path does not exist or is not a directory. The tool recursively walks the directory (skipping dotfiles) to grep for search_term, so a valid directory is a hard prerequisite.

Source

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

            start=None,
            end=None,
            content=content,
            is_insert=False,
            is_append=True,
        )
        self.resource.report(file_name, "path")
        return ret_str

    def search_dir(self, search_term: str, dir_path: str = "./") -> str:
        """Searches for search_term in all files in dir. If dir is not provided, searches in the current directory.

        Args:
            search_term: str: The term to search for.
            dir_path: str: The path to the directory to search.
        """
        dir_path = self._try_fix_path(dir_path)
        if not dir_path.is_dir():
            raise FileNotFoundError(f"Directory {dir_path} not found")
        matches = []
        for root, _, files in os.walk(dir_path):
            for file in files:
                if file.startswith("."):
                    continue
                file_path = Path(root) / file
                with file_path.open("r", errors="ignore") as f:
                    for line_num, line in enumerate(f, 1):
                        if search_term in line:
                            matches.append((file_path, line_num, line.strip()))

        if not matches:
            return f'No matches found for "{search_term}" in {dir_path}'

        num_matches = len(matches)
        num_files = len(set(match[0] for match in matches))

        if num_files > 100:

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Check Path(dir_path).is_dir() before calling, or list the parent to find the right directory name
  2. Pass an absolute directory path to avoid CWD-dependent resolution
  3. Default to './' when unsure, then narrow based on the matches

Example fix

// before
editor.search_dir('parse_args', './source')  # FileNotFoundError

// after
d = Path('./source')
root = d if d.is_dir() else Path('.')
editor.search_dir('parse_args', str(root))
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
d = Path(dir_path)
if not d.is_dir():
    d = Path('.')
editor.search_dir(search_term, str(d))

Type guard

def is_searchable_dir(p: str) -> bool:
    return Path(p).is_dir()

Try / catch

try:
    editor.search_dir(term, dir_path)
except FileNotFoundError:
    editor.search_dir(term, './')  # fall back to CWD-wide search

Prevention

When it happens

Trigger: editor.search_dir('foo', './sr c') typo paths; searching a directory that was never created or was deleted; passing a file path instead of a directory; relative path resolved against an unexpected CWD.

Common situations: Agents guessing at repo layout (searching './src' when the code is at './lib'); running from a different working directory than assumed; renaming or moving directories between steps.

Related errors


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