FoundationAgents/MetaGPT · error · FileNotFoundError
File {file_name} not found.
Error message
File {file_name} not found. What it means
Raised by Editor._edit_file_impl after name/path/permission checks pass but the target path is still not an existing regular file. Edit is strictly for existing files: it reads, transforms, and atomically rewrites content, so a missing file is an error rather than an implicit create.
Source
Thrown at metagpt/tools/libs/editor.py:546
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)
try:
# lint the original file
# enable_auto_lint = os.getenv("ENABLE_AUTO_LINT", "false").lower() == "true"
if self.enable_auto_lint:
original_lint_error, _ = self._lint_file(file_name)
View on GitHub (pinned to 11cdf466d0)
Solutions
- Use editor.create_file(path) first for new files, then edit
- Verify with Path(p).is_file() right before editing
- If the file was expected to exist, find where it went (renamed/deleted) and open the correct path
Example fix
// before
editor.edit('src/new_module.py', start=1, end=1, content='x = 1\n') # FileNotFoundError
// after
if not Path('src/new_module.py').is_file():
editor.create_file('src/new_module.py')
editor.edit('src/new_module.py', start=1, end=1, content='x = 1\n') Defensive patterns
Strategy: validation
Validate before calling
p = Path(file_name)
if not p.is_file():
editor.create_file(str(p))
editor.edit(str(p), start=s, end=e, content=c) Type guard
def editable_file(p: str) -> bool:
return Path(p).is_file() Try / catch
try:
editor.edit(path, ...)
except FileNotFoundError as e:
if 'not found' in str(e):
editor.create_file(path)
editor.edit(path, ...)
else:
raise Prevention
- create_file first, edit second — the edit API never creates
- Recheck existence right before editing if other processes touch the tree
- Watch for case-sensitivity errors in filenames on Linux
When it happens
Trigger: editor.edit('new_file.py', ...) where new_file.py was never created; the file was deleted between open_file and edit; a race where another process removed it; wrong extension/case in the path.
Common situations: Agents trying to 'edit' a file they only planned to write; stale references to deleted files; case-sensitivity mistakes on Linux.
Related errors
- File {path} not found
- File '{filename}' already exists.
- Could not access or create directories.
- Directory {dir_path} not found
- No file open. Use the open_file function first.
AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14).
Data as JSON: /api/errors/be31a45b7b8c2047.
Report an issue: GitHub.