{"record":{"id":"79e70e586e6e482a","repo":"Hmbown/CodeWhale","slug":"patch-is-empty","errorCode":null,"errorMessage":"Patch is empty.","messagePattern":"Patch is empty\\.","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/lib.rs","lineNumber":8419,"sourceCode":"    } else {\n        \"working-tree\".to_string()\n    };\n    if let Some(path) = &args.path {\n        label.push(' ');\n        label.push_str(path.to_string_lossy().as_ref());\n    }\n    label\n}\n\nfn run_apply(args: ApplyArgs) -> Result<()> {\n    let patch = if let Some(path) = args.patch_file {\n        std::fs::read_to_string(&path)\n            .map_err(|e| anyhow::anyhow!(\"Failed to read patch {}: {}\", path.display(), e))?\n    } else {\n        read_patch_from_stdin()?\n    };\n    if patch.trim().is_empty() {\n        bail!(\"Patch is empty.\");\n    }\n\n    let mut tmp = NamedTempFile::new()?;\n    tmp.write_all(patch.as_bytes())?;\n    let tmp_path = tmp.path().to_path_buf();\n\n    let output = crate::dependencies::Git::command()\n        .ok_or_else(|| anyhow::anyhow!(\"git not found on PATH\"))?\n        .arg(\"apply\")\n        .arg(\"--whitespace=nowarn\")\n        .arg(&tmp_path)\n        .output()\n        .map_err(|e| anyhow::anyhow!(\"Failed to run git apply: {e}\"))?;\n\n    if !output.status.success() {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        bail!(\"git apply failed: {}\", stderr.trim());\n    }","sourceCodeStart":8401,"sourceCodeEnd":8437,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/0c42157ee52f9d55af2b506d71b46249910f77d3/crates/tui/src/lib.rs#L8401-L8437","documentation":"run_apply reads the patch from --patch-file or stdin and refuses it before invoking git when the content is empty after trimming. It is an input guard: an all-whitespace patch is always a caller mistake (bad pipe, wrong file, empty model output), and applying it would be a no-op at best.","triggerScenarios":"--patch-file points at an empty or whitespace-only file; stdin piped from a producer that wrote nothing; the model/tool upstream returned an empty diff block that was saved to disk.","commonSituations":"LLM answered with prose instead of a diff and the pipeline saved the empty fenced block; upstream generate step failed silently because the script lacks `set -e`; truncated download or wrong file path.","solutions":["Inspect the file: `wc -c patch.diff` and `head patch.diff`","Regenerate the patch at its source (re-run the tool/model that produced it)","If piping, check the producer's exit status and output before applying","Confirm the path is a file, not a directory or wrong name"],"exampleFix":"# before\ncodewhale apply --patch-file empty.diff   # Patch is empty.\n\n# after\n[ -s patch.diff ] || { echo 'producer wrote nothing'; exit 1; }\ncodewhale apply --patch-file patch.diff","handlingStrategy":"validation","validationCode":"[ -s \"$PATCH\" ] && [ -n \"$(tr -d '[:space:]' < \"$PATCH\")\" ] || { echo \"patch '$PATCH' is empty\"; exit 1; }","typeGuard":null,"tryCatchPattern":"if let Err(e) = run_apply(args) {\n    if e.to_string().contains(\"Patch is empty\") {\n        // regenerate at the source; do not retry the same input\n    }\n}","preventionTips":["Use `set -euo pipefail` so a failed diff producer aborts before apply","Check `[ -s file ]` before passing --patch-file","Validate that model/tool output actually contains a non-empty diff before saving it"],"tags":["patch","input-validation","git-apply"],"backgroundTag":"input-validation-failed","analyzedSha":"0c42157ee52f9d55af2b506d71b46249910f77d3","analyzedAt":"2026-08-20T21:50:45.477Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}