Hmbown/CodeWhale · error
Failed to read patch
Error message
Failed to read patch {}: {} What it means
Raised by `run_apply` when the `--patch-file` path supplied to the apply command cannot be read with `std::fs::read_to_string`. It wraps the io::Error and includes the file path, so it covers nonexistent files, permission errors, and (via UTF-8 loss) non-UTF-8 content is tolerated but read failures abort. Without a patch file the code falls back to stdin, so this only fires for an explicit bad path.
Solutions
- Verify the path exists and is readable: `ls -l <path>` before running apply
- Use an absolute path, or run from the directory where the patch file lives
- Regenerate the patch file if it was cleaned up or truncated
- Check permissions (`chmod u+r`) and that the file is not a directory
Example fix
// before
let patch = std::fs::read_to_string(&path)
.map_err(|e| anyhow::anyhow!("Failed to read patch {}: {}", path.display(), e))?;
// after
if !path.is_file() { bail!("Patch file {} does not exist", path.display()); }
let patch = std::fs::read_to_string(&path)
.map_err(|e| anyhow::anyhow!("Failed to read patch {}: {}", path.display(), e))?; Defensive patterns
Strategy: validation
Validate before calling
// Rust
if !path.is_file() {
bail!("patch file {} does not exist or is not a file", path.display());
}
let patch = std::fs::read_to_string(&path)?; Try / catch
// Rust
match std::fs::read_to_string(&path) {
Ok(p) => p,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
bail!("patch file {} not found; check the path and CWD", path.display());
}
Err(e) => return Err(e.into()),
} Prevention
- Use absolute paths for patch files
- Confirm the file exists right after generating it, before applying
- Avoid leaving patch files in tmpwatch-cleaned directories
- Check file readability under the sandbox/user that runs apply
When it happens
Trigger: Calling the apply subcommand with a `--patch-file <path>` where the path does not exist, is a directory, or the process lacks read permission — `std::fs::read_to_string` returns Err.
Common situations: Typo in the patch file path; relative path resolved against a different CWD than expected; file deleted by a temp cleaner or another agent between generation and apply; read-only mount or sandbox denying access.
Understand the failure class
Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.
Related errors
- could not inspect
- could not inspect
- could not read
- {error}
- external credential path must name a regular file
AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22).
Data as JSON: /api/errors/1d8ed6f473f572a7.
Report an issue: GitHub.
Appendix: source
Thrown at crates/tui/src/lib.rs:9903
.as_deref()
.map(str::trim)
.filter(|base| !base.is_empty())
{
format!("base:{base}")
} else {
"working-tree".to_string()
};
if let Some(path) = &args.path {
label.push(' ');
label.push_str(path.to_string_lossy().as_ref());
}
label
}
fn run_apply(args: ApplyArgs) -> Result<()> {
let patch = if let Some(path) = args.patch_file {
std::fs::read_to_string(&path)
.map_err(|e| anyhow::anyhow!("Failed to read patch {}: {}", path.display(), e))?
} else {
read_patch_from_stdin()?
};
if patch.trim().is_empty() {
bail!("Patch is empty.");
}
let mut tmp = NamedTempFile::new()?;
tmp.write_all(patch.as_bytes())?;
let tmp_path = tmp.path().to_path_buf();
let output = crate::dependencies::Git::command()
.ok_or_else(|| anyhow::anyhow!("git not found on PATH"))?
.arg("apply")
.arg("--whitespace=nowarn")
.arg(&tmp_path)
.output()
.map_err(|e| anyhow::anyhow!("Failed to run git apply: {e}"))?;View on GitHub (pinned to 73e0f67d83)