Hmbown/CodeWhale · error · io::Error

path has no parent directory

Error message

path has no parent directory: {}

What it means

write_atomic_with_permissions needs the parent directory to stage its temporary file before renaming; it calls path.parent() and errors with ErrorKind::InvalidInput when the path has no parent component. Only paths with no directory part at all hit this (parent() returns None), e.g. a bare file name is usually fine (parent = ""), so this is a genuinely degenerate input guard.

Solutions

  1. Pass an absolute or directory-qualified path (e.g. workspace.join(file_name)).
  2. Canonicalize/validate the path before writing.
  3. Reject empty or component-less paths at the API boundary.

Example fix

// before
write_atomic(Path::new("config.toml"), &bytes)?;
// after
write_atomic(&workspace_root.join("config.toml"), &bytes)?;
Defensive patterns

Strategy: validation

Validate before calling

debug_assert!(path.parent().is_some(), "write path must have a parent: {}", path.display());

Try / catch

if path.parent().is_none() {
    return Err(anyhow!("cannot atomically write path without parent: {}", path.display()));
}

Prevention

When it happens

Trigger: Calling write_atomic or write_atomic_workspace with a Path that has no parent component per std::path::Path::parent() — e.g. a root-relative oddity or a path constructed without any directory component in a context where parent() is None.

Common situations: Programmatically built PathBuf from an empty or malformed string, passing a bare component where an absolute path was expected, path munging that stripped the directory.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/57279e1edc060388. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/utils.rs:388

    const MAX_PERSIST_ATTEMPTS: usize = 6;
    // 5 ERROR_ACCESS_DENIED, 32 ERROR_SHARING_VIOLATION, 33 ERROR_LOCK_VIOLATION.
    let transient = error.kind() == std::io::ErrorKind::PermissionDenied
        || matches!(error.raw_os_error(), Some(5 | 32 | 33));
    if !transient || attempt + 1 >= MAX_PERSIST_ATTEMPTS {
        return None;
    }
    Some(std::time::Duration::from_millis(
        10u64.saturating_mul(1u64 << attempt),
    ))
}

fn write_atomic_with_permissions(
    path: &Path,
    contents: &[u8],
    #[cfg_attr(not(unix), allow(unused_variables))] permission_policy: AtomicWritePermissions,
) -> std::io::Result<()> {
    let parent = path.parent().ok_or_else(|| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!("path has no parent directory: {}", path.display()),
        )
    })?;

    // Capture ordinary rwx bits before replacement. Use symlink_metadata so we
    // do not follow links: an inaccessible or dangling symlink target must not
    // abort the write — rename still replaces the directory entry, matching
    // the pre-#4606 private write_atomic behavior. Symlink entries themselves
    // are treated as "no mode to preserve" (new ordinary file after rename);
    // only regular-file modes are restored. Mask with 0o777 so setuid/setgid/
    // sticky are never restored after rewriting content.
    #[cfg(unix)]
    let existing_workspace_mode = if permission_policy == AtomicWritePermissions::Workspace {
        match fs::symlink_metadata(path) {
            Ok(metadata) if metadata.file_type().is_symlink() => None,
            Ok(metadata) => {
                use std::os::unix::fs::PermissionsExt;

View on GitHub (pinned to 73e0f67d83)