{"record":{"id":"1e7a11918c5f3db1","repo":"dagger/dagger","slug":"parse-hunk-header-q-w","errorCode":null,"errorMessage":"parse hunk header %q: %w","messagePattern":"parse hunk header %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/directory.go","lineNumber":1958,"sourceCode":"\n// convertRejectToMarkers folds a git-apply .rej file into its target as\n// git-style conflict markers: the surrounding content is left in place (the\n// \"workspace\" side) and each rejected hunk's intended result is inserted at\n// its recorded pre-image position (the \"patch\" side). Best-effort placement:\n// the content has drifted from what the hunk expected — that is why it was\n// rejected — so the markers flag the intent for a human or agent to resolve\n// rather than reproduce the edit exactly.\nfunc convertRejectToMarkers(targetPath, rejPath string) error {\n\trejBytes, err := os.ReadFile(rejPath)\n\tif err != nil {\n\t\treturn err\n\t}\n\tvar hunks []rejectHunk\n\tfor _, line := range strings.Split(string(rejBytes), \"\\n\") {\n\t\tif m := rejectHunkHeader.FindStringSubmatch(line); m != nil {\n\t\t\toldStart, err := strconv.Atoi(m[1])\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"parse hunk header %q: %w\", line, err)\n\t\t\t}\n\t\t\thunks = append(hunks, rejectHunk{oldStart: oldStart})\n\t\t\tcontinue\n\t\t}\n\t\tif len(hunks) == 0 {\n\t\t\tcontinue // file header\n\t\t}\n\t\th := &hunks[len(hunks)-1]\n\t\tif strings.HasPrefix(line, \" \") || strings.HasPrefix(line, \"+\") {\n\t\t\th.theirs = append(h.theirs, line[1:])\n\t\t}\n\t}\n\tif len(hunks) == 0 {\n\t\treturn nil\n\t}\n\n\tvar lines []string\n\ttrailingNewline := true","sourceCodeStart":1940,"sourceCodeEnd":1976,"githubUrl":"https://github.com/dagger/dagger/blob/82ba2681dbe30d3547a1dc50ea495900ab5b6047/core/directory.go#L1940-L1976","documentation":"When parsing a .rej (reject) file produced by `git apply`, each hunk header (e.g. `@@ -12,7 +12,8 @@`) is matched with rejectHunkHeader and its old start line parsed with strconv.Atoi. This error wraps a non-numeric or unexpected hunk-header line number.","triggerScenarios":"A malformed or hand-edited .rej/.patch whose hunk header looks like a match to rejectHunkHeader but whose line-number group is not a plain integer (e.g. `@@ -a,3 +b,4 @@`), or a regex false-positive on an unusual line.","commonSituations":"Patches edited by hand or by tooling that corrupted the @@ header; patches with nonstandard formats (unified diff variants); .rej files truncated or concatenated from multiple patches.","solutions":["Open the .rej/.patch file and correct the malformed @@ hunk header so the old start line is an integer.","Regenerate the patch/diff with `git diff` or `git apply` to replace the corrupted file.","If the .rej came from your own tooling, fix the generator to emit standard unified-diff headers.","Apply the rejected hunks manually and commit, bypassing automatic conflict-marker conversion."],"exampleFix":"// before (malformed hunk header)\n@@ -a,7 +b,8 @@\n// after\n@@ -12,7 +12,8 @@","handlingStrategy":"validation","validationCode":"// validate hunk headers are well-formed before processing\nconst hunkRe = /^@@ -(\\d+)(?:,\\d+)? \\+\\d+(?:,\\d+)? @@/;\nfor (const line of rejText.split(\"\\n\")) {\n    if (line.startsWith(\"@@\") && !hunkRe.test(line)) {\n        throw new Error(`malformed hunk header: ${line}`);\n    }\n}","typeGuard":"function isWellFormedHunkHeader(line) {\n  const m = /^@@ -(\\d+)(?:,\\d+)? \\+\\d+(?:,\\d+)? @@/.exec(line);\n  return m !== null && Number.isInteger(parseInt(m[1], 10));\n}","tryCatchPattern":"try {\n    dir.patch(diff)\n} catch (err) {\n    if (String(err).includes(\"parse hunk header\")) {\n        // regenerate the patch programmatically\n        diff = regenerateDiff(baseDir, targetDir)\n    } else throw err\n}","preventionTips":["Never hand-edit @@ hunk headers.","Regenerate patches with `git diff` instead of fixing them manually.","Validate generated patches with `git apply --check` before storage.","Watch for tools that rewrite diff files (editors, formatters) corrupting headers."],"tags":["git","patch","parsing","diff"],"backgroundTag":"malformed-patch-hunk-header","analyzedSha":"82ba2681dbe30d3547a1dc50ea495900ab5b6047","analyzedAt":"2026-09-05T07:21:37.930Z","contentChangedAt":"2026-09-05T07:21:37.930Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}