authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2024-07-30 18:41:22+10:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-26 14:48:04+01:00
logb84db311d90d722c206a7eea5464f46fd1b9e827
treea12db400890f30971815fa400f17ac04854c8936
parent5b4759bd3c860b0648bf847ba9aa52584a9547d9

zig build: add env_map entries to hash for Step.Run

This change fixes false-positive cache hits for run steps that get run with different sets of environment variables due the the environment map being excluded from the cache hash.

1 files changed, 29 insertions(+), 0 deletions(-)

lib/std/Build/Step/Run.zig+29
...@@ -620,6 +620,35 @@ fn make(step: *Step, options: Step.MakeOptions) !void {...@@ -620,6 +620,35 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
620 var man = b.graph.cache.obtain();620 var man = b.graph.cache.obtain();
621 defer man.deinit();621 defer man.deinit();
622622
623 if (run.env_map) |env_map| {
624 const KV = struct { []const u8, []const u8 };
625 var kv_pairs = try std.ArrayList(KV).initCapacity(arena, env_map.count());
626 var iter = env_map.iterator();
627 while (iter.next()) |entry| {
628 kv_pairs.appendAssumeCapacity(.{ entry.key_ptr.*, entry.value_ptr.* });
629 }
630
631 std.mem.sortUnstable(KV, kv_pairs.items, {}, struct {
632 fn lessThan(_: void, kv1: KV, kv2: KV) bool {
633 const k1 = kv1[0];
634 const k2 = kv2[0];
635
636 if (k1.len != k2.len) return k1.len < k2.len;
637
638 for (k1, k2) |c1, c2| {
639 if (c1 == c2) continue;
640 return c1 < c2;
641 }
642 unreachable; // two keys cannot be equal
643 }
644 }.lessThan);
645
646 for (kv_pairs.items) |kv| {
647 man.hash.addBytes(kv[0]);
648 man.hash.addBytes(kv[1]);
649 }
650 }
651
623 for (run.argv.items) |arg| {652 for (run.argv.items) |arg| {
624 switch (arg) {653 switch (arg) {
625 .bytes => |bytes| {654 .bytes => |bytes| {