{"record":{"id":"82d1d4a15e8e712a","repo":"continuedev/continue","slug":"hunk-could-not-be-applied-cleanly-to-source-code","errorCode":null,"errorMessage":"Hunk could not be applied cleanly to source code.","messagePattern":"Hunk could not be applied cleanly to source code\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"core/edit/lazy/unifiedDiffApply.ts","lineNumber":61,"sourceCode":"export function applyUnifiedDiff(\n  sourceCode: string,\n  unifiedDiffText: string,\n): DiffLine[] {\n  const sourceLines = sourceCode.split(/\\r?\\n/);\n  const hunks = parseUnifiedDiff(unifiedDiffText);\n  const diffResult: DiffLine[] = [];\n  let currentPos = 0; // pointer in sourceLines\n\n  for (const hunk of hunks) {\n    const hunkBeforeLines = extractBeforeLines(hunk.lines);\n    const hunkStart = findHunkInSource(\n      sourceLines,\n      hunkBeforeLines,\n      currentPos,\n    );\n    if (hunkStart === -1) {\n      // All hunks must be found in the source code. If not, throw an error.\n      throw new Error(\"Hunk could not be applied cleanly to source code.\");\n    }\n\n    // Emit any unchanged lines that come before this hunk.\n    for (let i = currentPos; i < hunkStart; i++) {\n      diffResult.push({ type: \"same\", line: sourceLines[i] });\n    }\n    let hunkSourcePos = hunkStart;\n\n    for (const dline of hunk.lines) {\n      const srcLine = sourceLines[hunkSourcePos];\n      if (dline.startsWith(\"+\")) {\n        // Insertion: output new line (strip the '+' marker)\n        diffResult.push({ type: \"new\", line: dline.substring(1) });\n      } else if (dline.startsWith(\"-\")) {\n        // Removal: output the removed (old) line and advance the pointer.\n        diffResult.push({ type: \"old\", line: srcLine });\n        hunkSourcePos++;\n      } else {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/continuedev/continue/blob/5522c6f44ca0ac3528b37244818fbfa39b5af470/core/edit/lazy/unifiedDiffApply.ts#L43-L79","documentation":"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.","triggerScenarios":"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.","commonSituations":"Stale diffs applied after edits; LLM-generated diffs with incorrect line numbers/@@ headers; whitespace or line-ending differences between diff and file.","solutions":["Re-apply against the current unmodified file (revert local changes first)","Regenerate the diff/LLM output and retry","Normalize line endings/whitespace before applying","Fall back to a full-file rewrite instead of hunk application"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"const hunksOk = unifiedDiff.every(h => findHunkStart(sourceLines, h.before, 0) !== -1);\nif (!hunksOk) { requestFullFileRewrite(); }","typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["Apply diffs promptly before the file changes","Re-read the file right before applying","Prefer search/replace edits over raw unified diffs from LLMs"],"tags":["diff","apply","edit","llm-output"],"backgroundTag":"patch-conflict","analyzedSha":"5522c6f44ca0ac3528b37244818fbfa39b5af470","analyzedAt":"2026-08-27T11:28:54.683Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}