Reference a dependency without a build.zig.zon

Hello all ! This is my very first message here. I’m getting back to zig, I used to use it back in 2017 and slowly stopped touching it since early 2018. Eight years later, I want to have a shot back :slight_smile:

I try to get to use the zig build system for fun and profit and discovered the build.zig.zon file.

It seems cool to get to express your dependency this way, but I was wondering if I could do without it.

For testing, I have two folder, a pack/ folder, with just a root.zig and build.zig file, nothing fancy, root has a unique function and build just do addModule.

The second folder is a exec/ folder, with a main.zig file, a build.zig file, and a build.zig.zon file. I have a setup that works. Basically, in my build.zig I have something like:

    const pack = b.dependency("pack", .{});
    const pack_mod = pack.module("pack");
    mod.addImport("pack", pack_mod);

and in build.zig.zon:

    .dependencies = .{
        .pack = .{
            .path = "../pack",
        },
    }

Ok. But I wished I could just express, for my use case, my dependency directly inside my build.zig file. Something like:

    b.declareDependency("pack", .{ .path = "../pack" });
    const pack = b.dependency("pack", .{});
    const pack_mod = pack.module("pack");
    mod.addImport("pack", pack_mod);

Then the build.zig.zon would be useless in my case !

Is this possible ? Wanted ? Planned ?

Thanks a lot and have a nice day all !

Welcome back!

You’ll want dependencyFromBuildZig

const pack = b.dependencyFromBuildZig(@import("path/to/build.zig"),  .{});
const pack_mod = pack.module("pack");
mod.addImport("pack", pack_mod);

Damn, I saw this in the Build.zig file but couldn’t understand what it did with its funky argument build_zig. Thank you so much for providing a clear example !!

Hmm still not perfect.

If I now have:

    const pack = b.dependencyFromBuildZig(@import("../pack/build.zig"), .{});

I get the error:

build.zig:12:51: error: import of file outside module path
    const pack = b.dependencyFromBuildZig(@import("../pack/build.zig"), .{});

My bad, I thought your build.zig was in your projects root.

If you want multiple build files then you’ll have to go with your original approach using build.zig.zon.

Another way is to put your exec/build.zig build code in the root of the project. Then importing path is ./pack/build.zig

In general, it’s better to have one build.zig file in your project. If you have a lot of build code then splitting into functions in other files you import and call is one valid approach. All without using the Dependency type.

2 Likes

Yeah sorry maybe I wasn’t very clear and there was a lot of info-dump at the start :stuck_out_tongue:

In any case, I understand now. My use case is usually that I have a folder where I develop some small libs and then for some projects I just want to get the libs. I was just wondering how this would go with zig. I’m not fan of having a .zig.zon file each time I want to pull a dependency that is not within the direct path… but there are worst things in life.

Thanks for taking the time to help me !

Have a nice day.

1 Like