{"record":{"id":"c969cf1b45fa1494","repo":"Hmbown/CodeWhale","slug":"failed-to-run-git-apply-e","errorCode":null,"errorMessage":"Failed to run git apply: {e}","messagePattern":"Failed to run git apply: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/lib.rs","lineNumber":9921,"sourceCode":"            .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    }\n    println!(\"Applied patch successfully.\");\n    Ok(())\n}\n\n/// Maximum bytes read for a patch on stdin. Generous for large diffs;\n/// anything larger is not a patch.\nconst MAX_STDIN_PATCH_BYTES: u64 = 16 * 1024 * 1024;\n\nfn read_patch_from_stdin() -> Result<String> {\n    let stdin = io::stdin();\n    if stdin.is_terminal() {\n        bail!(\"No patch file provided and stdin is empty.\");\n    }","sourceCodeStart":9903,"sourceCodeEnd":9939,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/lib.rs#L9903-L9939","documentation":"Raised when the `git apply` subprocess itself fails to launch — `cmd.output()` returns an io::Error (distinct from git running but exiting non-zero, which produces 'git apply failed: <stderr>'). The patch was already written to a temp file at this point; only process startup failed.","triggerScenarios":"`.output()` errors while spawning `git apply --whitespace=nowarn <tmpfile>` — git disappeared from PATH between check and spawn, exec permission lost, tmpfile path invalid for exec context, or fork/resource exhaustion.","commonSituations":"Race where the environment changes mid-run; SELinux/AppArmor blocking exec; out-of-memory or PID limits in containers preventing fork; temp file on a mount invisible to the spawned git process.","solutions":["Retry the apply; transient fork failures often clear under reduced load","Check container/fd/pid limits (ulimit, cgroup pids.max) if failures are repeated","Confirm git remains on PATH and executable for the whole session","Verify the temp directory (TMPDIR) is writable and accessible to git"],"exampleFix":"// before\n.output()\n.map_err(|e| anyhow::anyhow!(\"Failed to run git apply: {e}\"))?;\n// after\n.output()\n.map_err(|e| anyhow::anyhow!(\"Failed to run git apply: {e} (check git is installed and TMPDIR '{}' is accessible)\", std::env::temp_dir().display()))?;","handlingStrategy":"try-catch","validationCode":"// Rust\nif which::which(\"git\").is_err() {\n    bail!(\"git must be on PATH before running apply\");\n}\nassert!(tmp_path.exists(), \"temp patch file missing before git apply\");","typeGuard":null,"tryCatchPattern":"// Rust\nlet output = cmd.output()\n    .map_err(|e| anyhow::anyhow!(\"Failed to run git apply: {e}\"))?;\nif !output.status.success() {\n    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();\n    bail!(\"git apply failed: {stderr}\");\n}","preventionTips":["Check fd/pid limits in containers that spawn many subprocesses","Keep TMPDIR writable and accessible to child processes","Retry transient spawn failures once before failing the operation","Log stderr of git apply so patch conflicts are diagnosable"],"tags":["git","process","patch"],"backgroundTag":"git-command-failed","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}