Importing a library with examples

As a newbie to programming, I made a toy project with this guide: Build system tricks

It’s going well, but still can’t find a solution so that when importing, errors don’t appear regarding the missing examples.

All the code is here: ztester

  • ztester - is the library with examples (minus zimport)
  • zimport - is the project which import ztester

Tested only with zig version 0.12.0 on Linux Fedora 39.

Build and run the project result in:

zig build run
ztester: error.FileNotFound
Hello zimport

The error ztester: error.FileNotFound means that the examples directory from ztester library is missing in zimport.

This fact seems somewhat strange to me because no step from the build library is propagated to the top-level install step. From documentation, addInstallArtifact merely creates the step; it does not add it to the dependencies of the top-level install step.

The only solution have found so far is to make a separate build, without examples, for the ztester.tar.gz package.

Is there a better solution?

And one last question, creating a module only makes sense from the perspective of an already compiled library that is linked by user, such as a shared or static library, correct?

If the library is a dependency, @import(“ztester”) works as expected. No need for a module (or a build.zig), at least that’s what I noticed.

Sorry for my childish problems, but couldn’t find a solution on my own. Like I said, I have no programming experience.

Thanks in advance for your help.

All the best!!!

Hello @7R35C0 Welcome to ziggit :slight_smile:

In ztester build.zig.zon .paths you must add "examples",
see: build.zig.zon#paths

1 Like

Sorry but I do not understand.

Maybe I made myself difficult to understand by putting the ztester library together with zimport, which is an independent project, I had to make two repositories.

However, what does .path from ztester build.zig.zon have to do with examples, they are not imported, they are not actually part of the final library.

Thanks for your help! :pray:

Adding examples to .paths in ztester build.zig.zon does not remove the error.
I realized that maybe you don’t mean dependency.path

Another try with examples in .paths and in package content, not working.

Could you share the build.zig.zon? It’s hard to help without concrete details

All the code is here: ztester

  • ztester - is the library with examples (minus zimport)
  • zimport - is the project which import ztester

The dependency is from latest release.

I tried with and without examples in ztester build.zig.zon .paths and package, still the error occurs.

Thanks! :pray:

I am sorry, wrong fix.

The problem is in:

fn setupExamples(b: *std.Build, cfg: Config, mod: *std.Build.Module) void {
    var egs_dir = std.fs.cwd().openDir(
        "examples",

It tries to find examples in current working directory, which is fine when build is called from ztester but it is not fine when build is called from zimport.

The following code, opens the “examples” in the ztester “build.zig” directory:

fn setupExamples(b: *std.Build, cfg: Config, mod: *std.Build.Module) void {
    var egs_dir = std.fs.openDirAbsolute(
        b.path("examples").getPath(b),
        .{ .iterate = true },
    ) catch |err| {
        print("{s}: {!}\n", .{ cfg.name, err });
        return;
    };
    defer egs_dir.close();

To use the latest ztester code, change the zimport/build.zig.zon to:

.{
    .name = "zimport",
    .version = "0.0.0",
    //.minimum_zig_version = "0.12.0",

    .dependencies = .{
        .ztester = .{ .path = ".." },
    },

    .paths = .{
        "src",
        "build.zig",
        "build.zig.zon",
        "LICENSE",
        "README.md",
    },
}
2 Likes

No problem, I wasn’t clear enough either.

Your solution solves the problem when a local path is used.
With a remote package (.url and .hash), the error persists.

Do I need to add examples folder to ztester build.zig.zon .paths and package?
I honestly don’t see why it would be necessary.

Thanks!

Yes, finally it seems to be working.

I had to add examples in zon and package.

.paths = .{
        "examples",
        "src",
        "build.zig",
        "build.zig.zon",
        "LICENSE",
        "README.md",
    },

I don’t have enough words to thank you for your help.
All last week I struggled to solve the problem. :sweat_smile:

Thank you very much! :pray: :pray: :pray:

1 Like

All the code is up to date here, ztester if anyone needs it.

Thank you all!

For this small zdirwalker library, I needed to stop the examples error when importing into another project.

Thanks!