FoundationAgents/MetaGPT · error · FileNotFoundError

Invalid file name.

Error message

Invalid file name.

What it means

Raised at the top of Editor._edit_file_impl when _is_valid_filename(file_name.name) fails, i.e. the basename of the file being edited does not match the editor's allowed filename pattern. This is a pre-flight validation before any filesystem access; the message is a static string and does not echo the bad name.

Source

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

        Args:
            file_name: Path: The name of the file to edit or append to.
            start: int | None = None: The start line number for editing. Ignored if is_append is True.
            end: int | None = None: The end line number for editing. Ignored if is_append is True.
            content: str: The content to replace the lines with or to append.
            is_insert: bool = False: Whether to insert content at the given line number instead of editing.
            is_append: bool = False: Whether to append content to the file instead of editing.
        """

        ERROR_MSG = f"[Error editing file {file_name}. Please confirm the file is correct.]"
        ERROR_MSG_SUFFIX = (
            "Your changes have NOT been applied. Please fix your edit command and try again.\n"
            "You either need to 1) Open the correct file and try again or 2) Specify the correct line number arguments.\n"
            "DO NOT re-run the same failed edit command. Running it again will lead to the same error."
        )

        if not self._is_valid_filename(file_name.name):
            raise FileNotFoundError("Invalid file name.")

        if not self._is_valid_path(file_name):
            raise FileNotFoundError("Invalid path or file name.")

        if not self._create_paths(file_name):
            raise PermissionError("Could not access or create directories.")

        if not file_name.is_file():
            raise FileNotFoundError(f"File {file_name} not found.")

        if is_insert and is_append:
            raise ValueError("Cannot insert and append at the same time.")

        # Use a temporary file to write changes
        content = str(content or "")
        temp_file_path = ""
        src_abs_path = file_name.resolve()
        first_error_line = None

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Inspect Editor._is_valid_filename and normalize the basename to a plain, sane filename (letters, digits, dot, underscore, hyphen)
  2. Strip trailing slashes, quotes, and glob characters from the path before calling
  3. Rename the target file if its name genuinely violates the pattern

Example fix

// before
editor._edit_file_impl(Path("src/'weird name?.py"))  # FileNotFoundError: Invalid file name.

// after
editor._edit_file_impl(Path('src/weird_name.py'))
Defensive patterns

Strategy: validation

Validate before calling

import re
name = Path(file_name).name
if not re.fullmatch(r'[A-Za-z0-9._\- ]+', name) or name in ('', '.', '..'):
    raise ValueError(f'unusable filename: {name!r}')
# then call the editor

Type guard

def is_safe_filename(name: str) -> bool:
    return bool(name) and name not in ('.', '..') and '/' not in name and '\x00' not in name

Try / catch

try:
    editor._edit_file_impl(Path(p), ...)
except FileNotFoundError as e:
    if str(e) == 'Invalid file name.':
        p = sanitize_basename(p)  # normalize, then retry
    raise

Prevention

When it happens

Trigger: Editing a file whose basename contains disallowed characters, is empty, or otherwise fails the validity regex (e.g. names with wildcards, control chars, or hidden/dot-only names depending on the pattern); passing a directory-like path so .name is empty.

Common situations: LLM agents passing shell-quoted or glob-style names; paths ending in '/' causing an empty basename; unusual filenames with spaces or special characters rejected by the pattern.

Related errors


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