Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save akhildevelops/0df10810370d8f6557a032674c9c7c7d to your computer and use it in GitHub Desktop.

Select an option

Save akhildevelops/0df10810370d8f6557a032674c9c7c7d to your computer and use it in GitHub Desktop.

Here's my partial build.zig file of dependency containing the examples folder to build some test artifacts:

const examples_dir_lz = b.path("examples");
    const examples_path = examples_dir_lz.getPath2(b, null);
    var examples_dir = try std.fs.openDirAbsolute(examples_path, .{ .iterate = true });
    var examples_dir_iter = try examples_dir.walk(b.allocator);
    defer examples_dir_iter.deinit();

    while (try examples_dir_iter.next()) |entry| {
        switch (entry.kind) {
            .file => {
                var parts = std.mem.splitScalar(u8, entry.basename, '.');
                const file_name = parts.next().?;
                const test_name = try std.fmt.allocPrint(b.allocator, "{s}_test", .{file_name});

                // Path
                const file_path = try std.fmt.allocPrint(b.allocator, "examples/{s}", .{entry.basename});
                defer b.allocator.free(file_path);

                // Create module and import arrow
                const test_module = b.createModule(.{ .root_source_file = b.path(file_path), .target = target, .optimize = optimize });
                test_module.addImport("arrow", arrow_module);
                const sub_test_step = b.step(test_name, test_name);
                const test_compile = b.addTest(.{ .root_module = test_module });
                const test_run = b.addRunArtifact(test_compile);
                const test_install = b.addInstallArtifact(test_compile, .{ .dest_dir = .{ .override = .{ .custom = "testdata" } }, .dest_sub_path = test_name });
                sub_test_step.dependOn(&test_install.step);
                sub_test_step.dependOn(&test_run.step);
            },
            else => @panic("No directories are currently being supported"),
        }
    }
    const all_examples_module = b.createModule(.{ .root_source_file = b.path("examples/all.zig"), .target = target, .optimize = optimize });
    all_examples_module.addImport("arrow", arrow_module);

    const build_arrays_module = b.createModule(.{ .root_source_file = b.path("examples/build_arrays.zig"), .target = target, .optimize = optimize });
    build_arrays_module.addImport("arrow", arrow_module);

    const example_test_step = b.step("test-examples", "Run example tests");
    const example_tests = b.addTest(.{ .root_module = all_examples_module });
    const run_example_tests = b.addRunArtifact(example_tests);
    example_test_step.dependOn(&run_example_tests.step);

Here's the build.zig.zon file of the dependency that doesn't refer to examples folder

.{
    .name = .arrow_zig,
    .version = "0.0.4",
    .fingerprint = 0x27228191a43ad40a,
    .dependencies = .{
        .lz4 = .{
            .url = "https://github.com/akhildevelops/lz4/archive/refs/heads/master.tar.gz",
            .hash = "lz4-0.12.0-AAAAAMF0AABZR_rPXQ-jhiCLp7Q1TzR5mknsMDwpAbUD",
        },
        .@"flatbuffers-zig" = .{
            .url = "https://github.com/akhildevelops/flatbuffers-zig/archive/refs/heads/master.zip",
            .hash = "flatbuffers-0.0.3-x1VvmzmhAABEVooq1P_ZXmifl3V9tey2uB6J8szAeddl",
        },
    },
    .paths = .{
        "build.zig",
        "build.zig.zon",
        "src",
    },
}

Dependee build error that points to examples folder not being found.

❯ zig build test
thread 43643 panic: unhandled error
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/posix.zig:1828:23: 0x14297ed in openatZ (build)
            .NOENT => return error.FileNotFound,
                      ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/fs/Dir.zig:1565:21: 0x145e46c in openDirFlagsZ (build)
        else => |e| return e,
                    ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/fs/Dir.zig:1529:5: 0x144028a in openDirZ (build)
    return self.openDirFlagsZ(sub_path_c, symlink_flags);
    ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/fs/Dir.zig:1473:5: 0x142ad67 in openDir (build)
    return self.openDirZ(&sub_path_c, args);
    ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/fs.zig:255:5: 0x1415cad in openDirAbsolute (build)
    return cwd().openDir(absolute_path, flags);
    ^
/home/akhil/.cache/zig/p/arrow_zig-0.0.4-CtQ6pKWsBABOIxkmp56ZAejxeGPBKjkd1o5-QHSyWXjF/build.zig:49:24: 0x15757bb in build (build)
    var examples_dir = try std.fs.openDirAbsolute(examples_path, .{ .iterate = true });
                       ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/Build.zig:2428:25: 0x1537697 in runBuild__anon_75831 (build)
        .error_union => try build_zig.build(b),
                        ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/Build.zig:2408:40: 0x14ecadf in dependencyInner__anon_68451 (build)
        sub_builder.runBuild(bz) catch @panic("unhandled error");
                                       ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/Build.zig:2230:35: 0x14c7a35 in dependency__anon_64893 (build)
            return dependencyInner(b, name, pkg.build_root, if (@hasDecl(pkg, "build_zig")) pkg.build_zig else null, pkg_hash, pkg.deps, args);
                                  ^
/home/akhil/projects/flora64/build.zig:13:32: 0x14ad5ae in build (build)
    const module = b.dependency("arrow_zig", .{ .target = target, .optimize = optimize }).module("arrow");
                               ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/Build.zig:2427:33: 0x149ecd0 in runBuild__anon_24616 (build)
        .void => build_zig.build(b),
                                ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/compiler/build_runner.zig:339:29: 0x14978b0 in main (build)
        try builder.runBuild(root);
                            ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/start.zig:660:37: 0x1485f6a in posixCallMainAndExit (build)
            const result = root.main() catch |err| {
                                    ^
/home/akhil/.local/share/zigup/0.14.1/files/lib/std/start.zig:271:5: 0x1485b1d in _start (build)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
error: the following build command crashed:
/home/akhil/projects/flora64/.zig-cache/o/a8010bb4c045d3a2d8beaa05929578a1/build /home/akhil/.local/share/zigup/0.14.1/files/zig /home/akhil/.local/share/zigup/0.14.1/files/lib /home/akhil/projects/flora64 /home/akhil/projects/flora64/.zig-cache /home/akhil/.cache/zig --seed 0xce66e56a -Z0a764d8fd6fa3a15 test

If I have a way in build.zig file of dependency to know if it's used to compile as dependency then I wouldn't include examples as part of the build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment