From 689c8b57e7cc8f9b834dfd041cd5fa493c8018d2 Mon Sep 17 00:00:00 2001 From: Jiwoo Ahn Date: Sun, 9 Aug 2026 15:09:18 +0900 Subject: [PATCH] wasi: fix os.environb byte handling (#8476) Part of: #4583 This will enable -m test for wasi preview 1 Signed-off-by: Jiwoo Ahn --- crates/host_env/src/os.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/host_env/src/os.rs b/crates/host_env/src/os.rs index 01711687c..7af8f5861 100644 --- a/crates/host_env/src/os.rs +++ b/crates/host_env/src/os.rs @@ -519,13 +519,14 @@ pub fn set_errno(value: i32) { #[cfg(not(any(unix, windows, target_os = "wasi")))] pub fn set_errno(_value: i32) {} -#[cfg(unix)] +// WASIp1, like Unix, provides byte-preserving OsStr conversions. +#[cfg(any(unix, all(target_os = "wasi", not(target_env = "p2"))))] pub fn bytes_as_os_str(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> { - use std::os::unix::ffi::OsStrExt; + use self::ffi::OsStrExt; Ok(std::ffi::OsStr::from_bytes(b)) } -#[cfg(not(unix))] +#[cfg(not(any(unix, all(target_os = "wasi", not(target_env = "p2")))))] pub fn bytes_as_os_str(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> { Ok(core::str::from_utf8(b)?.as_ref()) }