bytecodealliance / bytecodealliance/wasmtime

Leading slash in `std::env::current_dir` output on Windows

Open
#10,415 3 comments 5 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
18.6k
Forks
1.8k
Avg merge
1d 18h
Merged PRs (30d)
126

Description

We use Wasm and Wasmtime to power the extension system in [Zed](https://zed.dev/) and are seeing some undesired behavior with `std::env::current_dir` when executing Wasm on Windows.

I have created a minimized test case that I believe showcases the issue. The repo can be found at [maxdeviant/wasmtime-current-dir-repro](https://github.com/maxdeviant/wasmtime-current-dir-repro/tree/bac35fa8d815ad46b8446d307f0d7ea3897a0d2a). It contains CI set up to run the reproduction case on Windows, as well as on macOS and Linux to contrast the behaviors.

If the Wasmtime repo isn't the right place for this issue, let me know where I can redirect it.

### Test Case

In the reproduction repo above, I have the following Rust program:

```rs
fn main() {
let pwd = std::env::var("PWD").expect("failed to get PWD");
println!("PWD: {pwd:?}");

println!(
"Before: {:?}",
std::env::current_dir().expect("failed to get current_dir")
);

std::env::set_current_dir(pwd).expect("failed to set current_dir");

println!(
"After: {:?}",
std::env::current_dir().expect("failed to get current_dir")
);
}
```

When run on Windows with the `PWD` environment variable set to `D:/a/wasmtime-current-dir-repro/wasmtime-current-dir-repro` this is the output of the program:

```
PWD: "D:/a/wasmtime-current-dir-repro/wasmtime-current-dir-repro"
Before: "/"
After: "/D:/a/wasmtime-current-dir-repro/wasmtime-current-dir-repro"
```

### Steps to Reproduce

1. Compile the above program with `cargo build --target wasm32-wasip1`
2. Run the Wasm program on Windows with `wasmtime run --dir=$PWD --env PWD ./target/wasm32-wasip1/debug/wasm-current-dir-repro.wasm`
3. Observe that the `std::env::current_dir` path in the `After: ` line contains a leading slash

### Expected Results

`std::env::current_dir` returns `D:/a/wasmtime-current-dir-repro/wasmtime-current-dir-repro` as the path.

### Actual Results

`std::env::current_dir` returns `/D:/a/wasmtime-current-dir-repro/wasmtime-current-dir-repro` as the path (note the leading slash).

### Versions and Environment

Wasmtime version or commit: `wasmtime 30.0.2 (398694a59 2025-02-25)`

Operating system: Windows 10

Architecture: x64

### Extra Info

I also tried testing this with `--target wasm32-wasip2` to see if it would make a difference, and it did not: the output remained the same on Windows (containing a leading slash).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.