FoundationAgents/MetaGPT · error · ValueError
No file open. Use the open_file function first.
Error message
No file open. Use the open_file function first.
What it means
Raised by Editor._check_current_file when a stateful editor operation (goto_line, scroll_down, scroll_up, read, edit, etc.) is called before any file has been opened with open_file or created with create_file. The editor tracks self.current_file, and this guard rejects the None/missing state. It is a usage-order error, not a filesystem error.
Source
Thrown at metagpt/tools/libs/editor.py:195
try:
return path.exists()
except PermissionError:
return False
@staticmethod
def _create_paths(file_path: Path) -> bool:
try:
if file_path.parent:
file_path.parent.mkdir(parents=True, exist_ok=True)
return True
except PermissionError:
return False
def _check_current_file(self, file_path: Optional[Path] = None) -> bool:
if file_path is None:
file_path = self.current_file
if not file_path or not file_path.is_file():
raise ValueError("No file open. Use the open_file function first.")
return True
@staticmethod
def _clamp(value, min_value, max_value):
return max(min_value, min(value, max_value))
def _lint_file(self, file_path: Path) -> tuple[Optional[str], Optional[int]]:
"""Lint the file at the given path and return a tuple with a boolean indicating if there are errors,
and the line number of the first error, if any.
Returns:
tuple[str | None, int | None]: (lint_error, first_error_line_number)
"""
linter = Linter(root=self.working_dir)
lint_error = linter.lint(str(file_path))
if not lint_error:
# Linting successful. No issues found.View on GitHub (pinned to 11cdf466d0)
Solutions
- Call editor.open_file(path) (or create_file for new files) before goto_line/scroll/read/edit operations
- If the file should exist but does not, check the path and recreate/relocate it, then open it again
- Persist and restore self.current_file across steps if you reuse the editor in a multi-turn agent loop
Example fix
// before
editor = Editor()
editor.goto_line(5) # ValueError: No file open
// after
editor = Editor()
editor.open_file('src/main.py')
editor.goto_line(5) Defensive patterns
Strategy: validation
Validate before calling
if editor.current_file is None or not editor.current_file.is_file():
editor.open_file(str(path))
editor.goto_line(n) Try / catch
try:
editor.goto_line(n)
except ValueError as e:
if 'No file open' in str(e):
editor.open_file(str(path)) # recover and retry once
else:
raise Prevention
- Always pair Editor() construction with an immediate open_file/create_file
- In multi-turn agent loops, restore current_file from persisted state before operating
- Wrap stateful editor calls in a helper that asserts current_file is set
When it happens
Trigger: Calling editor.goto_line(10), editor.scroll_down(), editor.read(...), or any method that starts with self._check_current_file() on a fresh Editor() instance, or after the previously opened file was deleted/moved so current_file.is_file() is False.
Common situations: Agent pipelines or scripts that instantiate a new Editor per step and forget the open_file call; resuming a session where the file was deleted externally; concurrent runs where another process removed the file between open and edit.
Related errors
- File {path} not found
- Line number must be between 1 and {total_lines}
- File '{filename}' already exists.
- Invalid line number: {start}. Line numbers must be between 1
- Invalid start line number: {start}. Line numbers must be bet
AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14).
Data as JSON: /api/errors/39ff0f953071a394.
Report an issue: GitHub.