continuedev/continue · error · Error

Hunk could not be applied cleanly to source code.

Error message

Hunk could not be applied cleanly to source code.

What it means

applyUnifiedDiff walks the source locating each hunk via its leading context lines; if findHunkStart returns -1 the hunk's context doesn't exist in the file at the expected position, so the diff cannot be applied and it throws.

Source

Thrown at core/edit/lazy/unifiedDiffApply.ts:61

export function applyUnifiedDiff(
  sourceCode: string,
  unifiedDiffText: string,
): DiffLine[] {
  const sourceLines = sourceCode.split(/\r?\n/);
  const hunks = parseUnifiedDiff(unifiedDiffText);
  const diffResult: DiffLine[] = [];
  let currentPos = 0; // pointer in sourceLines

  for (const hunk of hunks) {
    const hunkBeforeLines = extractBeforeLines(hunk.lines);
    const hunkStart = findHunkInSource(
      sourceLines,
      hunkBeforeLines,
      currentPos,
    );
    if (hunkStart === -1) {
      // All hunks must be found in the source code. If not, throw an error.
      throw new Error("Hunk could not be applied cleanly to source code.");
    }

    // Emit any unchanged lines that come before this hunk.
    for (let i = currentPos; i < hunkStart; i++) {
      diffResult.push({ type: "same", line: sourceLines[i] });
    }
    let hunkSourcePos = hunkStart;

    for (const dline of hunk.lines) {
      const srcLine = sourceLines[hunkSourcePos];
      if (dline.startsWith("+")) {
        // Insertion: output new line (strip the '+' marker)
        diffResult.push({ type: "new", line: dline.substring(1) });
      } else if (dline.startsWith("-")) {
        // Removal: output the removed (old) line and advance the pointer.
        diffResult.push({ type: "old", line: srcLine });
        hunkSourcePos++;
      } else {

View on GitHub (pinned to 5522c6f44c)

Solutions

  1. Re-apply against the current unmodified file (revert local changes first)
  2. Regenerate the diff/LLM output and retry
  3. Normalize line endings/whitespace before applying
  4. Fall back to a full-file rewrite instead of hunk application
Defensive patterns

Strategy: fallback

Validate before calling

const hunksOk = unifiedDiff.every(h => findHunkStart(sourceLines, h.before, 0) !== -1);
if (!hunksOk) { requestFullFileRewrite(); }

Try / catch

catch (e) { if (e.message.includes('could not be applied cleanly')) { re-generate the diff against current file content and retry once; } else throw e; }

Prevention

When it happens

Trigger: Applying a unified diff whose context lines don't match the file — file changed since diff generation, LLM-produced malformed offsets, or prior hunks shifting lines so later context no longer aligns.

Common situations: Stale diffs applied after edits; LLM-generated diffs with incorrect line numbers/@@ headers; whitespace or line-ending differences between diff and file.

Related errors


AI-assisted analysis of continuedev/continue@5522c6f44c (2026-08-27). Data as JSON: /api/errors/82d1d4a15e8e712a. Report an issue: GitHub.