tracel-ai/burn · error
Should be able to create the new file '{}': {}
Error message
Should be able to create the new file '{}': {} What it means
`FileLogger::new` panics when it cannot open (create/truncate) the log file at the given path with `std::fs::File::options().write(true).truncate(true).create(true)`. The panic message includes the path and the underlying `io::Error`. Burn assumes a file logger path must be creatable at startup, so it fails fast instead of returning `Result`.
Source
Thrown at crates/burn-train/src/logger/file.rs:28
/// Create a new file logger.
///
/// # Arguments
///
/// * `path` - The path.
///
/// # Returns
///
/// The file logger.
pub fn new(path: impl AsRef<Path>) -> Self {
let path = path.as_ref();
let mut options = std::fs::File::options();
let file = options
.write(true)
.truncate(true)
.create(true)
.open(path)
.unwrap_or_else(|err| {
panic!(
"Should be able to create the new file '{}': {}",
path.display(),
err
)
});
Self { file }
}
}
impl<T> Logger<T> for FileLogger
where
T: std::fmt::Display,
{
fn log(&mut self, item: T) {
writeln!(&mut self.file, "{item}").expect("Can log an item.");
}
}View on GitHub (pinned to d16f7ba2ed)
Solutions
- Create the parent directory before constructing the logger: `std::fs::create_dir_all(path.parent().unwrap())`
- Check permissions on the target directory and that the process user can write there
- Confirm the path is a file path, not an existing directory, and uses valid characters for the OS
- Verify the filesystem/mount is writable (not read-only, disk not full)
- If the path is user-supplied, wrap `FileLogger::new` in `std::panic::catch_unwind` or validate writability beforehand
Example fix
// before
let logger = FileLogger::new("/var/log/train/metrics.log"); // parent dir missing
// after
std::fs::create_dir_all("/var/log/train").expect("create log dir");
let logger = FileLogger::new("/var/log/train/metrics.log"); Defensive patterns
Strategy: validation
Validate before calling
fn ensure_log_path_ok(path: &std::path::Path) -> std::io::Result<()> {
if path.is_dir() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "path is a directory"));
}
if let Some(parent) = path.parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
}
// probe writability with a temp create/truncate
std::fs::OpenOptions::new().write(true).create(true).truncate(true).open(path)?;
Ok(())
} Prevention
- Always `create_dir_all` the parent directory of log/checkpoint paths before training starts
- Check directory permissions for the user the training process runs as (especially in Docker/CI)
- Never pass an existing directory as the logger file path
- Confirm the target filesystem is writable (not read-only, disk not full)
- Proactively probe-open log files at startup, before a long training run, so failures surface early
When it happens
Trigger: Calling `FileLogger::new(path)` (or a learner `log`/metric-logger setup using it, e.g. via `FileMetricLogger`) when the path's parent directory does not exist, the directory is not writable, the path is an existing directory, or the filesystem denies creation (permissions, read-only mount, disk full, invalid characters).
Common situations: Pointing the learner's checkpoint/log directory at a path whose parent folders were never created; running training in a container as a non-root user writing to a root-owned dir; read-only NFS/CI workspace; passing a directory instead of a file path; Windows path with illegal characters.
Understand the failure class
Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.
Related errors
- Can delete model checkpoint.
- Failed to load module from file
- Can delete optimizer checkpoint.
- Can delete learning rate scheduler checkpoint.
- Can save model checkpoint.
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/7f11b6fa8b0bdfc8.
Report an issue: GitHub.