From 5743f1a17c88f6f8b6956929706234e04e359d65 Mon Sep 17 00:00:00 2001 From: Techatrix Date: Sat, 16 Aug 2025 20:50:49 +0200 Subject: [PATCH] std.Build: do not override properties of a package like named modules The `addModule` function could override the module that is visible to dependants which most likely unintentional. Instead should asserts that no existing module has been added with the given name. The same issue applies to `addNamedWriteFiles` and `addNamedLazyPath`. --- lib/std/Build.zig | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 548f95199192..a37304df11b9 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -906,7 +906,13 @@ pub const AssemblyOptions = struct { /// `createModule` can be used instead to create a private module. pub fn addModule(b: *Build, name: []const u8, options: Module.CreateOptions) *Module { const module = Module.create(b, options); - b.modules.put(b.dupe(name), module) catch @panic("OOM"); + const gop = b.modules.getOrPutValue(b.dupe(name), module) catch @panic("OOM"); + if (gop.found_existing) { + panic( + "A module with the name '{s}' has already been added to the package. Consider creating a private module with std.Build.createModule", + .{name}, + ); + } return module; } @@ -1025,12 +1031,24 @@ pub fn addWriteFile(b: *Build, file_path: []const u8, data: []const u8) *Step.Wr pub fn addNamedWriteFiles(b: *Build, name: []const u8) *Step.WriteFile { const wf = Step.WriteFile.create(b); - b.named_writefiles.put(b.dupe(name), wf) catch @panic("OOM"); + const gop = b.named_writefiles.getOrPutValue(b.dupe(name), wf) catch @panic("OOM"); + if (gop.found_existing) { + panic( + "A WriteFile step with the name '{s}' has already been added to the package. Consider creating a private WriteFile step with std.Build.addWriteFiles", + .{name}, + ); + } return wf; } pub fn addNamedLazyPath(b: *Build, name: []const u8, lp: LazyPath) void { - b.named_lazy_paths.put(b.dupe(name), lp.dupe(b)) catch @panic("OOM"); + const gop = b.named_lazy_paths.getOrPutValue(b.dupe(name), lp) catch @panic("OOM"); + if (gop.found_existing) { + panic( + "A LazyPath with the name '{s}' has already been added to the package.", + .{name}, + ); + } } pub fn addWriteFiles(b: *Build) *Step.WriteFile {