commit ea044a4b829a03861823bac7bbef8e28e4c54af6 (tree)
parent 284ab0ad86310df45ecc3887cb6ed2f8cf507e45
Author: Techatrix <techatrix@mailbox.org>
Date: Wed, 27 May 2026 18:54:08 +0200
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`.
Diffstat:
1 file changed, 33 insertions(+), 3 deletions(-)
diff --git a/lib/std/Build.zig b/lib/std/Build.zig
@@ -830,7 +830,17 @@ pub fn addModule(b: *Build, name: []const u8, options: Module.CreateOptions) *Mo
const graph = b.graph;
const arena = graph.arena;
const module = Module.create(b, options);
- b.modules.put(arena, graph.dupeString(name), module) catch @panic("OOM");
+ const gop = b.modules.getOrPutValue(
+ arena,
+ graph.dupeString(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;
}
@@ -992,13 +1002,33 @@ 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 graph = b.graph;
const wf = Step.WriteFile.create(b);
- b.named_writefiles.put(graph.arena, graph.dupeString(name), wf) catch @panic("OOM");
+ const gop = b.named_writefiles.getOrPutValue(
+ graph.arena,
+ graph.dupeString(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 {
const graph = b.graph;
- b.named_lazy_paths.put(graph.arena, graph.dupeString(name), lp.dupe(graph)) catch @panic("OOM");
+ const gop = b.named_lazy_paths.getOrPutValue(
+ graph.arena,
+ graph.dupeString(name),
+ lp.dupe(graph),
+ ) catch @panic("OOM");
+ if (gop.found_existing) {
+ panic(
+ "A LazyPath with the name '{s}' has already been added to the package.",
+ .{name},
+ );
+ }
}
/// Creates a step for mutating files inside a temporary directory created lazily