0

how to properly add an import on a module that is already a dependency of the imported module, I do it like this:

    const tkm = b.dependency("tokamak", .{
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addImport("tokamak", tkm.module("tokamak"));

    var it = tkm.module("tokamak").iterateDependencies(null, false);

    while (it.next())|dep| {
        if(std.mem.eql(u8, dep.name, "httpz")) {
            exe.root_module.addImport("httpz", dep.module);
        }
    }

maybe there is a more elegant way?

Просто хочу знать можно ли сделать это лучше

3
  • using your example, your module depends on 'tokamak' and 'httpz', while 'tokamak' also depends on 'httpz' - is that correct? Commented Oct 10, 2024 at 15:13
  • Yes, that's how I want it Commented Oct 10, 2024 at 15:49
  • are you experiencing any issues if you add 'tokamak' and 'httpz' as dependencies to your project, "normally" via build.zig and build.zig.zon? I mean, any project should be able to specify its own dependencies, no? For example your project might even depend on another version of 'httpz' than 'tokamak' does (to be specified in each project's build.zig.zon). Commented Oct 11, 2024 at 8:11

1 Answer 1

0

It is planned for projects to have IDs and versions to handle this better in the future: https://github.com/ziglang/zig/issues/14288

For now, you can add the dependency to your build.zig.zon with the exact same url and hash and call b.dependency() on it with the exact same arguments and it will resolve to the same dependency:

In your build.zig.zon:

    .dependencies = .{
        .tokamak = .{ ... },
        .httpz = .{
            // make sure this url and hash are the same as the one in tokmak's build.zig.zon, and update this when you update tokmak
            .url = "git+https://github.com/karlseguin/http.zig?ref=master#6e45041ef1e0ed8d3e36e15ccb459a3392571f7f",
            .hash = "1220c47311099ba5705d906d11927bc222e9562830bcc32b6d638e10124371f4eeb6",
        },
    },

In your build.zig:

    const tkm = b.dependency("tokamak", .{ .target = target, .optimize = optimize });

    // make sure the arguments passed in ".{ }" are the same that tokamak passes to httpz in its build.zig
    const httpz = b.dependency("httpz", .{ .target = target, .optimize = optimize });

As long as the hashes are the same and the arguments passed to b.dependency() are the same, b.dependency() will return the same pointer in your project as it does in httpz.

Alternatively, you can instead extract the httpz dependency out of tokamak's builder instead of copying it into your own build.zig.zon:

    const tkm = b.dependency("tokamak", .{ .target = target, .optimize = optimize });

    // make sure the arguments passed in ".{ }" are the same that tokamak passes to httpz in its build.zig
    const httpz = tkm.builder.dependency("httpz", .{ .target = target, .optimize = optimize });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.