facebook/flow · error
content_of_file_input_unsafe: failed to read file
Error message
content_of_file_input_unsafe: failed to read file
What it means
FileInput::content_of_file_input_unsafe reads the file named by the FileName variant with std::fs::read_to_string and expects success (rust_port/crates/flow_server_utils/src/file_input.rs:31-37). It panics when the path does not exist, permissions block the read, or the bytes are not valid UTF-8. The crate deliberately ships the safe twin content_of_file_input() returning Result<String, String>; calling the _unsafe variant is opting into a process-killing panic on ordinary I/O failure.
Source
Thrown at rust_port/crates/flow_server_utils/src/file_input.rs:36
match self {
FileInput::FileName(f) => Some(f),
FileInput::FileContent(Some(f), _) => Some(f),
_ => None,
}
}
pub fn filename_of_file_input(&self) -> &str {
match self {
FileInput::FileName(f) => f,
FileInput::FileContent(Some(f), _) => f,
FileInput::FileContent(None, _) => "-",
}
}
pub fn content_of_file_input_unsafe(&self) -> String {
match self {
FileInput::FileName(f) => std::fs::read_to_string(f)
.expect("content_of_file_input_unsafe: failed to read file"),
FileInput::FileContent(_, content) => content.to_string(),
}
}
pub fn content_of_file_input(&self) -> Result<String, String> {
match self {
FileInput::FileName(f) => std::fs::read_to_string(f).map_err(|e| format!("{}", e)),
FileInput::FileContent(_, content) => Ok(content.to_string()),
}
}
pub fn content_of_file_input_arc(&self) -> Result<Arc<str>, String> {
match self {
FileInput::FileName(f) => std::fs::read_to_string(f)
.map(Arc::<str>::from)
.map_err(|e| format!("{}", e)),
FileInput::FileContent(_, content) => Ok(content.clone()),
}View on GitHub (pinned to f88ac94bcf)
Solutions
- Switch to content_of_file_input() (or content_of_file_input_arc()) and handle Err(String) as a normal error
- Verify the path exists and is readable before constructing FileInput::FileName; prefer FileInput::FileContent to inline contents
- If non-UTF-8 input is legitimate, read the bytes and transcode explicitly rather than relying on read_to_string
- Grep for remaining content_of_file_input_unsafe call sites and remove them
Example fix
// before
let content = input.content_of_file_input_unsafe();
// after
let content = input
.content_of_file_input()
.map_err(|e| format!("failed to read {}: {e}", input.filename_of_file_input()))?; Defensive patterns
Strategy: type-guard
Validate before calling
// Narrow the enum before reading: only FileName can fail
if matches!(input, FileInput::FileName(_)) {
let meta = std::fs::metadata(input.filename_of_file_input());
if meta.is_err() || !meta.unwrap().is_file() {
return Err(format!("input file missing: {}", input.filename_of_file_input()));
}
} Type guard
fn safe_content(input: &FileInput) -> Result<String, String> {
match input {
FileInput::FileName(f) => std::fs::read_to_string(f).map_err(|e| e.to_string()),
FileInput::FileContent(_, c) => Ok(c.to_string()),
}
} Prevention
- Ban content_of_file_input_unsafe in code review; use the Result-returning twin
- Prefer FileContent when the client already has the text
- Handle non-UTF-8 sources explicitly instead of relying on read_to_string
When it happens
Trigger: Calling content_of_file_input_unsafe() on FileInput::FileName(p) where p is missing, unreadable, or non-UTF-8 (binary or legacy-encoded files). FileContent variants return the inlined string and can never panic here.
Common situations: Server code paths that took the shortcut variant; stale paths after file deletion or rename; path mismatches across container bind mounts or operating systems; files in non-UTF-8 encodings.
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
- check-contents input should be readable
- Unknown exception reading from the server: {}
- Error sending command to server: {}
- invalid line
- invalid column
AI-assisted analysis of facebook/flow@f88ac94bcf (2026-08-20).
Data as JSON: /api/errors/d83373be42bb15b0.
Report an issue: GitHub.