Aider-AI/aider · error · DiffError

{path}: Mismatch applying patch near line {chunk_start_index

Error message

{path}: Mismatch applying patch near line {chunk_start_index + 1}.
Expected lines to remove:
{expected_str}
Found lines in file:
{actual_str}

What it means

Verification failure while applying a chunk: the lines the patch wants to delete at chunk_start_index do not match the file's actual lines, after whitespace-normalizing both. This is the apply-time double-check of the parser's context match — usually the file changed between parse and apply.

Source

Thrown at aider/coders/patch_coder.py:687

            # Add lines from original file between the last chunk and this one
            dest_lines.extend(orig_lines[current_orig_line_idx:chunk_start_index])

            # Verify that the lines to be deleted actually match the original file content
            # (The parser should have used find_context, but double-check here)
            num_del = len(chunk.del_lines)
            actual_deleted_lines = orig_lines[chunk_start_index : chunk_start_index + num_del]

            # Use the same normalization as find_context_core for comparison robustness
            norm_chunk_del = [_norm(s).strip() for s in chunk.del_lines]
            norm_actual_del = [_norm(s).strip() for s in actual_deleted_lines]

            if norm_chunk_del != norm_actual_del:
                # This indicates the context matching failed or the file changed since parsing
                # Provide detailed error message
                expected_str = "\n".join(f"- {s}" for s in chunk.del_lines)
                actual_str = "\n".join(f"  {s}" for s in actual_deleted_lines)
                raise DiffError(
                    f"{path}: Mismatch applying patch near line {chunk_start_index + 1}.\n"
                    f"Expected lines to remove:\n{expected_str}\n"
                    f"Found lines in file:\n{actual_str}"
                )

            # Add the inserted lines from the chunk
            dest_lines.extend(chunk.ins_lines)

            # Advance the original line index past the lines processed (deleted lines)
            current_orig_line_idx = chunk_start_index + num_del

        # Add any remaining lines from the original file after the last chunk
        dest_lines.extend(orig_lines[current_orig_line_idx:])

        # Join lines and ensure a single trailing newline
        result = "\n".join(dest_lines)
        if result or orig_lines:  # Add newline unless result is empty and original was empty
            result += "\n"

View on GitHub (pinned to 5dc9490bb3)

Solutions

  1. Re-read the file and regenerate the patch from its current content
  2. Disable/reconcile auto-formatting processes that touch the file during apply
  3. Diff the 'Expected lines to remove' vs 'Found lines in file' in the message to see exactly what drifted, then hand-fix the patch or file
Defensive patterns

Strategy: try-catch

Validate before calling

def chunk_de_lines_match(chunks, file_lines) -> list[int]:
    bad = []
    for c in sorted(chunks, key=lambda c: c.orig_index):
        actual = [s.strip() for s in file_lines[c.orig_index:c.orig_index + len(c.del_lines)]]
        if [s.strip() for s in c.del_lines] != actual:
            bad.append(c.orig_index)
    return bad  # empty == safe to apply

Try / catch

try:
    coder.apply_edits(edits)
except ValueError as e:
    if "Mismatch applying patch" in str(e):
        # re-read file and regenerate patch from fresh content
        content = read_file(action_path)
        edits = coder.get_edits(regenerate_patch_for(content))
        coder.apply_edits(edits)
    else:
        raise

Prevention

When it happens

Trigger: File on disk modified after parsing (format-on-save, linter, another process); chunk.del_lines that never matched due to parser fuzz tolerance; encoding differences making normalized comparison fail.

Common situations: IDE auto-format on save racing the apply; two aider sessions on the same file; applying a stale patch after a git pull.

Related errors


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