How to import a module inside my module so user don't need to import it again

Hi,

I created a module called cowsay, in this module, I need a third party module to process unicode. It is called zg. when I am developing my module, I just add the zg use exec.root_module.addImport("DisplayWidth", zg.module("DisplayWidth")) and everything is fine.

But, when I create a new project and try to import my cowsay, I have to in addition to exec.root_module.addImport("Cowsay", Cowsay.module("Cowsay");, I have to add zg into root_module again. Otherwise, there is an error when building saying DisplayWidth is not in Cowsay.

Is there a way to enclose zg in my cowsay module, that when user fetch my module, it automatically fetch all dependencies, and the users do not need to do anything with zg at all?

You can add imports to your exported module in build.zig:

    _ = b.addModule("Cowsay", .{
        .root_source_file = b.path("src/main.zig"),
        .imports = &.{
            .{
                  .name = "DisplayWidth",
                  .module = zg.module("DisplayWidth"),
            },
    });
5 Likes

Great! It worked like a charm!

1 Like

2 posts were split to a new topic: Imports and zig run