I have a small module that exports functionality which is used in the importing project’s build script. The module uses anonymous imports which are passed to @embedFile
. The intended purpose in the original project is changing a string used by the program based on build options. Running zig build
in the module’s own root runs fine, but the anonymous imports are not found when the module is used as a dependency. I’ve made a toy example that reproduces the behaviour. Am I misunderstanding something?
The importing project’s build.zig
:
const std = @import("std");
const example_module = @import("example_module");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
_ = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
example_module.printFoo();
}
build.zig.zon
of importing project:
.{
.name = .minimal_anon_import_repro,
.version = "0.0.0",
.fingerprint = 0xac8d4bd961888d11,
.minimum_zig_version = "0.14.1",
.dependencies = .{
.example_module = .{
.path = "./example_module/"
}
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
}
build.zig
of module:
const std = @import("std");
pub const printFoo = @import("src/root.zig").printFoo;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const root_mod = b.addModule("example_module", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
root_mod.addAnonymousImport("foo", .{ .root_source_file = b.path("foo.txt") } });
}
build.zig.zon
of module:
.{
.name = .example_module,
.version = "0.0.0",
.fingerprint = 0xadeb8b104ce18ea0,
.minimum_zig_version = "0.14.1",
.dependencies = .{
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"foo.txt"
},
}
root.zig
of module:
const text = @embedFile("foo");
const std = @import("std");
pub fn printFoo() void {
std.log.debug(text, .{});
}
The directory of the project looks like this:
├── build.zig
├── build.zig.zon
├── example_module
│ ├── build.zig
│ ├── build.zig.zon
│ ├── foo.txt
│ └── src
│ └── root.zig
└── src
└── root.zig
zig build
gives me the error:
minimal-anon-import-repro/example_module/src/root.zig:1:25: error: unable to open 'foo': FileNotFound
const text = @embedFile("foo");
^~~~~