{"record":{"id":"52a3aa600b2959a2","repo":"datawhalechina/hello-agents","slug":"update-file-target-missing-rel-path","errorCode":null,"errorMessage":"Update File target missing: {rel_path}","messagePattern":"Update File target missing: (.+?)","errorType":"exception","errorClass":"PatchApplyError","httpStatus":null,"severity":"error","filePath":"Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/executors/apply_patch_executor.py","lineNumber":165,"sourceCode":"                # 记录变更\n                files_changed.append(rel_path)\n                \n            elif kind == \"delete\":\n                # 删除文件操作\n                if not target.exists():\n                    raise PatchApplyError(f\"Delete File target missing: {rel_path}\")\n                # 删除前先备份文件\n                b = self._backup_file(target, backup_run_dir)\n                backups.append(str(b))\n                # 删除文件\n                target.unlink()\n                # 记录变更\n                files_changed.append(rel_path)\n                \n            elif kind == \"update\":\n                # 更新文件操作\n                if not target.exists():\n                    raise PatchApplyError(f\"Update File target missing: {rel_path}\")\n                # 读取原始文件内容（保留换行符）\n                original = target.read_text(encoding=\"utf-8\").splitlines(keepends=True)\n                # 修改前先备份文件\n                b = self._backup_file(target, backup_run_dir)\n                backups.append(str(b))\n                # 应用更新补丁内容\n                updated = self._apply_update_payload(original, payload, rel_path)\n                # 原子写入更新后的内容\n                self._atomic_write(target, \"\".join(updated))\n                # 记录变更\n                files_changed.append(rel_path)\n                \n            else:\n                # 未知操作类型\n                raise PatchApplyError(f\"Unknown op kind: {kind}\")\n\n        # 返回最终的应用结果\n        return ApplyResult(files_changed=files_changed, backups=backups)","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/executors/apply_patch_executor.py#L147-L183","documentation":"PatchApplyError raised when an '*** Update File' operation targets a file that does not exist on disk. Update requires reading the original content (splitlines with keepends) to apply the payload, so a missing target cannot be updated. As with delete, earlier operations in the same patch may already have been applied when this aborts.","triggerScenarios":"Update File op for a typo'd/wrong-case path, a file not yet created, a double-apply after the file was deleted, or a patch built against a different branch/commit.","commonSituations":"Patch generated from a stale repo snapshot; agent path hallucination; Windows/Unix path separator or case mismatches; applying a patch meant for another worktree.","solutions":["Verify the target path exists relative to the executor's root before applying; fix the path in the patch.","If the file genuinely should be new, use Add File instead.","Re-sync the workspace (git checkout/clean) and regenerate the patch from the current state.","Check _safe_path behavior if you suspect path normalization (leading './', backslashes)."],"exampleFix":"# before\npatch updates 'src/util/helpers.py' which doesn't exist -> PatchApplyError\n\n# after\n# correct the path to the real location\npatch updates 'src/helpers.py'","handlingStrategy":"type-guard","validationCode":"from pathlib import Path\nfor op in update_ops:\n    if not Path(repo_root, op.path).exists():\n        raise FileNotFoundError(f\"update target missing: {op.path}; regenerate patch\")","typeGuard":"from pathlib import Path\n\ndef classify_op(rel_path: str, intend_create: bool) -> str:\n    exists = Path(rel_path).is_file()\n    if intend_create and exists:\n        return \"Update File\"  # avoid Add conflict\n    if not intend_create and not exists:\n        return \"Add File\"      # avoid Update-missing\n    return \"Update File\" if exists else \"Add File\"","tryCatchPattern":"try:\n    executor.apply(patch_text)\nexcept PatchApplyError as e:\n    if \"Update File target missing\" in str(e):\n        # patch built from stale tree: re-sync workspace and regenerate\n        ...","preventionTips":["Generate patches from a freshly read tree state, never a cached listing.","Verify update targets exist relative to the executor's root (mind _safe_path normalization).","Sync/checkout the expected branch before applying patches produced elsewhere."],"tags":["python","patching","conflict","code-agent"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}