main: classify empty environment variables as unset

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

Closes #22380.
This commit is contained in:
Carter Snook 2025-01-27 10:53:32 -06:00 committed by GitHub
parent 5647dd84c1
commit 2043e8ae05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 13 deletions

View File

@ -700,10 +700,6 @@ pub const EnvVar = enum {
XDG_CACHE_HOME, XDG_CACHE_HOME,
HOME, HOME,
pub fn isSet(comptime ev: EnvVar) bool {
return std.process.hasEnvVarConstant(@tagName(ev));
}
pub fn get(ev: EnvVar, arena: std.mem.Allocator) !?[]u8 { pub fn get(ev: EnvVar, arena: std.mem.Allocator) !?[]u8 {
if (std.process.getEnvVarOwned(arena, @tagName(ev))) |value| { if (std.process.getEnvVarOwned(arena, @tagName(ev))) |value| {
return value; return value;
@ -716,6 +712,11 @@ pub const EnvVar = enum {
pub fn getPosix(comptime ev: EnvVar) ?[:0]const u8 { pub fn getPosix(comptime ev: EnvVar) ?[:0]const u8 {
return std.posix.getenvZ(@tagName(ev)); return std.posix.getenvZ(@tagName(ev));
} }
pub fn isSet(ev: EnvVar, arena: std.mem.Allocator) !bool {
const value = try ev.get(arena) orelse return false;
return value.len != 0;
}
}; };
pub const SimpleComptimeReason = enum(u32) { pub const SimpleComptimeReason = enum(u32) {

View File

@ -826,9 +826,9 @@ fn buildOutputType(
var listen: Listen = .none; var listen: Listen = .none;
var debug_compile_errors = false; var debug_compile_errors = false;
var verbose_link = (native_os != .wasi or builtin.link_libc) and var verbose_link = (native_os != .wasi or builtin.link_libc) and
EnvVar.ZIG_VERBOSE_LINK.isSet(); try EnvVar.ZIG_VERBOSE_LINK.isSet(arena);
var verbose_cc = (native_os != .wasi or builtin.link_libc) and var verbose_cc = (native_os != .wasi or builtin.link_libc) and
EnvVar.ZIG_VERBOSE_CC.isSet(); try EnvVar.ZIG_VERBOSE_CC.isSet(arena);
var verbose_air = false; var verbose_air = false;
var verbose_intern_pool = false; var verbose_intern_pool = false;
var verbose_generic_instances = false; var verbose_generic_instances = false;
@ -1006,9 +1006,9 @@ fn buildOutputType(
// if set, default the color setting to .off or .on, respectively // if set, default the color setting to .off or .on, respectively
// explicit --color arguments will still override this setting. // explicit --color arguments will still override this setting.
// Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162 // Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162
var color: Color = if (native_os == .wasi or EnvVar.NO_COLOR.isSet()) var color: Color = if (native_os == .wasi or try EnvVar.NO_COLOR.isSet(arena))
.off .off
else if (EnvVar.CLICOLOR_FORCE.isSet()) else if (try EnvVar.CLICOLOR_FORCE.isSet(arena))
.on .on
else else
.auto; .auto;
@ -4749,9 +4749,9 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
var reference_trace: ?u32 = null; var reference_trace: ?u32 = null;
var debug_compile_errors = false; var debug_compile_errors = false;
var verbose_link = (native_os != .wasi or builtin.link_libc) and var verbose_link = (native_os != .wasi or builtin.link_libc) and
EnvVar.ZIG_VERBOSE_LINK.isSet(); try EnvVar.ZIG_VERBOSE_LINK.isSet(arena);
var verbose_cc = (native_os != .wasi or builtin.link_libc) and var verbose_cc = (native_os != .wasi or builtin.link_libc) and
EnvVar.ZIG_VERBOSE_CC.isSet(); try EnvVar.ZIG_VERBOSE_CC.isSet(arena);
var verbose_air = false; var verbose_air = false;
var verbose_intern_pool = false; var verbose_intern_pool = false;
var verbose_generic_instances = false; var verbose_generic_instances = false;
@ -4934,7 +4934,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
} }
const work_around_btrfs_bug = native_os == .linux and const work_around_btrfs_bug = native_os == .linux and
EnvVar.ZIG_BTRFS_WORKAROUND.isSet(); try EnvVar.ZIG_BTRFS_WORKAROUND.isSet(arena);
const root_prog_node = std.Progress.start(.{ const root_prog_node = std.Progress.start(.{
.disable_printing = (color == .off), .disable_printing = (color == .off),
.root_name = "Compile Build Script", .root_name = "Compile Build Script",
@ -5433,7 +5433,7 @@ fn jitCmd(
fatal("unable to find self exe path: {s}", .{@errorName(err)}); fatal("unable to find self exe path: {s}", .{@errorName(err)});
}; };
const optimize_mode: std.builtin.OptimizeMode = if (EnvVar.ZIG_DEBUG_CMD.isSet()) const optimize_mode: std.builtin.OptimizeMode = if (try EnvVar.ZIG_DEBUG_CMD.isSet(arena))
.Debug .Debug
else else
.ReleaseFast; .ReleaseFast;
@ -6965,7 +6965,7 @@ fn cmdFetch(
const color: Color = .auto; const color: Color = .auto;
const work_around_btrfs_bug = native_os == .linux and const work_around_btrfs_bug = native_os == .linux and
EnvVar.ZIG_BTRFS_WORKAROUND.isSet(); try EnvVar.ZIG_BTRFS_WORKAROUND.isSet(arena);
var opt_path_or_url: ?[]const u8 = null; var opt_path_or_url: ?[]const u8 = null;
var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena); var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
var debug_hash: bool = false; var debug_hash: bool = false;