Skip to content

Leading slash in std::env::current_dir output on Windows #10415

Open
@maxdeviant

Description

@maxdeviant

We use Wasm and Wasmtime to power the extension system in Zed 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. 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:

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIncorrect behavior in the current implementation that needs fixing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions