authorgravatar for cartersnook04@gmail.comCarter Snook <cartersnook04@gmail.com> 2025-01-27 10:53:32-06:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-27 17:53:32+01:00
log2043e8ae05449dc92ffaaf480fc22440cc9181ae
treeb2e61d4f13d718b0c6d223240e45bb1a212354b5
parent5647dd84c1930a70422fd80a7c2207c81cf858d8
signaturebadge-check Signed by PGP key B5690EEEBB952194

main: classify empty environment variables as unset

This matches existing well-specified conventions for e.g. NO_COLOR. Closes #22380.

2 files changed, 14 insertions(+), 13 deletions(-)

lib/std/zig.zig+5-4
...@@ -700,10 +700,6 @@ pub const EnvVar = enum {...@@ -700,10 +700,6 @@ pub const EnvVar = enum {
700 XDG_CACHE_HOME,700 XDG_CACHE_HOME,
701 HOME,701 HOME,
702702
703 pub fn isSet(comptime ev: EnvVar) bool {
704 return std.process.hasEnvVarConstant(@tagName(ev));
705 }
706
707 pub fn get(ev: EnvVar, arena: std.mem.Allocator) !?[]u8 {703 pub fn get(ev: EnvVar, arena: std.mem.Allocator) !?[]u8 {
708 if (std.process.getEnvVarOwned(arena, @tagName(ev))) |value| {704 if (std.process.getEnvVarOwned(arena, @tagName(ev))) |value| {
709 return value;705 return value;
...@@ -716,6 +712,11 @@ pub const EnvVar = enum {...@@ -716,6 +712,11 @@ pub const EnvVar = enum {
716 pub fn getPosix(comptime ev: EnvVar) ?[:0]const u8 {712 pub fn getPosix(comptime ev: EnvVar) ?[:0]const u8 {
717 return std.posix.getenvZ(@tagName(ev));713 return std.posix.getenvZ(@tagName(ev));
718 }714 }
715
716 pub fn isSet(ev: EnvVar, arena: std.mem.Allocator) !bool {
717 const value = try ev.get(arena) orelse return false;
718 return value.len != 0;
719 }
719};720};
720721
721pub const SimpleComptimeReason = enum(u32) {722pub const SimpleComptimeReason = enum(u32) {
src/main.zig+9-9
...@@ -826,9 +826,9 @@ fn buildOutputType(...@@ -826,9 +826,9 @@ fn buildOutputType(
826 var listen: Listen = .none;826 var listen: Listen = .none;
827 var debug_compile_errors = false;827 var debug_compile_errors = false;
828 var verbose_link = (native_os != .wasi or builtin.link_libc) and828 var verbose_link = (native_os != .wasi or builtin.link_libc) and
829 EnvVar.ZIG_VERBOSE_LINK.isSet();829 try EnvVar.ZIG_VERBOSE_LINK.isSet(arena);
830 var verbose_cc = (native_os != .wasi or builtin.link_libc) and830 var verbose_cc = (native_os != .wasi or builtin.link_libc) and
831 EnvVar.ZIG_VERBOSE_CC.isSet();831 try EnvVar.ZIG_VERBOSE_CC.isSet(arena);
832 var verbose_air = false;832 var verbose_air = false;
833 var verbose_intern_pool = false;833 var verbose_intern_pool = false;
834 var verbose_generic_instances = false;834 var verbose_generic_instances = false;
...@@ -1006,9 +1006,9 @@ fn buildOutputType(...@@ -1006,9 +1006,9 @@ fn buildOutputType(
1006 // if set, default the color setting to .off or .on, respectively1006 // if set, default the color setting to .off or .on, respectively
1007 // explicit --color arguments will still override this setting.1007 // explicit --color arguments will still override this setting.
1008 // Disable color on WASI per https://github.com/WebAssembly/WASI/issues/1621008 // Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162
1009 var color: Color = if (native_os == .wasi or EnvVar.NO_COLOR.isSet())1009 var color: Color = if (native_os == .wasi or try EnvVar.NO_COLOR.isSet(arena))
1010 .off1010 .off
1011 else if (EnvVar.CLICOLOR_FORCE.isSet())1011 else if (try EnvVar.CLICOLOR_FORCE.isSet(arena))
1012 .on1012 .on
1013 else1013 else
1014 .auto;1014 .auto;
...@@ -4749,9 +4749,9 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -4749,9 +4749,9 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4749 var reference_trace: ?u32 = null;4749 var reference_trace: ?u32 = null;
4750 var debug_compile_errors = false;4750 var debug_compile_errors = false;
4751 var verbose_link = (native_os != .wasi or builtin.link_libc) and4751 var verbose_link = (native_os != .wasi or builtin.link_libc) and
4752 EnvVar.ZIG_VERBOSE_LINK.isSet();4752 try EnvVar.ZIG_VERBOSE_LINK.isSet(arena);
4753 var verbose_cc = (native_os != .wasi or builtin.link_libc) and4753 var verbose_cc = (native_os != .wasi or builtin.link_libc) and
4754 EnvVar.ZIG_VERBOSE_CC.isSet();4754 try EnvVar.ZIG_VERBOSE_CC.isSet(arena);
4755 var verbose_air = false;4755 var verbose_air = false;
4756 var verbose_intern_pool = false;4756 var verbose_intern_pool = false;
4757 var verbose_generic_instances = false;4757 var verbose_generic_instances = false;
...@@ -4934,7 +4934,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -4934,7 +4934,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4934 }4934 }
49354935
4936 const work_around_btrfs_bug = native_os == .linux and4936 const work_around_btrfs_bug = native_os == .linux and
4937 EnvVar.ZIG_BTRFS_WORKAROUND.isSet();4937 try EnvVar.ZIG_BTRFS_WORKAROUND.isSet(arena);
4938 const root_prog_node = std.Progress.start(.{4938 const root_prog_node = std.Progress.start(.{
4939 .disable_printing = (color == .off),4939 .disable_printing = (color == .off),
4940 .root_name = "Compile Build Script",4940 .root_name = "Compile Build Script",
...@@ -5433,7 +5433,7 @@ fn jitCmd(...@@ -5433,7 +5433,7 @@ fn jitCmd(
5433 fatal("unable to find self exe path: {s}", .{@errorName(err)});5433 fatal("unable to find self exe path: {s}", .{@errorName(err)});
5434 };5434 };
54355435
5436 const optimize_mode: std.builtin.OptimizeMode = if (EnvVar.ZIG_DEBUG_CMD.isSet())5436 const optimize_mode: std.builtin.OptimizeMode = if (try EnvVar.ZIG_DEBUG_CMD.isSet(arena))
5437 .Debug5437 .Debug
5438 else5438 else
5439 .ReleaseFast;5439 .ReleaseFast;
...@@ -6965,7 +6965,7 @@ fn cmdFetch(...@@ -6965,7 +6965,7 @@ fn cmdFetch(
69656965
6966 const color: Color = .auto;6966 const color: Color = .auto;
6967 const work_around_btrfs_bug = native_os == .linux and6967 const work_around_btrfs_bug = native_os == .linux and
6968 EnvVar.ZIG_BTRFS_WORKAROUND.isSet();6968 try EnvVar.ZIG_BTRFS_WORKAROUND.isSet(arena);
6969 var opt_path_or_url: ?[]const u8 = null;6969 var opt_path_or_url: ?[]const u8 = null;
6970 var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);6970 var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
6971 var debug_hash: bool = false;6971 var debug_hash: bool = false;