rust-lang / rust-lang/rust

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

Open
#160,359 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage O-wasi
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example for wasm32-wasip1 and wasm32-wasip2 via wasmtime, then inspect library/std/src/sys/fs/unix.rs and the surrounding open/File::open path. Trace how the deferred directory read error reaches decode_error_kind; done means opening a directory as a file reports IsADirectory while genuine closed-file EBADF behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.