authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2026-05-27 18:54:08+02:00
committergravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2026-05-27 18:54:08+02:00
logea044a4b829a03861823bac7bbef8e28e4c54af6
tree8d4d960847ad5cab266dcd4784e6f83ee4767275
parent284ab0ad86310df45ecc3887cb6ed2f8cf507e45
signaturebadge-check Signed by SSH key SHA256:HYC3SjXQcAt6uwv9pu/6OoVQ2rUH8rb5zKiUHSe9uxk

std.Build: do not clobber properties of a package like named modules

Repeated calls to `addModule` with the same name will override the module that is visible to dependants which is likely unintentional. Instead it should assert that no existing module has already been added with the given name. The same issue applies to `addNamedWriteFiles` and `addNamedLazyPath`.

1 files changed, 33 insertions(+), 3 deletions(-)

lib/std/Build.zig+33-3
......@@ -830,7 +830,17 @@ pub fn addModule(b: *Build, name: []const u8, options: Module.CreateOptions) *Mo
830830 const graph = b.graph;
831831 const arena = graph.arena;
832832 const module = Module.create(b, options);
833 b.modules.put(arena, graph.dupeString(name), module) catch @panic("OOM");
833 const gop = b.modules.getOrPutValue(
834 arena,
835 graph.dupeString(name),
836 module,
837 ) catch @panic("OOM");
838 if (gop.found_existing) {
839 panic(
840 "A module with the name '{s}' has already been added to the package. Consider creating a private module with std.Build.createModule",
841 .{name},
842 );
843 }
834844 return module;
835845}
836846
......@@ -992,13 +1002,33 @@ pub fn addWriteFile(b: *Build, file_path: []const u8, data: []const u8) *Step.Wr
9921002pub fn addNamedWriteFiles(b: *Build, name: []const u8) *Step.WriteFile {
9931003 const graph = b.graph;
9941004 const wf = Step.WriteFile.create(b);
995 b.named_writefiles.put(graph.arena, graph.dupeString(name), wf) catch @panic("OOM");
1005 const gop = b.named_writefiles.getOrPutValue(
1006 graph.arena,
1007 graph.dupeString(name),
1008 wf,
1009 ) catch @panic("OOM");
1010 if (gop.found_existing) {
1011 panic(
1012 "A WriteFile step with the name '{s}' has already been added to the package. Consider creating a private WriteFile step with std.Build.addWriteFiles",
1013 .{name},
1014 );
1015 }
9961016 return wf;
9971017}
9981018
9991019pub fn addNamedLazyPath(b: *Build, name: []const u8, lp: LazyPath) void {
10001020 const graph = b.graph;
1001 b.named_lazy_paths.put(graph.arena, graph.dupeString(name), lp.dupe(graph)) catch @panic("OOM");
1021 const gop = b.named_lazy_paths.getOrPutValue(
1022 graph.arena,
1023 graph.dupeString(name),
1024 lp.dupe(graph),
1025 ) catch @panic("OOM");
1026 if (gop.found_existing) {
1027 panic(
1028 "A LazyPath with the name '{s}' has already been added to the package.",
1029 .{name},
1030 );
1031 }
10021032}
10031033
10041034/// Creates a step for mutating files inside a temporary directory created lazily