From 69982339ed3c35df890f205fada0bb5772abdb63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fern=C3=A1ndez=20L=C3=B3pez?= Date: Wed, 13 Sep 2023 11:14:15 +0200 Subject: [PATCH] std: add compile error when using `std.os.getenv` on the wasi target `std.process.getEnvMap` or `std.process.getEnvVarOwned` should be used instead, requiring allocation. --- lib/std/os.zig | 2 ++ lib/std/os/test.zig | 5 +++++ lib/std/zig/system/NativePaths.zig | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/std/os.zig b/lib/std/os.zig index 0e4606f443..daf485c49e 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1908,6 +1908,8 @@ pub fn getenv(key: []const u8) ?[:0]const u8 { } if (builtin.os.tag == .windows) { @compileError("std.os.getenv is unavailable for Windows because environment string is in WTF-16 format. See std.process.getEnvVarOwned for cross-platform API or std.os.getenvW for Windows-specific API."); + } else if (builtin.os.tag == .wasi) { + @compileError("std.os.getenv is unavailable for WASI. See std.process.getEnvMap or std.process.getEnvVarOwned for a cross-platform API."); } // The simplified start logic doesn't populate environ. if (std.start.simplified_logic) return null; diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig index b9ea4f2e2d..58acb33de5 100644 --- a/lib/std/os/test.zig +++ b/lib/std/os/test.zig @@ -660,6 +660,11 @@ test "mmap" { } test "getenv" { + if (native_os == .wasi and !builtin.link_libc) { + // std.os.getenv is not supported on WASI due to the need of allocation + return error.SkipZigTest; + } + if (native_os == .windows) { try expect(os.getenvW(&[_:0]u16{ 'B', 'O', 'G', 'U', 'S', 0x11, 0x22, 0x33, 0x44, 0x55 }) == null); } else { diff --git a/lib/std/zig/system/NativePaths.zig b/lib/std/zig/system/NativePaths.zig index 99ff554106..b2608a2c5e 100644 --- a/lib/std/zig/system/NativePaths.zig +++ b/lib/std/zig/system/NativePaths.zig @@ -100,7 +100,7 @@ pub fn detect(arena: Allocator, native_info: NativeTargetInfo) !NativePaths { return self; } - if (builtin.os.tag != .windows) { + if (builtin.os.tag != .windows and builtin.os.tag != .wasi) { const triple = try native_target.linuxTriple(arena); const qual = native_target.ptrBitWidth();