From 1042cbe0100ba0fbd8265f975c49356d31e4bd91 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sun, 7 Jun 2026 20:58:45 -0700 Subject: [PATCH] Build: fix tryFindProgram on Windows and reintroduce ref in test block Also reduce the amount of unnecessary allocations, since one buffer can be used for appending all supported PATHEXT extensions Fixes #35668 --- lib/std/Build.zig | 7 ++++++- lib/std/process.zig | 10 +++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index ec552b98ed2bed478aef81c77c0549a12d6075aa..798729533c70421c8c5126a22540341f3129091d 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1836,10 +1836,14 @@ fn tryFindProgram(b: *Build, full_path: []const u8) ?[]const u8 { if (b.graph.environ_map.get("PATHEXT")) |PATHEXT| { var it = mem.tokenizeScalar(u8, PATHEXT, fs.path.delimiter); + const extended_path_buf = arena.alloc(u8, full_path.len + 1 + std.process.WindowsExtension.max_len) catch @panic("OOM"); + @memcpy(extended_path_buf[0..full_path.len], full_path); + while (it.next()) |ext| { if (!supportedWindowsProgramExtension(ext)) continue; - const extended_path = try mem.concat(arena, u8, &.{ full_path, ext }); + @memcpy(extended_path_buf[full_path.len..][0..ext.len], ext); + const extended_path = extended_path_buf[0 .. full_path.len + ext.len]; if (Io.Dir.cwd().access(io, extended_path, .{ .execute = true })) |_| { return extended_path; @@ -2708,4 +2712,5 @@ test { _ = Cache; _ = Step; _ = Configuration; + _ = &findProgram; } diff --git a/lib/std/process.zig b/lib/std/process.zig index bcee558a65eef07afab17b484cc012326bb5004c..f3ef291763d642426770883e886c8a5a50aeae41 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -315,7 +315,15 @@ pub fn replacePath(io: Io, dir: Io.Dir, options: ReplaceOptions) ReplaceError { pub const ArgExpansion = enum { expand, no_expand }; /// File name extensions supported natively by `CreateProcess()` on Windows. -pub const WindowsExtension = enum { bat, cmd, com, exe }; +pub const WindowsExtension = enum { + bat, + cmd, + com, + exe, + + /// Length of the longest supported extension (in ASCII characters) + pub const max_len = 3; +}; pub const SpawnError = error{ /// The operating system does not support creating child processes. -- 2.54.0