Aider-AI/aider · error · DiffError

File referenced in patch not found or could not be read: {re

Error message

File referenced in patch not found or could not be read: {rel_path}

What it means

Raised by PatchCoder.get_edits while preloading context: for a path listed under '*** Update File:'/'*** Delete File:', self.io.read_text(abs_path) returned None (aider's IO returns None for unreadable/missing files instead of raising). The patch references a file that does not exist on disk or cannot be read.

Source

Thrown at aider/coders/patch_coder.py:265

                return []
            # If it looks like a patch but lacks sentinels, try parsing anyway but warn.
            self.io.tool_warning(
                "Patch format warning: Missing '*** Begin Patch'/'*** End Patch' sentinels."
            )
            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:

View on GitHub (pinned to 5dc9490bb3)

Solutions

  1. Verify the path exists relative to the coder's root: ls the exact string from '*** Update File:' (watch case and leading slashes).
  2. If the file is new, use '*** Add File:' instead of '*** Update File:'.
  3. Check permissions/encoding if the file exists — io.read_text returns None for files aider refuses to read (e.g. non-UTF-8 without the right setting).
  4. Re-run against the correct branch/commit where the file exists, or /add the real file so the model sees its true path.

Example fix

# before
*** Update File: src/utils/Helpers.py   (actual file: src/utils/helpers.py)

# after
*** Update File: src/utils/helpers.py
Defensive patterns

Strategy: validation

Validate before calling

def verify_patch_paths_readable(patch_text: str, root: str):
    import os
    bad = []
    for l in patch_text.splitlines():
        s = l.strip()
        if s.startswith(("*** Update File: ", "*** Delete File: ")):
            rel = s.split(": ", 1)[1].strip()
            p = os.path.join(root, rel)
            if not os.path.isfile(p) or not os.access(p, os.R_OK):
                bad.append(rel)
    return bad  # non-empty → fix paths (or switch to *** Add File:) before get_edits()

Try / catch

try:
    coder.get_edits(patch_text)
except ValueError as e:
    if "not found or could not be read" in str(e):
        rel = str(e).rsplit(": ", 1)[-1]
        if not (root / rel).exists():
            patch_text = convert_update_to_add(patch_text, rel)
        coder.get_edits(patch_text)
    else:
        raise

Prevention

When it happens

Trigger: A patch updating/deleting a file whose path doesn't exist under the repo root, has wrong case, points outside the working tree, or has an encoding/permission problem that makes io.read_text return None.

Common situations: Model hallucinating a plausible-but-wrong filename; patches generated against a different commit where the file existed; path case mismatches on case-insensitive filesystems ported to Linux; paths with a wrong leading './' or absolute path; unreadable binary/encoding-mismatched files.

Related errors


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