rust-lang / rust-lang/rust

std::env::temp_dir() unconditionally panics on wasm32-wasip1/wasm32-wasip2

Open Beginner friendly
#160,363 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

fn main() {
    println!("{:?}", std::env::temp_dir());
}

compiled with rustc --target wasm32-wasip1 main.rs (also reproduces on wasm32-wasip2) and run under wasmtime 47.0.3, with or without TMPDIR set in the environment.

I expected to see the path std would use as a scratch directory — e.g. respecting TMPDIR if set, falling back to /tmp otherwise, the same way sys/paths/unix.rs's temp_dir() behaves for every other Unix-family target.

Instead, this happened: an unconditional panic that aborts the process:

thread 'main' (1) panicked at library/std/src/sys/paths/wasi.rs:44:5:
not supported by WASI yet

sys/paths/wasi.rs implements temp_dir() as a bare panic!, unlike getcwd/chdir in the same file which have real WASI implementations via libc::getcwd/libc::chdir. There is no capability gap forcing this: reading TMPDIR via std::env::var_os and falling back to a /tmp literal, exactly like the unix implementation, requires nothing WASI doesn't already support — env::var_os and plain string literals are both already used elsewhere in the same file and module.

This is not a hypothetical: it's user-visible in real programs built for WASI. In uutils/coreutils, sort and mktemp both call std::env::temp_dir() in their normal (no-argument) code paths; sort was patched around this with an explicit #[cfg(target_os = "wasi")] fallback to a hardcoded /tmp, but mktemp was not (see uutils/coreutils source), and mktemp invoked with no arguments panics/traps immediately when run under wasm32-wasip1 or wasm32-wasip2 via wasmtime — confirmed via local build and wasmtime run.

Suggested fix: give wasi.rs's temp_dir() the same body as unix.rs's (minus the Apple/Android special cases, which don't apply to WASI):

pub fn temp_dir() -> PathBuf {
    crate::env::var_os("TMPDIR").map(PathBuf::from).unwrap_or_else(|| PathBuf::from("/tmp"))
}
Meta

rustc --version --verbose:

rustc 1.97.1 (8bab26f4f 2026-07-14)

Reproduced on both wasm32-wasip1 and wasm32-wasip2, with wasmtime 47.0.3.

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

Start in library/std/src/sys/paths/wasi.rs and compare temp_dir() with the implementation in library/std/src/sys/paths/unix.rs. Reproduce with wasm32-wasip1 or wasm32-wasip2 under wasmtime, checking both TMPDIR set and unset; done means std::env::temp_dir() no longer panics and returns TMPDIR or /tmp.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
operating-systems
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.