From 8660cdaad5d2607136f2c2b6e4693cce15ef8e0e Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Sun, 2 Feb 2025 12:58:42 -0600 Subject: [PATCH 1/3] core: add explicit errors to src/os/env.zig --- src/os/env.zig | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/os/env.zig b/src/os/env.zig index cf6cc0fe7..d1cbbc01f 100644 --- a/src/os/env.zig +++ b/src/os/env.zig @@ -3,13 +3,15 @@ const builtin = @import("builtin"); const Allocator = std.mem.Allocator; const posix = std.posix; +pub const Errors = Allocator.Error; + /// Append a value to an environment variable such as PATH. /// The returned value is always allocated so it must be freed. pub fn appendEnv( alloc: Allocator, current: []const u8, value: []const u8, -) ![]u8 { +) Errors![]u8 { // If there is no prior value, we return it as-is if (current.len == 0) return try alloc.dupe(u8, value); @@ -26,7 +28,7 @@ pub fn appendEnvAlways( alloc: Allocator, current: []const u8, value: []const u8, -) ![]u8 { +) Errors![]u8 { return try std.fmt.allocPrint(alloc, "{s}{c}{s}", .{ current, std.fs.path.delimiter, @@ -40,7 +42,7 @@ pub fn prependEnv( alloc: Allocator, current: []const u8, value: []const u8, -) ![]u8 { +) Errors![]u8 { // If there is no prior value, we return it as-is if (current.len == 0) return try alloc.dupe(u8, value); @@ -68,7 +70,7 @@ pub const GetEnvResult = struct { /// This will allocate on Windows but not on other platforms. The returned /// value should have deinit called to do the proper cleanup no matter what /// platform you are on. -pub fn getenv(alloc: Allocator, key: []const u8) !?GetEnvResult { +pub fn getenv(alloc: Allocator, key: []const u8) Errors!?GetEnvResult { return switch (builtin.os.tag) { // Non-Windows doesn't need to allocate else => if (posix.getenv(key)) |v| .{ .value = v } else null, From 002cce4e81c1d67fbc163e5ae4d97ac5e1237474 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Sun, 2 Feb 2025 13:16:53 -0600 Subject: [PATCH 2/3] core: handle src/os/env.zig errors on windows --- src/os/env.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/os/env.zig b/src/os/env.zig index d1cbbc01f..f3611663b 100644 --- a/src/os/env.zig +++ b/src/os/env.zig @@ -80,7 +80,8 @@ pub fn getenv(alloc: Allocator, key: []const u8) Errors!?GetEnvResult { .value = v, } else |err| switch (err) { error.EnvironmentVariableNotFound => null, - else => err, + error.InvalidWtf8 => null, + else => |e| e, }, }; } From 8607d463ff3bd5e48bb85498ea329bce44c511c8 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Mon, 3 Feb 2025 14:38:00 -0600 Subject: [PATCH 3/3] core: fix puralization of src/os/env.zig Errors->Error --- src/os/env.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/os/env.zig b/src/os/env.zig index f3611663b..fe2be20de 100644 --- a/src/os/env.zig +++ b/src/os/env.zig @@ -3,7 +3,7 @@ const builtin = @import("builtin"); const Allocator = std.mem.Allocator; const posix = std.posix; -pub const Errors = Allocator.Error; +pub const Error = Allocator.Error; /// Append a value to an environment variable such as PATH. /// The returned value is always allocated so it must be freed. @@ -11,7 +11,7 @@ pub fn appendEnv( alloc: Allocator, current: []const u8, value: []const u8, -) Errors![]u8 { +) Error![]u8 { // If there is no prior value, we return it as-is if (current.len == 0) return try alloc.dupe(u8, value); @@ -28,7 +28,7 @@ pub fn appendEnvAlways( alloc: Allocator, current: []const u8, value: []const u8, -) Errors![]u8 { +) Error![]u8 { return try std.fmt.allocPrint(alloc, "{s}{c}{s}", .{ current, std.fs.path.delimiter, @@ -42,7 +42,7 @@ pub fn prependEnv( alloc: Allocator, current: []const u8, value: []const u8, -) Errors![]u8 { +) Error![]u8 { // If there is no prior value, we return it as-is if (current.len == 0) return try alloc.dupe(u8, value); @@ -70,7 +70,7 @@ pub const GetEnvResult = struct { /// This will allocate on Windows but not on other platforms. The returned /// value should have deinit called to do the proper cleanup no matter what /// platform you are on. -pub fn getenv(alloc: Allocator, key: []const u8) Errors!?GetEnvResult { +pub fn getenv(alloc: Allocator, key: []const u8) Error!?GetEnvResult { return switch (builtin.os.tag) { // Non-Windows doesn't need to allocate else => if (posix.getenv(key)) |v| .{ .value = v } else null,