Skip to content

Commit dc282df

Browse files
iceghostTechatrix
authored andcommitted
collect module imports of public modules
Currently, zls only collects module imports of Step.Compile. For public modules, it only collect the root_modules, not their module imports. This commit makes the build_runner also collects the module imports and also adds one regression test case when one does `addOptions` on a public module, which is my original motivation (and confusion why my @import("options") was not working). The generated options.zig file is generated in the c/ instead of o/ namespace, so I also removed the namespace assertion. I assume the original assertion is only for reminding reader of what that component is, and not an actual useful property of the path, so I keep the intent as a comment.
1 parent b9eb9c4 commit dc282df

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

src/build_runner/build_runner.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,9 +1078,11 @@ fn extractBuildInformation(
10781078
var modules: std.array_hash_map.Auto(*std.Build.Module, void) = .empty;
10791079
defer modules.deinit(gpa);
10801080

1081-
try modules.ensureUnusedCapacity(gpa, b.modules.count());
1081+
// collect all modules of root modules
10821082
for (b.modules.values()) |root_module| {
1083-
modules.putAssumeCapacity(root_module, {});
1083+
const graph = root_module.getGraph();
1084+
try modules.ensureUnusedCapacity(gpa, graph.modules.len);
1085+
for (graph.modules) |module| modules.putAssumeCapacity(module, {});
10841086
}
10851087

10861088
// collect all modules of `Step.Compile`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"dependencies": {},
3+
"modules": {
4+
"root.zig": {
5+
"import_table": {
6+
"options": ".zig-local-cache/options.zig"
7+
}
8+
},
9+
".zig-local-cache/options.zig": {
10+
"import_table": {}
11+
}
12+
},
13+
"compilations": [],
14+
"top_level_steps": [
15+
"install",
16+
"uninstall"
17+
],
18+
"available_options": {}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const module = b.addModule("root", .{
5+
.root_source_file = b.path("root.zig"),
6+
});
7+
8+
const options = b.addOptions();
9+
options.addOption(bool, "enabled", true);
10+
module.addOptions("options", options);
11+
}

tests/build_runner_check.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ fn sanitizePath(
8888
}
8989
if (stripBasePath(local_cache_dir, path)) |to| {
9090
var it = std.Io.Dir.path.componentIterator(to);
91-
std.debug.assert(std.mem.eql(u8, it.next().?.name, "o"));
91+
_ = it.next().?; // skip cache namespace (o, c, h, z, tmp, ...)
9292
std.debug.assert(it.next().?.name.len == std.Build.Cache.hex_digest_len);
9393
break :new try std.fmt.allocPrint(arena, ".zig-local-cache/{s}", .{to[it.end_index + 1 ..]});
9494
}
9595
if (stripBasePath(global_cache_dir, path)) |to| {
9696
var it = std.Io.Dir.path.componentIterator(to);
97-
std.debug.assert(std.mem.eql(u8, it.next().?.name, "o"));
97+
_ = it.next().?; // skip cache namespace (o, c, h, z, tmp, ...)
9898
std.debug.assert(it.next().?.name.len == std.Build.Cache.hex_digest_len);
9999
break :new try std.fmt.allocPrint(arena, ".zig-global-cache/{s}", .{to[it.end_index + 1 ..]});
100100
}

0 commit comments

Comments
 (0)