FoundationAgents/MetaGPT · error · FileNotFoundError
Invalid path or file name.
Error message
Invalid path or file name.
What it means
Raised by Editor._edit_file_impl when _is_valid_path(file_name) fails, i.e. the full path is rejected by the editor's path policy even though the basename was fine. It guards against paths the editor refuses to touch (pattern-dependent: typically empty paths, disallowed shapes, or paths outside expectations).
Source
Thrown at metagpt/tools/libs/editor.py:540
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
# The file to store previous content and will be removed automatically.
temp_backup_file = tempfile.NamedTemporaryFile("w", delete=True)
View on GitHub (pinned to 11cdf466d0)
Solutions
- Pass a clean absolute Path built with pathlib (Path(base) / relative)
- Read Editor._is_valid_path to learn the exact accepted shape and conform to it
- Sanitize user/LLM-supplied paths before they reach the editor
Example fix
// before
editor._edit_file_impl(Path('')) # FileNotFoundError: Invalid path or file name.
// after
editor._edit_file_impl(Path('/workspace/project/src/main.py')) Defensive patterns
Strategy: validation
Validate before calling
from pathlib import Path
p = Path(file_name)
if not p.is_absolute():
p = (workspace_root / p).resolve()
assert str(p).startswith(str(workspace_root)) # stay inside workspace
# then call the editor with p Type guard
def is_valid_workspace_path(p: Path, root: Path) -> bool:
try:
p.resolve().relative_to(root.resolve())
return True
except ValueError:
return False Try / catch
try:
editor._edit_file_impl(p, ...)
except FileNotFoundError as e:
if str(e) == 'Invalid path or file name.':
p = (workspace_root / p).resolve()
editor._edit_file_impl(p, ...)
else:
raise Prevention
- Always resolve paths against an explicit workspace root
- Reject empty path strings at input validation time
- Read Editor._is_valid_path once and mirror its rules in your wrapper
When it happens
Trigger: Editing with an empty path string, a path with redundant or disallowed components, or a path shape that fails the editor's regex after _try_fix_path normalization.
Common situations: Agent tool calls emitting relative paths with unusual prefixes; paths assembled from untrusted text; platform-specific separators mangled by string concatenation.
Related errors
- File {path} not found
- Line number must be between 1 and {total_lines}
- Invalid start line number: {start}. Line numbers must be bet
- Invalid file name.
- Cannot insert and append at the same time.
AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14).
Data as JSON: /api/errors/1b6374a6383f13a5.
Report an issue: GitHub.