Advice on examples project structure?

Scenario

I’m bulding a library, and includes examples.
I already implemented a build.zig file that compiles examples with zig build examples.
main part be like:

const example_step = b.step("examples", "Build all examples");
const examples = [_][]const u8{
    "foo",
};
for (examples) |name| {
    // executable
    const source = b.fmt("examples/{s}.zig", .{name});
    const exe = b.addExecutable(.{
        .name = name,
        .root_module = b.createModule(.{
            .root_source_file = b.path(source),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "mylib", .module = mod },
            },
        }),
    });

    // install
    const install = b.addInstallArtifact(exe, .{
        .dest_dir = .{ .override = .{ .custom = "examples" } },
    });
    example_step.dependOn(&install.step);
}

Problems

  1. working with ZLS, it doesn’t recognize the mylib import in examples (not giving completions for stuff in the library).
    I’m thinking maybe somehow redirecting the import to ../src/root.zig in zls config?
    Looked in the docs but didn’t find anything similar
  2. I originally made build.zig scan for files under examples automatically, but it seems to mess up build caching (failing to compile new files and ignoring removed ones)
    So eventually ended up with the examples list approach above. It’d be nice to make the scan work.

You could create a separate build.zig with its own build.zig.zon in your examples directory, that way you can write your examples so that they exactly resemble the way that a user of your library would interact with it.

So your example would declare your library as a dependency and your examples/build.zig.zon then lists your library dependency using .mylib = .{ .path = ".." },.

Thanks man, solved second issue, and separating examples build does seem sensible.

First problem left.

I came up with a solution for problem 1, via b.dependOnDirectoryContents function.

const std = @import("std");

pub fn build(b: *std.Build) !void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    // depend on src content
    b.dependOnDirectoryContents(b.path("src"));

    // get src dir
    const io = b.graph.io;
    var dir = try b.root.openDir(io, "src", .{ .iterate = true });
    defer dir.close(io);

    // iterate
    var it = dir.iterate();
    while (try it.next(io)) |entry| {
        if (entry.kind != .file) continue;
        if (!std.mem.endsWith(u8, entry.name, ".zig")) continue;

        const name = entry.name[0 .. entry.name.len - ".zig".len];

        const exe = b.addExecutable(.{
            .name = name,
            .root_module = b.createModule(.{
                .root_source_file = b.path(b.fmt("src/{s}", .{entry.name})),
                .target = target,
                .optimize = optimize,
            }),
        });
        b.installArtifact(exe);
    }
}

Should be okay, pls point out if there’s anything wrong.