{"record":{"id":"1b6374a6383f13a5","repo":"FoundationAgents/MetaGPT","slug":"invalid-path-or-file-name","errorCode":null,"errorMessage":"Invalid path or file name.","messagePattern":"Invalid path or file name\\.","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":540,"sourceCode":"            start: int | None = None: The start line number for editing. Ignored if is_append is True.\n            end: int | None = None: The end line number for editing. Ignored if is_append is True.\n            content: str: The content to replace the lines with or to append.\n            is_insert: bool = False: Whether to insert content at the given line number instead of editing.\n            is_append: bool = False: Whether to append content to the file instead of editing.\n        \"\"\"\n\n        ERROR_MSG = f\"[Error editing file {file_name}. Please confirm the file is correct.]\"\n        ERROR_MSG_SUFFIX = (\n            \"Your changes have NOT been applied. Please fix your edit command and try again.\\n\"\n            \"You either need to 1) Open the correct file and try again or 2) Specify the correct line number arguments.\\n\"\n            \"DO NOT re-run the same failed edit command. Running it again will lead to the same error.\"\n        )\n\n        if not self._is_valid_filename(file_name.name):\n            raise FileNotFoundError(\"Invalid file name.\")\n\n        if not self._is_valid_path(file_name):\n            raise FileNotFoundError(\"Invalid path or file name.\")\n\n        if not self._create_paths(file_name):\n            raise PermissionError(\"Could not access or create directories.\")\n\n        if not file_name.is_file():\n            raise FileNotFoundError(f\"File {file_name} not found.\")\n\n        if is_insert and is_append:\n            raise ValueError(\"Cannot insert and append at the same time.\")\n\n        # Use a temporary file to write changes\n        content = str(content or \"\")\n        temp_file_path = \"\"\n        src_abs_path = file_name.resolve()\n        first_error_line = None\n        # The file to store previous content and will be removed automatically.\n        temp_backup_file = tempfile.NamedTemporaryFile(\"w\", delete=True)\n","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L522-L558","documentation":"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).","triggerScenarios":"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.","commonSituations":"Agent tool calls emitting relative paths with unusual prefixes; paths assembled from untrusted text; platform-specific separators mangled by string concatenation.","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"],"exampleFix":"// before\neditor._edit_file_impl(Path(''))  # FileNotFoundError: Invalid path or file name.\n\n// after\neditor._edit_file_impl(Path('/workspace/project/src/main.py'))","handlingStrategy":"validation","validationCode":"from pathlib import Path\np = Path(file_name)\nif not p.is_absolute():\n    p = (workspace_root / p).resolve()\nassert str(p).startswith(str(workspace_root))  # stay inside workspace\n# then call the editor with p","typeGuard":"def is_valid_workspace_path(p: Path, root: Path) -> bool:\n    try:\n        p.resolve().relative_to(root.resolve())\n        return True\n    except ValueError:\n        return False","tryCatchPattern":"try:\n    editor._edit_file_impl(p, ...)\nexcept FileNotFoundError as e:\n    if str(e) == 'Invalid path or file name.':\n        p = (workspace_root / p).resolve()\n        editor._edit_file_impl(p, ...)\n    else:\n        raise","preventionTips":["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"],"tags":["editor","path","validation","metagpt"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}