Skip to content

Commit 185f9cf

Browse files
committed
Classify empty environment variables as unset
Modifies EnvVar.isSet() to assume empty strings are unset. This is required for correctly parsing NO_COLOR values, and @ifreund suggested that all variables should follow this behavior. Closes #22380.
1 parent 4b910e5 commit 185f9cf

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

lib/std/zig.zig

+5-4
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,6 @@ pub const EnvVar = enum {
700700
XDG_CACHE_HOME,
701701
HOME,
702702

703-
pub fn isSet(comptime ev: EnvVar) bool {
704-
return std.process.hasEnvVarConstant(@tagName(ev));
705-
}
706-
707703
pub fn get(ev: EnvVar, arena: std.mem.Allocator) !?[]u8 {
708704
if (std.process.getEnvVarOwned(arena, @tagName(ev))) |value| {
709705
return value;
@@ -716,6 +712,11 @@ pub const EnvVar = enum {
716712
pub fn getPosix(comptime ev: EnvVar) ?[:0]const u8 {
717713
return std.posix.getenvZ(@tagName(ev));
718714
}
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+
}
719720
};
720721

721722
pub const SimpleComptimeReason = enum(u32) {

src/main.zig

+9-9
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,9 @@ fn buildOutputType(
816816
var listen: Listen = .none;
817817
var debug_compile_errors = false;
818818
var verbose_link = (native_os != .wasi or builtin.link_libc) and
819-
EnvVar.ZIG_VERBOSE_LINK.isSet();
819+
try EnvVar.ZIG_VERBOSE_LINK.isSet(arena);
820820
var verbose_cc = (native_os != .wasi or builtin.link_libc) and
821-
EnvVar.ZIG_VERBOSE_CC.isSet();
821+
try EnvVar.ZIG_VERBOSE_CC.isSet(arena);
822822
var verbose_air = false;
823823
var verbose_intern_pool = false;
824824
var verbose_generic_instances = false;
@@ -996,9 +996,9 @@ fn buildOutputType(
996996
// if set, default the color setting to .off or .on, respectively
997997
// explicit --color arguments will still override this setting.
998998
// Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162
999-
var color: Color = if (native_os == .wasi or EnvVar.NO_COLOR.isSet())
999+
var color: Color = if (native_os == .wasi or try EnvVar.NO_COLOR.isSet(arena))
10001000
.off
1001-
else if (EnvVar.CLICOLOR_FORCE.isSet())
1001+
else if (try EnvVar.CLICOLOR_FORCE.isSet(arena))
10021002
.on
10031003
else
10041004
.auto;
@@ -4741,9 +4741,9 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
47414741
var reference_trace: ?u32 = null;
47424742
var debug_compile_errors = false;
47434743
var verbose_link = (native_os != .wasi or builtin.link_libc) and
4744-
EnvVar.ZIG_VERBOSE_LINK.isSet();
4744+
try EnvVar.ZIG_VERBOSE_LINK.isSet(arena);
47454745
var verbose_cc = (native_os != .wasi or builtin.link_libc) and
4746-
EnvVar.ZIG_VERBOSE_CC.isSet();
4746+
try EnvVar.ZIG_VERBOSE_CC.isSet(arena);
47474747
var verbose_air = false;
47484748
var verbose_intern_pool = false;
47494749
var verbose_generic_instances = false;
@@ -4926,7 +4926,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
49264926
}
49274927

49284928
const work_around_btrfs_bug = native_os == .linux and
4929-
EnvVar.ZIG_BTRFS_WORKAROUND.isSet();
4929+
try EnvVar.ZIG_BTRFS_WORKAROUND.isSet(arena);
49304930
const root_prog_node = std.Progress.start(.{
49314931
.disable_printing = (color == .off),
49324932
.root_name = "Compile Build Script",
@@ -5424,7 +5424,7 @@ fn jitCmd(
54245424
fatal("unable to find self exe path: {s}", .{@errorName(err)});
54255425
};
54265426

5427-
const optimize_mode: std.builtin.OptimizeMode = if (EnvVar.ZIG_DEBUG_CMD.isSet())
5427+
const optimize_mode: std.builtin.OptimizeMode = if (try EnvVar.ZIG_DEBUG_CMD.isSet(arena))
54285428
.Debug
54295429
else
54305430
.ReleaseFast;
@@ -6955,7 +6955,7 @@ fn cmdFetch(
69556955

69566956
const color: Color = .auto;
69576957
const work_around_btrfs_bug = native_os == .linux and
6958-
EnvVar.ZIG_BTRFS_WORKAROUND.isSet();
6958+
try EnvVar.ZIG_BTRFS_WORKAROUND.isSet(arena);
69596959
var opt_path_or_url: ?[]const u8 = null;
69606960
var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
69616961
var debug_hash: bool = false;

0 commit comments

Comments
 (0)