Hello.
I’m trying to use @import("root")
to access modules, as Zig’s standard library and Bun are doing (actually, I always refer to them as the best example ). My project has below structure:
.
├── build.zig
├── tool.zig
├── root.zig
└── src
├── main.zig
├── me.zig
└── mod_a.zig
me.zig
is a kind of utility to expose mod_a.zig
(and other more modules in an actual project):
pub const mod_a = @import("mod_a.zig");
mod_a.zig
is a module which defines one function:
pub fn mod_a_fn() void {
@import("std").log.info("hello mod_a", .{});
}
main.zig
implements a main function that calls mod_a_fn
:
const me = @import("root").me;
pub fn main() !void {
me.mod_a.mod_a_fn();
}
root.zig
is a root_source_file
of the executable and exposes main.zig
with usingnamespace
and me.zig
with @import
:
pub usingnamespace @import("src/main.zig");
pub const me = @import("src/me.zig");
When I build them, it can compile and ZLS works completely fine, showing type information of mod_a_fn
.
However, suppose I have one more executable(tool.zig
) to build that is used as a tool. If I added it in build.zig
by b.installArtifact()
, ZLS suddenly stops showing the type information of @import("root")
while it still can compile without errors.
I guess that b.addExecutable
is doing things unexpectedly. Could someone explain why ZLS does not work when I installed more than two executables?
Here’s my build.zig
:
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const tool = b.addExecutable(.{
.name = "tool",
.root_source_file = b.path("tool.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(tool);
const exe = b.addExecutable(.{
.name = "zig-test",
.root_source_file = .{ .path = "root.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);