jdx/mise · error · eyre::Report

refusing to remove directory as a file

Error message

refusing to remove directory as a file: {}

What it means

`remove_file` in the managed-files module checks the entry's type with `fs::symlink_metadata` before deleting. If the path is actually a directory, it refuses with this error instead of calling `fs::remove_file`, which would fail confusingly on most platforms. This prevents accidentally treating a directory as a deletable file.

Solutions

  1. Use the directory-removal API (managed directory removal) instead of the file-removal API
  2. Manually remove the directory (`rm -r <path>`) if it is no longer needed
  3. Verify the path in the manifest matches the intended kind on disk

Example fix

// before
remove_managed_file(Path::new("/etc/app/old.d"))?; // it's a directory
// after
remove_managed_directory(Path::new("/etc/app/old.d"))?;
Defensive patterns

Strategy: validation

Validate before calling

fn safe_remove(path: &std::path::Path) -> std::io::Result<()> {
    match std::fs::symlink_metadata(path) {
        Ok(m) if m.is_dir() => std::fs::remove_dir_all(path),
        Ok(_) => std::fs::remove_file(path),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(e) => Err(e),
    }
}

Type guard

fn is_dir_path(path: &std::path::Path) -> bool {
    std::fs::symlink_metadata(path).map_or(false, |m| m.is_dir())
}

Try / catch

match result {
    Err(e) if e.to_string().contains("refusing to remove directory as a file") => {
        remove_dir_all(path)?; // only if the contents are safe to delete
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling the managed-file removal API on a path whose symlink metadata reports a directory — e.g. the manifest previously declared a directory at that path, or the user replaced the file with a directory between runs.

Common situations: State drift: a managed path changed kind on disk; a user replaced a managed config file with a directory; manifest corrections that repurpose a path.

Related errors


AI-assisted analysis of jdx/mise@533346cc37 (2026-09-17). Data as JSON: /api/errors/1394bc36f31e5170. Report an issue: GitHub.

Appendix: source

Thrown at src/system/managed_files.rs:1578

                )
            })?
        }
        Ok(_) => fs::remove_file(path)?,
    }
    temporary
        .persist(path)
        .map_err(|error| error.error)
        .wrap_err_with(|| format!("failed to atomically replace {}", path.display()))?;
    fs::File::open(parent)?.sync_all()?;
    Ok(())
}

fn remove_file(path: &Path) -> Result<()> {
    match fs::symlink_metadata(path) {
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(err) => Err(err.into()),
        Ok(metadata) if metadata.file_type().is_dir() => {
            bail!("refusing to remove directory as a file: {}", path.display())
        }
        Ok(_) => fs::remove_file(path).map_err(Into::into),
    }
}

fn create_directory(
    path: &Path,
    owner: Option<&str>,
    group: Option<&str>,
    mode: u32,
    replace: bool,
) -> Result<()> {
    match fs::symlink_metadata(path) {
        Err(error)
            if matches!(
                error.kind(),
                std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory
            ) => {}

View on GitHub (pinned to 533346cc37)