Skip to content

wasm32-wasi: opening/reading a directory as a file surfaces EBADF, not IsADirectory #160359

Description

@eduardomourar

I tried this code:

use std::fs::File;
use std::io::Read;

fn main() {
    let mut f = File::open("/some/dir").unwrap(); // succeeds on WASI
    let mut buf = [0u8; 16];
    println!("{:?}", f.read(&mut buf));
}

I expected to see this happen: an Err with kind() == IsADirectory, matching Unix (EISDIR) and Windows.

Instead, this happened: Err with kind() == Uncategorized, raw_os_error() == Some(8) (EBADF). WASI defers the directory check from open to the first read, and reports it as EBADF rather than EISDIR, so it falls through every arm of decode_error_kind (shared between unix/wasi since #143189) to Uncategorized.

Reproduced on both wasm32-wasip1 and wasm32-wasip2 via wasmtime 45.

Note EBADF is ambiguous (a real closed-fd error must not become IsADirectory), so this likely cannot be a decode_error_kind table entry — it probably needs to be handled contextually in the WASI open/File::open path in sys/fs/unix.rs, where std already knows a directory was being opened as a file. See uutils/coreutils#13625 for a userland workaround (wasi_normalize_open_error) doing exactly this at the call site.

Meta

rustc --version --verbose:

rustc 1.99.0-nightly (ad3d0bc14 2026-07-31)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-wasiOperating system: Wasi, Webassembly System Interfaceneeds-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions