How to import from the .zon file

Hi all, I’ve spent some time now figuring out the proper way to use the dependencies declared in the build.zig.zon file
I’ve been trying to import either capy or raylib without success.
I know Capy relies on zigmod, and raylib expects to be a git submodule, but I’d like to use the .zon file.

my build.zig.zon

.{
	.version = "0.0.0",
	.name = "test",
	.dependencies = .{
		.capy = .{
			.url = "https://github.com/capy-ui/capy/archive/refs/heads/master.tar.gz",
			.hash = "1220836cd761d0afd2c837a59e806d4b63c0d73ea6868a6f3e66485a1d9fa9c067bb"
		}	
	},
}

I’m doing what zls project does to add dependencies

    const capy = b.dependency("capy", .{}).module("capy");
    exe.addModule("capy", capy);

after changing this line in the downloaded capy cache

var examplesDir = try std.fs.cwd().openIterableDir("examples", .{});

to

var examplesDir = try b.build_root.handle.openIterableDir("examples", .{});

I get

panic("unable to find module '{s}'", .{name});
    const capy = b.dependency("capy", .{}).module("capy");
                                                 ^

I tried looking at sources, but I really can’t figure out how the build system works…

Thanks in advance :slight_smile:

1 Like

The error is because the module has not been exposed. One possible solution is that adding following lines in the capy’s build.zig file.

    const mod = b.addModule("capy", .{
        .source_file = .{ .path = "src/main.zig" },
    });

And add following lines in your project’s build.zig.

    const capy_artifact = b.dependency("capy", .{}).artifact("capy");
    const capy_module = b.dependency("capy", .{}).module("capy");
    exe.linkLibrary(capy_artifact);
    exe.addModule("capy", capy_module);

I don’t recommend making modifications like this, as it changes the code of download cache. I noticed that capy has a template that you can try using instead.

3 Likes

Thanks, in fact, editing the cache, not only is it a bad idea, but it turned out to be completely unnecessary.
Capy build file exposes the install function that does all the .b.addModule("capy")... stuff, and adds appropriate dependencies.

Thanks for pointing out the b.addModule, I wouldn’t have known what to look for otherwise :slight_smile: