zed-industries/zed · error

couldn't resolve hunk

Error message

couldn't resolve hunk

What it means

A parsed udiff hunk could not be matched to any location in the target text: neither context-line search nor line-number disambiguation produced a usable offset in apply_diff_to_string_with_hunk_offset. Usually means the diff context no longer matches the buffer (stale text or earlier hunks shifted lines).

Solutions

  1. Regenerate the diff against the current buffer contents and reapply
  2. Fall back to fuzzy/whitespace-relaxed context matching before giving up
  3. Track cumulative line_delta across hunks so later hunks resolve against already-patched text
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/zeta_prompt/src/udiff.rs:413 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zed-industries/zed@f4178619ac (2026-08-20). Data as JSON: /api/errors/a95c778230fb557f. Report an issue: GitHub.

Appendix: source

Thrown at crates/zeta_prompt/src/udiff.rs:413

    let mut line_delta = 0i64;

    while let Some(event) = diff.next().context("Failed to parse diff")? {
        match event {
            DiffEvent::Hunk {
                mut hunk,
                path: _,
                status: _,
            } => {
                let candidates = find_context_candidates(&text, &mut hunk);
                let adjusted_start_line = hunk
                    .start_line
                    .and_then(|start_line| u32::try_from(start_line as i64 + line_delta).ok());

                let hunk_offset =
                    disambiguate_by_line_number(&candidates, adjusted_start_line, &|offset| {
                        text[..offset].matches('\n').count() as u32
                    })
                    .ok_or_else(|| anyhow!("couldn't resolve hunk"))?;

                if first_hunk_offset.is_none() {
                    first_hunk_offset = Some(hunk_offset);
                }

                let mut hunk_line_delta = 0i64;
                for edit in hunk.edits.iter().rev() {
                    let range = (hunk_offset + edit.range.start)..(hunk_offset + edit.range.end);
                    let deleted_lines = text[range.clone()].matches('\n').count() as i64;
                    let inserted_lines = edit.text.matches('\n').count() as i64;
                    text.replace_range(range, &edit.text);
                    hunk_line_delta += inserted_lines - deleted_lines;
                }
                line_delta += hunk_line_delta;
            }
            DiffEvent::FileEnd { .. } => {
                line_delta = 0;
            }

View on GitHub (pinned to f4178619ac)