Hmbown/CodeWhale · error · anyhow::Error

patch context not found in

Error message

patch context not found in {}: {}

What it means

When applying a context line (` ` prefix) in a patch hunk, apply_patch scans the remaining file lines for an exact match; if the context line is not found from the current cursor onward, it fails reporting the file and the unmatched line. The patch cannot be located within the current file contents.

Solutions

  1. Regenerate the patch against the file's current contents.
  2. Verify exact whitespace/indentation of the context line; it must match byte-for-byte.
  3. Check the file hasn't been modified by an earlier step in the same eval run; reorder or refresh the patch.
  4. Normalize line endings (CRLF vs LF) in both the file and the patch.

Example fix

// before: context line doesn't match file
    fn main() {
// after: match the actual file text exactly (e.g. 4-space indent)
    fn main() {
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check: every ' ' context line must appear in the current file
let file = std::fs::read_to_string(&target)?;
for line in patch.lines().filter(|l| l.starts_with(' ')) {
    let ctx = &line[1..];
    if !file.contains(ctx) {
        eprintln!("context line not in current file: {ctx:?}");
    }
}

Try / catch

match apply_patch(&root, patch) {
    Err(e) if e.to_string().starts_with("patch context not found") => {
        eprintln!("file drifted from patch assumptions; regenerate the patch against current contents");
    }
    other => other?,
}

Prevention

When it happens

Trigger: A ` ` context line (or `-` removal line's context) does not appear verbatim in the target file from the current position — the file content differs from what the patch assumed.

Common situations: File was already edited (by an earlier patch or the model) so context drifted; whitespace/indentation mismatch; patch generated against a different file version; trailing whitespace or CRLF differences.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/99fadb8bbcc7c3e0. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/eval.rs:720

        }
        if raw_line.starts_with("*** ") {
            return Err(anyhow!("unexpected patch directive: {raw_line}"));
        }
        if raw_line.starts_with("@@") {
            continue;
        }

        let (kind, rest) = raw_line.split_at(1);
        let content = rest.to_string();

        match kind {
            " " => {
                let Some(found) = file_lines[cursor..]
                    .iter()
                    .position(|line| line == &content)
                    .map(|offset| cursor + offset)
                else {
                    return Err(anyhow!(
                        "patch context not found in {}: {}",
                        file_path.display(),
                        content
                    ));
                };
                cursor = found + 1;
            }
            "-" => {
                if cursor >= file_lines.len() || file_lines[cursor] != content {
                    return Err(anyhow!(
                        "patch removal mismatch in {}: expected '{}'",
                        file_path.display(),
                        content
                    ));
                }
                file_lines.remove(cursor);
            }
            "+" => {

View on GitHub (pinned to 73e0f67d83)