My print-debugging skills are not strong enough to understand why I’m getting `no module available` error even though I clearly adding import.
const wf = b.addWriteFiles();
const shader_info_file = wf.add("shader_info.generated.zig", shader_info_file_text.items);
const shader_info_module = b.createModule(.{ .root_source_file = shader_info_file, .target = target, .optimize = optimize });
for (zig_shader_modules.items) |zip| {
const name, const module = zip;
// defer b.allocator.free(name);
const exe_mod = b.createModule(.{ .root_source_file = b.path("src/shader_info_generator.zig"), .target = target, .optimize = optimize });
exe_mod.addImport("shader", module);
const exe = b.addExecutable(.{ .name = "shader_info_generator", .root_module = exe_mod });
const exe_run = b.addRunArtifact(exe);
const stdout = exe_run.captureStdOut();
const s_name = b.fmt("{s}.shader", .{name});
std.debug.print("adding module '{s}'\n", .{s_name});
const info_module = b.createModule(.{ .root_source_file = stdout, .target = target, .optimize = optimize });
shader_info_module.addImport(s_name, info_module);
}
for (shader_info_module.import_table.keys()) |k| {
std.debug.print("import: '{s}'\n", .{k});
}
for (modules) |module| { // `modules` here is my main exe module
module.addImport("shaders_info_generated", shader_info_module);
}
> zig build
adding module 'test.shader'
adding module 'textured_quad.shader'
import: 'test.shader'
import: 'textured_quad.shader'
install
└─ install zig_sdl3
└─ compile exe zig_sdl3 Debug native 1 errors
.zig-cache/o/9788381fafe38bea09f56d7e028d2e6c/shader_info.generated.zig:3:29: error: no module named 'test.shader' available within module 'shaders_info_generated'
pub const @"test" = @import("test.shader");
^~~~~~~~~~~~~
referenced by:
spirvShader__anon_22672: src/shader_helper.zig:8:18
main: src/main.zig:14:38
4 reference(s) hidden; use '-freference-trace=6' to see all references
P.S the whole thing is me trying to generate info for zig shaders - number of samplers, storage buffers, etc. In theory I could do it just by importing zig file and counting declaration, but I need to be able to get shader info by comptime name, but import only supports string literals. I tried to create a wrapper that simpy imports shader - one module for each shader, each with one import for one shader, and then generate file that import each wrapper (pub const @"test" = @import('test.shader')), but I got file exist in multiple module for the root file that imports each wrapper. So now i’m trying to hack it with this generator instead:
const std = @import("std");
/// shader - on of src/shaders/*.shader.zig files
const shader = @import("shader");
pub const vert = struct {
pub const num_samplers = @typeInfo(shader.vertex.samplers).@"struct".decls.len;
pub const num_storage_textures = @typeInfo(shader.vertex.storage_textures).@"struct".decls.len;
pub const num_storage_buffers = @typeInfo(shader.vertex.storage_buffers).@"struct".decls.len;
pub const num_uniform_buffers = @typeInfo(shader.vertex.uniform_buffers).@"struct".decls.len;
};
pub const frag = struct {
pub const num_samplers = @typeInfo(shader.fragment.samplers).@"struct".decls.len;
pub const num_storage_textures = @typeInfo(shader.fragment.storage_textures).@"struct".decls.len;
pub const num_storage_buffers = @typeInfo(shader.fragment.storage_buffers).@"struct".decls.len;
pub const num_uniform_buffers = @typeInfo(shader.fragment.uniform_buffers).@"struct".decls.len;
};
pub fn main() !void {
const stdout = std.fs.File.stdout();
var buffer: [1024]u8 = undefined;
var writer = stdout.writer(&buffer);
try writer.interface.print(
\\pub const vert = struct {{
\\ pub const num_samplers = {};
\\ pub const num_storage_textures = {};
\\ pub const num_storage_buffers = {};
\\ pub const num_uniform_buffers = {};
\\}};
\\
\\pub const frag = struct {{
\\ pub const num_samplers = {};
\\ pub const num_storage_textures = {};
\\ pub const num_storage_buffers = {};
\\ pub const num_uniform_buffers = {};
\\}};
, .{
vert.num_samplers,
vert.num_storage_textures,
vert.num_storage_buffers,
vert.num_uniform_buffers,
frag.num_samplers,
frag.num_storage_textures,
frag.num_storage_buffers,
frag.num_uniform_buffers,
});
}