expected type '[]const u8', found 'std.fs.OpenError'

I am calling a the following function from the build script and I keep getting the error: expected type '[]const u8', found 'std.fs.OpenError'.

    fn addSources(directory: []const u8, b: *std.build.Builder) []const u8 {
    //Searching for source files
    var sources = std.ArrayList([]const u8).init(b.allocator);

        var dir = try std.fs.cwd().openDir(directory, .{ .iterate = true });

        var walker = try dir.walk(b.allocator);
        defer walker.deinit();

        const allowed_exts = [_][]const u8{ ".c", ".cpp" };

        while (try walker.next()) |entry| {
            const ext = std.fs.path.extension(entry.basename);
            const include_file = for (allowed_exts) |e| {
                if (std.mem.eql(u8, ext, e))
                    break true;
            } else false;

            if (include_file) {
                try sources.append(b.dupe(entry.path));
    return sources.items;

The function call is as follows: var jetSource = addSources("./src/jet", b); where ./src/jet is the path. From what I understand the Zig is unable to open the directory. But, I don’t know why, because the error is dumb and is not detailed enough. I just need a way to get the paths of all the files recursively in a folder I pass that end in *.c and *.cpp.

I read in the source that / is invalid in Windows. I replaced the path with \\src\\jet and it still doesn’t work.

I am using Zig 0.9.1 on Windows.

>Solution :

error: expected type '[]const u8', found 'std.fs.OpenError'

This is a compilation error. Your function uses try, but doesn’t return an error union type. Change the return type to ![]const u8.

Also, you need to use try to call your function: var jetSource = try addSources(...);.

Leave a Reply