datawhalechina/hello-agents · error · PatchApplyError

Update File target missing: {rel_path}

Error message

Update File target missing: {rel_path}

What it means

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.

Source

Thrown at Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/executors/apply_patch_executor.py:165

                # 记录变更
                files_changed.append(rel_path)
                
            elif kind == "delete":
                # 删除文件操作
                if not target.exists():
                    raise PatchApplyError(f"Delete File target missing: {rel_path}")
                # 删除前先备份文件
                b = self._backup_file(target, backup_run_dir)
                backups.append(str(b))
                # 删除文件
                target.unlink()
                # 记录变更
                files_changed.append(rel_path)
                
            elif kind == "update":
                # 更新文件操作
                if not target.exists():
                    raise PatchApplyError(f"Update File target missing: {rel_path}")
                # 读取原始文件内容(保留换行符)
                original = target.read_text(encoding="utf-8").splitlines(keepends=True)
                # 修改前先备份文件
                b = self._backup_file(target, backup_run_dir)
                backups.append(str(b))
                # 应用更新补丁内容
                updated = self._apply_update_payload(original, payload, rel_path)
                # 原子写入更新后的内容
                self._atomic_write(target, "".join(updated))
                # 记录变更
                files_changed.append(rel_path)
                
            else:
                # 未知操作类型
                raise PatchApplyError(f"Unknown op kind: {kind}")

        # 返回最终的应用结果
        return ApplyResult(files_changed=files_changed, backups=backups)

View on GitHub (pinned to 606a07d341)

Solutions

  1. Verify the target path exists relative to the executor's root before applying; fix the path in the patch.
  2. If the file genuinely should be new, use Add File instead.
  3. Re-sync the workspace (git checkout/clean) and regenerate the patch from the current state.
  4. Check _safe_path behavior if you suspect path normalization (leading './', backslashes).

Example fix

# before
patch updates 'src/util/helpers.py' which doesn't exist -> PatchApplyError

# after
# correct the path to the real location
patch updates 'src/helpers.py'
Defensive patterns

Strategy: type-guard

Validate before calling

from pathlib import Path
for op in update_ops:
    if not Path(repo_root, op.path).exists():
        raise FileNotFoundError(f"update target missing: {op.path}; regenerate patch")

Type guard

from pathlib import Path

def classify_op(rel_path: str, intend_create: bool) -> str:
    exists = Path(rel_path).is_file()
    if intend_create and exists:
        return "Update File"  # avoid Add conflict
    if not intend_create and not exists:
        return "Add File"      # avoid Update-missing
    return "Update File" if exists else "Add File"

Try / catch

try:
    executor.apply(patch_text)
except PatchApplyError as e:
    if "Update File target missing" in str(e):
        # patch built from stale tree: re-sync workspace and regenerate
        ...

Prevention

When it happens

Trigger: 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.

Common situations: Patch generated from a stale repo snapshot; agent path hallucination; Windows/Unix path separator or case mismatches; applying a patch meant for another worktree.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/52a3aa600b2959a2. Report an issue: GitHub.