compiler: update std lib API usage

This commit is contained in:
Andrew Kelley 2026-01-01 19:40:18 -08:00
parent a6f519c20f
commit 960c512efd
14 changed files with 126 additions and 119 deletions

View file

@ -21,19 +21,12 @@ fn usage(io: Io) noreturn {
std.process.exit(1);
}
pub fn main() !void {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const gpa = init.gpa;
const io = init.io;
var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
const gpa = general_purpose_allocator.allocator();
var threaded: Io.Threaded = .init(gpa, .{});
defer threaded.deinit();
const io = threaded.io();
var argv = try std.process.argsWithAllocator(arena);
var argv = try init.minimal.args.iterateAllocator(arena);
defer argv.deinit();
assert(argv.skip());
const zig_lib_directory = argv.next().?;
@ -72,7 +65,7 @@ pub fn main() !void {
const url_with_newline = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});
Io.File.stdout().writeStreamingAll(io, url_with_newline) catch {};
if (should_open_browser) {
openBrowserTab(gpa, io, url_with_newline[0 .. url_with_newline.len - 1 :'\n']) catch |err| {
openBrowserTab(io, url_with_newline[0 .. url_with_newline.len - 1 :'\n']) catch |err| {
std.log.err("unable to open browser: {t}", .{err});
};
}
@ -324,11 +317,12 @@ fn buildWasmBinary(
"--listen=-", //
});
var child = std.process.Child.init(argv.items, gpa);
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
try child.spawn(io);
var child = try std.process.spawn(io, .{
.argv = argv.items,
.stdin = .pipe,
.stdout = .pipe,
.stderr = .pipe,
});
var poller = Io.poll(gpa, enum { stdout, stderr }, .{
.stdout = child.stdout.?,
@ -388,19 +382,26 @@ fn buildWasmBinary(
child.stdin = null;
switch (try child.wait(io)) {
.Exited => |code| {
.exited => |code| {
if (code != 0) {
std.log.err(
"the following command exited with error code {d}:\n{s}",
.{ code, try std.Build.Step.allocPrintCmd(arena, null, argv.items) },
.{ code, try std.Build.Step.allocPrintCmd(arena, null, null, argv.items) },
);
return error.WasmCompilationFailed;
}
},
.Signal, .Stopped, .Unknown => {
.signal => |sig| {
std.log.err(
"the following command terminated with signal {t}:\n{s}",
.{ sig, try std.Build.Step.allocPrintCmd(arena, null, null, argv.items) },
);
return error.WasmCompilationFailed;
},
.stopped, .unknown => {
std.log.err(
"the following command terminated unexpectedly:\n{s}",
.{try std.Build.Step.allocPrintCmd(arena, null, argv.items)},
.{try std.Build.Step.allocPrintCmd(arena, null, null, argv.items)},
);
return error.WasmCompilationFailed;
},
@ -410,14 +411,14 @@ fn buildWasmBinary(
try result_error_bundle.renderToStderr(io, .{}, .auto);
std.log.err("the following command failed with {d} compilation errors:\n{s}", .{
result_error_bundle.errorMessageCount(),
try std.Build.Step.allocPrintCmd(arena, null, argv.items),
try std.Build.Step.allocPrintCmd(arena, null, null, argv.items),
});
return error.WasmCompilationFailed;
}
return result orelse {
std.log.err("child process failed to report result\n{s}", .{
try std.Build.Step.allocPrintCmd(arena, null, argv.items),
try std.Build.Step.allocPrintCmd(arena, null, null, argv.items),
});
return error.WasmCompilationFailed;
};
@ -434,22 +435,24 @@ fn sendMessage(io: Io, file: Io.File, tag: std.zig.Client.Message.Tag) !void {
};
}
fn openBrowserTab(gpa: Allocator, io: Io, url: []const u8) !void {
fn openBrowserTab(io: Io, url: []const u8) !void {
// Until https://github.com/ziglang/zig/issues/19205 is implemented, we
// spawn a thread for this child process.
_ = try std.Thread.spawn(.{}, openBrowserTabThread, .{ gpa, io, url });
// spawn and then leak a concurrent task for this child process.
const future = try io.concurrent(openBrowserTabTask, .{ io, url });
_ = future; // leak it
}
fn openBrowserTabThread(gpa: Allocator, io: Io, url: []const u8) !void {
fn openBrowserTabTask(io: Io, url: []const u8) !void {
const main_exe = switch (builtin.os.tag) {
.windows => "explorer",
.macos => "open",
else => "xdg-open",
};
var child = std.process.Child.init(&.{ main_exe, url }, gpa);
child.stdin_behavior = .ignore;
child.stdout_behavior = .ignore;
child.stderr_behavior = .ignore;
try child.spawn(io);
var child = try std.process.spawn(io, .{
.argv = &.{ main_exe, url },
.stdin = .ignore,
.stdout = .ignore,
.stderr = .ignore,
});
_ = try child.wait(io);
}

View file

@ -9,19 +9,10 @@ const Translator = @import("Translator.zig");
const fast_exit = @import("builtin").mode != .Debug;
var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
pub fn main() u8 {
const gpa = general_purpose_allocator.allocator();
defer _ = general_purpose_allocator.deinit();
var arena_instance = std.heap.ArenaAllocator.init(gpa);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
var threaded: std.Io.Threaded = .init(gpa, .{});
defer threaded.deinit();
const io = threaded.io();
pub fn main(init: std.process.Init) u8 {
const gpa = init.gpa;
const arena = init.arena.allocator();
const io = init.io;
const args = process.argsAlloc(arena) catch {
std.debug.print("ran out of memory allocating arguments\n", .{});

View file

@ -742,6 +742,7 @@ pub const EnvVar = enum {
ZIG_IS_DETECTING_LIBC_PATHS,
ZIG_IS_TRYING_TO_NOT_CALL_ITSELF,
// C toolchain integration
NIX_CFLAGS_COMPILE,
NIX_CFLAGS_LINK,
NIX_LDFLAGS,
@ -750,13 +751,18 @@ pub const EnvVar = enum {
LIBRARY_PATH,
CC,
// Terminal integration
NO_COLOR,
CLICOLOR_FORCE,
// Debug info integration
XDG_CACHE_HOME,
LOCALAPPDATA,
HOME,
// Windows SDK integration
PROGRAMDATA,
pub fn isSet(ev: EnvVar, map: *const std.process.Environ.Map) bool {
return map.contains(@tagName(ev));
}

View file

@ -13,6 +13,7 @@ const fs = std.fs;
const Allocator = std.mem.Allocator;
const Path = std.Build.Cache.Path;
const log = std.log.scoped(.libc_installation);
const Environ = std.process.Environ;
include_dir: ?[]const u8 = null,
sys_include_dir: ?[]const u8 = null,
@ -167,7 +168,7 @@ pub fn render(self: LibCInstallation, out: *std.Io.Writer) !void {
pub const FindNativeOptions = struct {
target: *const std.Target,
env_map: *const std.process.Environ.Map,
env_map: *const Environ.Map,
/// If enabled, will print human-friendly errors to stderr.
verbose: bool = false,
@ -192,7 +193,7 @@ pub fn findNative(gpa: Allocator, io: Io, args: FindNativeOptions) FindError!Lib
});
return self;
} else if (is_windows) {
const sdk = std.zig.WindowsSdk.find(gpa, io, args.target.cpu.arch) catch |err| switch (err) {
const sdk = std.zig.WindowsSdk.find(gpa, io, args.target.cpu.arch, args.env_map) catch |err| switch (err) {
error.NotFound => return error.WindowsSdkNotFound,
error.PathTooLong => return error.WindowsSdkNotFound,
error.OutOfMemory => return error.OutOfMemory,
@ -552,7 +553,7 @@ fn findNativeMsvcLibDir(
}
pub const CCPrintFileNameOptions = struct {
env_map: *const std.process.Environ.Map,
env_map: *const Environ.Map,
search_basename: []const u8,
want_dirname: enum { full_path, only_dir },
verbose: bool = false,
@ -672,7 +673,7 @@ const inf_loop_env_key = "ZIG_IS_DETECTING_LIBC_PATHS";
fn appendCcExe(
args: *std.array_list.Managed([]const u8),
skip_cc_env_var: bool,
env_map: *const std.process.Environ.Map,
env_map: *const Environ.Map,
) !void {
const default_cc_exe = if (is_windows) "cc.exe" else "cc";
try args.ensureUnusedCapacity(1);

View file

@ -6,6 +6,7 @@ const Io = std.Io;
const Dir = std.Io.Dir;
const Writer = std.Io.Writer;
const Allocator = std.mem.Allocator;
const Environ = std.process.Environ;
windows10sdk: ?Installation,
windows81sdk: ?Installation,
@ -24,7 +25,12 @@ const product_version_max_length = version_major_minor_max_length + ".65535".len
/// Find path and version of Windows 10 SDK and Windows 8.1 SDK, and find path to MSVC's `lib/` directory.
/// Caller owns the result's fields.
/// Returns memory allocated by `gpa`
pub fn find(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemory, NotFound, PathTooLong }!WindowsSdk {
pub fn find(
gpa: Allocator,
io: Io,
arch: std.Target.Cpu.Arch,
env_map: *const Environ.Map,
) error{ OutOfMemory, NotFound, PathTooLong }!WindowsSdk {
if (builtin.os.tag != .windows) return error.NotFound;
//note(dimenus): If this key doesn't exist, neither the Win 8 SDK nor the Win 10 SDK is installed
@ -49,7 +55,7 @@ pub fn find(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemor
};
errdefer if (windows81sdk) |*w| w.free(gpa);
const msvc_lib_dir: ?[]const u8 = MsvcLibDir.find(gpa, io, arch) catch |err| switch (err) {
const msvc_lib_dir: ?[]const u8 = MsvcLibDir.find(gpa, io, arch, env_map) catch |err| switch (err) {
error.MsvcLibDirNotFound => null,
error.OutOfMemory => return error.OutOfMemory,
};
@ -671,7 +677,11 @@ const MsvcLibDir = struct {
return Dir.openDirAbsolute(io, instances_path, .{ .iterate = true }) catch return error.PathNotFound;
}
fn findInstancesDir(gpa: Allocator, io: Io) error{ OutOfMemory, PathNotFound }!Dir {
fn findInstancesDir(
gpa: Allocator,
io: Io,
env_map: *const Environ.Map,
) error{ OutOfMemory, PathNotFound }!Dir {
// First, try getting the packages cache path from the registry.
// This only seems to exist when the path is different from the default.
method1: {
@ -691,16 +701,13 @@ const MsvcLibDir = struct {
// If that can't be found, fall back to manually appending
// `Microsoft\VisualStudio\Packages\_Instances` to %PROGRAMDATA%
method3: {
const program_data = std.process.getEnvVarOwned(gpa, "PROGRAMDATA") catch |err| switch (err) {
error.OutOfMemory => |e| return e,
error.InvalidWtf8 => unreachable,
error.EnvironmentVariableNotFound => break :method3,
};
defer gpa.free(program_data);
const program_data = std.zig.EnvVar.PROGRAMDATA.get(env_map) orelse break :method3;
if (!Dir.path.isAbsolute(program_data)) break :method3;
const instances_path = try Dir.path.join(gpa, &.{ program_data, "Microsoft", "VisualStudio", "Packages", "_Instances" });
const instances_path = try Dir.path.join(gpa, &.{
program_data, "Microsoft", "VisualStudio", "Packages", "_Instances",
});
defer gpa.free(instances_path);
return Dir.openDirAbsolute(io, instances_path, .{ .iterate = true }) catch break :method3;
@ -754,12 +761,17 @@ const MsvcLibDir = struct {
///
/// The logic in this function is intended to match what ISetupConfiguration does
/// under-the-hood, as verified using Procmon.
fn findViaCOM(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemory, PathNotFound }![]const u8 {
fn findViaCOM(
gpa: Allocator,
io: Io,
arch: std.Target.Cpu.Arch,
env_map: *const Environ.Map,
) error{ OutOfMemory, PathNotFound }![]const u8 {
// Typically `%PROGRAMDATA%\Microsoft\VisualStudio\Packages\_Instances`
// This will contain directories with names of instance IDs like 80a758ca,
// which will contain `state.json` files that have the version and
// installation directory.
var instances_dir = try findInstancesDir(gpa, io);
var instances_dir = try findInstancesDir(gpa, io, env_map);
defer instances_dir.close(io);
var state_subpath_buf: [Dir.max_name_bytes + 32]u8 = undefined;
@ -856,15 +868,16 @@ const MsvcLibDir = struct {
}
// https://learn.microsoft.com/en-us/visualstudio/install/tools-for-managing-visual-studio-instances?view=vs-2022#editing-the-registry-for-a-visual-studio-instance
fn findViaRegistry(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemory, PathNotFound }![]const u8 {
fn findViaRegistry(
gpa: Allocator,
io: Io,
arch: std.Target.Cpu.Arch,
env_map: *const Environ.Map,
) error{ OutOfMemory, PathNotFound }![]const u8 {
// %localappdata%\Microsoft\VisualStudio\
// %appdata%\Local\Microsoft\VisualStudio\
const local_app_data_path = (std.zig.EnvVar.LOCALAPPDATA.get(gpa) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
error.InvalidWtf8 => return error.PathNotFound,
}) orelse return error.PathNotFound;
defer gpa.free(local_app_data_path);
const local_app_data_path = std.zig.EnvVar.LOCALAPPDATA.get(env_map) orelse return error.PathNotFound;
const visualstudio_folder_path = try Dir.path.join(gpa, &.{
local_app_data_path, "Microsoft\\VisualStudio\\",
});
@ -955,7 +968,7 @@ const MsvcLibDir = struct {
gpa: Allocator,
io: Io,
arch: std.Target.Cpu.Arch,
env_map: *const std.process.Environ.Map,
env_map: *const Environ.Map,
) error{ OutOfMemory, PathNotFound }![]const u8 {
var base_path: std.array_list.Managed(u8) = base_path: {
try_env: {
@ -1029,12 +1042,17 @@ const MsvcLibDir = struct {
/// Find path to MSVC's `lib/` directory.
/// Caller owns the result.
pub fn find(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemory, MsvcLibDirNotFound }![]const u8 {
const full_path = MsvcLibDir.findViaCOM(gpa, io, arch) catch |err1| switch (err1) {
pub fn find(
gpa: Allocator,
io: Io,
arch: std.Target.Cpu.Arch,
env_map: *const Environ.Map,
) error{ OutOfMemory, MsvcLibDirNotFound }![]const u8 {
const full_path = MsvcLibDir.findViaCOM(gpa, io, arch, env_map) catch |err1| switch (err1) {
error.OutOfMemory => return error.OutOfMemory,
error.PathNotFound => MsvcLibDir.findViaRegistry(gpa, io, arch) catch |err2| switch (err2) {
error.PathNotFound => MsvcLibDir.findViaRegistry(gpa, io, arch, env_map) catch |err2| switch (err2) {
error.OutOfMemory => return error.OutOfMemory,
error.PathNotFound => MsvcLibDir.findViaVs7Key(gpa, io, arch) catch |err3| switch (err3) {
error.PathNotFound => MsvcLibDir.findViaVs7Key(gpa, io, arch, env_map) catch |err3| switch (err3) {
error.OutOfMemory => return error.OutOfMemory,
error.PathNotFound => return error.MsvcLibDirNotFound,
},

View file

@ -2128,6 +2128,7 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic,
.manifest_dir = options.dirs.local_cache.handle.createDirPathOpen(io, "h", .{}) catch |err| {
return diag.fail(.{ .create_cache_path = .{ .which = .local, .sub = "h", .err = err } });
},
.cwd = options.dirs.cwd,
};
// These correspond to std.zig.Server.Message.PathPrefix.
cache.addPrefix(.{ .path = null, .handle = Io.Dir.cwd() });

View file

@ -2571,10 +2571,7 @@ fn newEmbedFile(
try whole.cache_manifest_mutex.lock(io);
defer whole.cache_manifest_mutex.unlock(io);
man.addFilePostContents(path_str, contents, new_file.stat) catch |err| switch (err) {
error.Unexpected => unreachable,
else => |e| return e,
};
try man.addFilePostContents(path_str, contents, new_file.stat);
}
return new_file;

View file

@ -6,6 +6,7 @@ const Dir = std.Io.Dir;
const mem = std.mem;
const Allocator = std.mem.Allocator;
const Cache = std.Build.Cache;
const assert = std.debug.assert;
const build_options = @import("build_options");
@ -62,14 +63,14 @@ pub fn getResolvedCwd(gpa: Allocator) error{
if (std.debug.runtime_safety) {
const cwd = try std.process.getCwdAlloc(gpa);
defer gpa.free(cwd);
std.debug.assert(mem.eql(u8, cwd, "."));
assert(mem.eql(u8, cwd, "."));
}
return "";
}
const cwd = try std.process.getCwdAlloc(gpa);
defer gpa.free(cwd);
const resolved = try Dir.path.resolve(gpa, &.{cwd});
std.debug.assert(Dir.path.isAbsolute(resolved));
assert(Dir.path.isAbsolute(resolved));
return resolved;
}
@ -140,7 +141,7 @@ pub fn resolvePath(
paths: []const []const u8,
) Allocator.Error![]u8 {
if (builtin.target.os.tag == .wasi) {
std.debug.assert(mem.eql(u8, cwd_resolved, ""));
assert(mem.eql(u8, cwd_resolved, ""));
const res = try Dir.path.resolve(gpa, paths);
if (mem.eql(u8, res, ".")) {
gpa.free(res);
@ -160,8 +161,8 @@ pub fn resolvePath(
gpa.free(res);
return "";
}
std.debug.assert(!Dir.path.isAbsolute(res));
std.debug.assert(!isUpDir(res));
assert(!Dir.path.isAbsolute(res));
assert(!isUpDir(res));
return res;
}
@ -180,8 +181,8 @@ pub fn resolvePath(
};
errdefer gpa.free(path_resolved);
std.debug.assert(Dir.path.isAbsolute(path_resolved));
std.debug.assert(Dir.path.isAbsolute(cwd_resolved));
assert(Dir.path.isAbsolute(path_resolved));
assert(Dir.path.isAbsolute(cwd_resolved));
if (!std.mem.startsWith(u8, path_resolved, cwd_resolved)) return path_resolved; // not in cwd
if (path_resolved.len == cwd_resolved.len) {

View file

@ -259,6 +259,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
.gpa = gpa,
.io = io,
.manifest_dir = try comp.dirs.global_cache.handle.createDirPathOpen(io, "h", .{}),
.cwd = comp.dirs.cwd,
};
cache.addPrefix(.{ .path = null, .handle = Io.Dir.cwd() });
cache.addPrefix(comp.dirs.zig_lib);

View file

@ -1,15 +1,15 @@
const std = @import("std");
const Io = std.Io;
pub fn main() !void {
pub fn main(init: std.process.Init.Minimal) !void {
// make sure safety checks are enabled even in release modes
var gpa_state = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
var gpa_state: std.heap.GeneralPurposeAllocator(.{ .safety = true }) = .{};
defer if (gpa_state.deinit() != .ok) {
@panic("found memory leaks");
};
const gpa = gpa_state.allocator();
var it = try std.process.argsWithAllocator(gpa);
var it = try init.iterateAllocator(gpa);
defer it.deinit();
_ = it.next() orelse unreachable; // skip binary name
const child_path, const needs_free = child_path: {
@ -21,7 +21,10 @@ pub fn main() !void {
};
defer if (needs_free) gpa.free(child_path);
var threaded: Io.Threaded = .init(gpa, .{});
var threaded: Io.Threaded = .init(gpa, .{
.argv0 = .init(init.args),
.environ = init.environ,
});
defer threaded.deinit();
const io = threaded.io();

View file

@ -2,16 +2,11 @@ const std = @import("std");
const builtin = @import("builtin");
// Note: the environment variables under test are set by the build.zig
pub fn main() !void {
pub fn main(init: std.process.Init) !void {
@setEvalBranchQuota(10000);
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var arena_state = std.heap.ArenaAllocator.init(allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const allocator = init.gpa;
const arena = init.arena.allocator();
// hasNonEmptyEnvVar
{

View file

@ -1,8 +1,8 @@
const std = @import("std");
pub fn main() !void {
const io = std.Io.Threaded.global_single_threaded.ioBasic();
var args = try std.process.argsWithAllocator(std.heap.page_allocator);
pub fn main(init: std.process.Init) !void {
const io = init.io;
var args = try init.args.iterateAllocator(init.arena.allocator());
_ = args.skip();
const dir_name = args.next().?;
const dir = try std.Io.Dir.cwd().openDir(io, if (std.mem.startsWith(u8, dir_name, "--dir="))

View file

@ -1,21 +1,17 @@
const std = @import("std");
pub fn main() anyerror!void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
defer if (gpa.deinit() == .leak) @panic("found memory leaks");
const allocator = gpa.allocator();
var it = try std.process.argsWithAllocator(allocator);
pub fn main(init: std.process.Init) !void {
const io = init.io;
const gpa = init.gpa;
var it = try init.args.iterateAllocator(gpa);
defer it.deinit();
_ = it.next() orelse unreachable; // skip binary name
const exe_path = it.next() orelse unreachable;
const symlink_path = it.next() orelse unreachable;
// If `exe_path` is relative to our cwd, we need to convert it to be relative to the dirname of `symlink_path`.
const exe_rel_path = try std.fs.path.relative(allocator, std.fs.path.dirname(symlink_path) orelse ".", exe_path);
defer allocator.free(exe_rel_path);
const io = std.Io.Threaded.global_single_threaded.ioBasic();
const exe_rel_path = try std.fs.path.relative(gpa, std.fs.path.dirname(symlink_path) orelse ".", exe_path);
defer gpa.free(exe_rel_path);
try std.Io.Dir.cwd().symLink(io, exe_rel_path, symlink_path, .{});
}

View file

@ -4,16 +4,10 @@ const mem = std.mem;
const warn = std.log.warn;
const fatal = std.process.fatal;
pub fn main() !void {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
var threaded: std.Io.Threaded = .init(arena, .{});
defer threaded.deinit();
const io = threaded.io();
const args = try std.process.argsAlloc(arena);
pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const io = init.io;
const args = try init.args.toSlice(arena);
const exe = args[0];
var catted_anything = false;