Skip to content

Commit 5743f1a

Browse files
committed
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`.
1 parent 4fcdb08 commit 5743f1a

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

lib/std/Build.zig

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,13 @@ pub const AssemblyOptions = struct {
906906
/// `createModule` can be used instead to create a private module.
907907
pub fn addModule(b: *Build, name: []const u8, options: Module.CreateOptions) *Module {
908908
const module = Module.create(b, options);
909-
b.modules.put(b.dupe(name), module) catch @panic("OOM");
909+
const gop = b.modules.getOrPutValue(b.dupe(name), module) catch @panic("OOM");
910+
if (gop.found_existing) {
911+
panic(
912+
"A module with the name '{s}' has already been added to the package. Consider creating a private module with std.Build.createModule",
913+
.{name},
914+
);
915+
}
910916
return module;
911917
}
912918

@@ -1025,12 +1031,24 @@ pub fn addWriteFile(b: *Build, file_path: []const u8, data: []const u8) *Step.Wr
10251031

10261032
pub fn addNamedWriteFiles(b: *Build, name: []const u8) *Step.WriteFile {
10271033
const wf = Step.WriteFile.create(b);
1028-
b.named_writefiles.put(b.dupe(name), wf) catch @panic("OOM");
1034+
const gop = b.named_writefiles.getOrPutValue(b.dupe(name), wf) catch @panic("OOM");
1035+
if (gop.found_existing) {
1036+
panic(
1037+
"A WriteFile step with the name '{s}' has already been added to the package. Consider creating a private WriteFile step with std.Build.addWriteFiles",
1038+
.{name},
1039+
);
1040+
}
10291041
return wf;
10301042
}
10311043

10321044
pub fn addNamedLazyPath(b: *Build, name: []const u8, lp: LazyPath) void {
1033-
b.named_lazy_paths.put(b.dupe(name), lp.dupe(b)) catch @panic("OOM");
1045+
const gop = b.named_lazy_paths.getOrPutValue(b.dupe(name), lp) catch @panic("OOM");
1046+
if (gop.found_existing) {
1047+
panic(
1048+
"A LazyPath with the name '{s}' has already been added to the package.",
1049+
.{name},
1050+
);
1051+
}
10341052
}
10351053

10361054
pub fn addWriteFiles(b: *Build) *Step.WriteFile {

0 commit comments

Comments
 (0)