authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-05 14:17:44-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-07 11:03:36-08:00
loge3e9c7c33c029368ad644619ed20f3d7cccd3a47
tree4f4939db9c21513ab523ee8f6f11ea735a2ab93d
parent2f639a45b401998a113174829839110ba8970dfa

std.Build.Step.Compile: take advantage of std lib atomic files


1 files changed, 19 insertions(+), 8 deletions(-)

lib/std/Build/Step/Compile.zig+19-8
......@@ -1706,18 +1706,29 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
17061706 // The args file is already present from a previous run.
17071707 } else |err| switch (err) {
17081708 error.FileNotFound => {
1709 try b.cache_root.handle.createDirPath(io, "tmp");
1710 const rand_int = std.crypto.random.int(u64);
1711 const tmp_path = "tmp" ++ fs.path.sep_str ++ std.fmt.hex(rand_int);
1712 try b.cache_root.handle.writeFile(io, .{ .sub_path = tmp_path, .data = args });
1713 defer b.cache_root.handle.deleteFile(io, tmp_path) catch {
1714 // It's fine if the temporary file can't be cleaned up.
1709 var af = b.cache_root.handle.createFileAtomic(io, args_file, .{
1710 .replace = false,
1711 .make_path = true,
1712 }) catch |e| return step.fail("failed creating tmp args file {f}{s}: {t}", .{
1713 b.cache_root, args_file, e,
1714 });
1715 defer af.deinit(io);
1716
1717 af.file.writeStreamingAll(io, args) catch |e| {
1718 return step.fail("failed writing args data to tmp file {f}{s}: {t}", .{
1719 b.cache_root, args_file, e,
1720 });
17151721 };
1716 b.cache_root.handle.rename(tmp_path, b.cache_root.handle, args_file, io) catch |rename_err| switch (rename_err) {
1722 // Note we can't clean up this file, not even after build
1723 // success, because that might interfere with another build
1724 // process that needs the same file.
1725 af.link(io) catch |e| switch (e) {
17171726 error.PathAlreadyExists => {
17181727 // The args file was created by another concurrent build process.
17191728 },
1720 else => |other_err| return other_err,
1729 else => |other_err| return step.fail("failed linking tmp file {f}{s}: {t}", .{
1730 b.cache_root, args_file, other_err,
1731 }),
17211732 };
17221733 },
17231734 else => |other_err| return other_err,