Aider-AI/aider · error · DiffError

File referenced in patch not found: {rel_path}

Error message

File referenced in patch not found: {rel_path}

What it means

The FileNotFoundError branch of the same preload loop in PatchCoder.get_edits: abs_root_path resolved the path, but io.read_text raised FileNotFoundError, so the patch targets a file that is definitively absent from disk.

Source

Thrown at aider/coders/patch_coder.py:270

            start_index = 0
        else:
            start_index = 1  # Skip "*** Begin Patch"

        # Identify files needed for context lookups during parsing
        needed_paths = identify_files_needed(content)
        current_files: Dict[str, str] = {}
        for rel_path in needed_paths:
            abs_path = self.abs_root_path(rel_path)
            try:
                # Use io.read_text to handle potential errors/encodings
                file_content = self.io.read_text(abs_path)
                if file_content is None:
                    raise DiffError(
                        f"File referenced in patch not found or could not be read: {rel_path}"
                    )
                current_files[rel_path] = file_content
            except FileNotFoundError:
                raise DiffError(f"File referenced in patch not found: {rel_path}")
            except IOError as e:
                raise DiffError(f"Error reading file {rel_path}: {e}")

        try:
            # Parse the patch text using adapted logic
            patch_obj = self._parse_patch_text(lines, start_index, current_files)
            # Convert Patch object actions dict to a list of tuples (path, action)
            # for compatibility with the base Coder's prepare_to_edit method.
            results = []
            for path, action in patch_obj.actions.items():
                results.append((path, action))
            return results
        except DiffError as e:
            # Raise as ValueError for consistency with other coders' error handling
            raise ValueError(f"Error parsing patch content: {e}")
        except Exception as e:
            # Catch unexpected errors during parsing
            raise ValueError(f"Unexpected error parsing patch: {e}")

View on GitHub (pinned to 5dc9490bb3)

Solutions

  1. Confirm the file exists: ls <path-from-patch> from the repo root.
  2. Restore/recreate the file (git checkout -- path) if it was deleted mid-session, then re-apply.
  3. Use '*** Add File:' for files that should be created rather than updated.
  4. Re-sync the model's view of the repo (/drop the stale file, /add the current one) and regenerate the patch.

Example fix

# before
*** Update File: deleted_module.py   (file was removed)

# after
*** Add File: deleted_module.py
def restored():
    ...
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path

def missing_update_paths(patch_text: str, root: Path) -> list[str]:
    out = []
    for l in patch_text.splitlines():
        s = l.strip()
        if s.startswith(("*** Update File: ", "*** Delete File: ")):
            rel = s.split(": ", 1)[1].strip()
            if not (root / rel).is_file():
                out.append(rel)
    return out

Try / catch

try:
    coder.get_edits(patch_text)
except ValueError as e:
    if "File referenced in patch not found:" in str(e):
        rel = str(e).split("File referenced in patch not found:")[-1].strip()
        patch_text = relabel_or_add_file(patch_text, rel)  # fix path or use Add
        coder.get_edits(patch_text)
    else:
        raise

Prevention

When it happens

Trigger: io.read_text raises FileNotFoundError (rather than returning None) for an '*** Update File:'/'*** Delete File:' path — e.g. a path in a directory that doesn't exist, or a file deleted between chat history generation and patch application.

Common situations: Continuing a session after the file was deleted/moved externally; patches referencing paths from another repo layout; hallucinated filenames whose parent directories don't exist.

Related errors


AI-assisted analysis of Aider-AI/aider@5dc9490bb3 (2026-08-15). Data as JSON: /api/errors/28421f2816b9fdde. Report an issue: GitHub.