Skip to content

Instantly share code, notes, and snippets.

@stoand
Created January 3, 2025 21:47
Show Gist options
  • Save stoand/87f4129c0b90be5fac44198223f1e246 to your computer and use it in GitHub Desktop.
Save stoand/87f4129c0b90be5fac44198223f1e246 to your computer and use it in GitHub Desktop.
Zig walk directory error: BADF => unreachable

If you attempt to dir.walk on a fs.openDir without setting .{ .iterable = true } in fs.openDir you will get the error:

thread 42910 panic: reached unreachable code
/home/user/.zig/lib/std/posix.zig:5170:18: 0x14f21c9 in lseek_SET (build)
        .BADF => unreachable, // always a race condition

Wrong Code:

const fs = std.fs.cwd();
const dir = fs.openDir("/tmp", .{}) catch unreachable;
var walker = dir.walk(b.allocator) catch unreachable;

while (walker.next() catch unreachable) |entry| {
    std.debug.print("entry basename: {s}\n", .{entry.basename});
}

Right Code:

...
const dir = fs.openDir("/tmp", .{ .iterate = true }) catch unreachable;
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment