authorgravatar for ereslibre@ereslibre.esRafael Fernández López <ereslibre@ereslibre.es> 2023-09-13 11:14:15+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-09-14 20:04:35+03:00
log69982339ed3c35df890f205fada0bb5772abdb63
treeb736d6b26a3fd2afab4721f5783b0bbda2f36839
parent742030a8f27190ce36fd7b240c2def6774dfdb63

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.

3 files changed, 8 insertions(+), 1 deletions(-)

lib/std/os.zig+2
......@@ -1908,6 +1908,8 @@ pub fn getenv(key: []const u8) ?[:0]const u8 {
19081908 }
19091909 if (builtin.os.tag == .windows) {
19101910 @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.");
1911 } else if (builtin.os.tag == .wasi) {
1912 @compileError("std.os.getenv is unavailable for WASI. See std.process.getEnvMap or std.process.getEnvVarOwned for a cross-platform API.");
19111913 }
19121914 // The simplified start logic doesn't populate environ.
19131915 if (std.start.simplified_logic) return null;
lib/std/os/test.zig+5
......@@ -660,6 +660,11 @@ test "mmap" {
660660}
661661
662662test "getenv" {
663 if (native_os == .wasi and !builtin.link_libc) {
664 // std.os.getenv is not supported on WASI due to the need of allocation
665 return error.SkipZigTest;
666 }
667
663668 if (native_os == .windows) {
664669 try expect(os.getenvW(&[_:0]u16{ 'B', 'O', 'G', 'U', 'S', 0x11, 0x22, 0x33, 0x44, 0x55 }) == null);
665670 } else {
lib/std/zig/system/NativePaths.zig+1-1
......@@ -100,7 +100,7 @@ pub fn detect(arena: Allocator, native_info: NativeTargetInfo) !NativePaths {
100100 return self;
101101 }
102102
103 if (builtin.os.tag != .windows) {
103 if (builtin.os.tag != .windows and builtin.os.tag != .wasi) {
104104 const triple = try native_target.linuxTriple(arena);
105105
106106 const qual = native_target.ptrBitWidth();