FoundationAgents/MetaGPT · error · PermissionError
Could not access or create directories.
Error message
Could not access or create directories.
What it means
Raised by Editor._edit_file_impl when _create_paths returns False, which happens when mkdir(parents=True, exist_ok=True) on the file's parent directory raises PermissionError. The editor tries to ensure the parent directory exists before editing and surfaces an OS permission failure as this error.
Source
Thrown at metagpt/tools/libs/editor.py:543
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)
try:
# lint the original file
# enable_auto_lint = os.getenv("ENABLE_AUTO_LINT", "false").lower() == "true"View on GitHub (pinned to 11cdf466d0)
Solutions
- Check os.access(file_name.parent, os.W_OK) and fix ownership/permissions (chmod/chown) or move the workspace
- Point the editor at a writable workspace root instead of system directories
- In containers, ensure the mounted volume is writable by the running UID
Example fix
# before: editing under root-owned dir
# editor.edit('/opt/app/config.py', ...) -> PermissionError
# after
# chmod/chown the dir, or relocate:
editor.edit('/workspace/app/config.py', ...) Defensive patterns
Strategy: validation
Validate before calling
import os
parent = Path(file_name).parent
if not os.access(parent, os.W_OK):
raise PermissionError(f'no write access to {parent}')
# then call the editor Type guard
def dir_writable(p: Path) -> bool:
return p.is_dir() and os.access(p, os.W_OK) Try / catch
try:
editor._edit_file_impl(p, ...)
except PermissionError as e:
if 'Could not access or create directories' in str(e):
log.error('workspace not writable: %s', p.parent)
raise Prevention
- Run the agent with write access to its workspace root
- In Docker, match the container UID with the volume owner
- Never point the editor at system directories; keep edits inside the mounted workspace
When it happens
Trigger: Editing a file under a directory the process cannot write (root-owned dirs, read-only mounts, another user's home); running the agent as an unprivileged user against /etc or system paths; containers with a read-only volume mounted at the target path.
Common situations: Wrong workspace root (editing absolute paths outside the mounted workspace); Docker/CI permission mismatches; directory owned by a different UID after a copy or volume mount.
Related errors
- File {path} not found
- File '{filename}' already exists.
- File {file_name} not found.
- 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/7a3537ecbbb25ceb.
Report an issue: GitHub.