Can smb explain to me why I get error.FileNotFound?
const dir = std.Io.Dir.cwd();
var dir_path_buffer: [128]u8 = undefined;
_ = try dir.realPath(init.io, &dir_path_buffer);
~/Projects/programming/Zig/Test
❯ zig build run
error: FileNotFound
/home/squidbow/.local/share/zvm/master/lib/std/Io/Threaded.zig:7013:39: 0x117bb1b in realPathPosix (std.zig)
.NOENT => return error.FileNotFound,
^
/home/squidbow/.local/share/zvm/master/lib/std/Io/Threaded.zig:6925:5: 0x11935f6 in dirRealPathPosix (std.zig)
return realPathPosix(dir.handle, out_buffer);
^
/home/squidbow/.local/share/zvm/master/lib/std/Io/Dir.zig:916:5: 0x11e7b2e in realPath (std.zig)
return io.vtable.dirRealPath(io.userdata, dir, out_buffer);
^
/home/squidbow/Projects/programming/Zig/Test/src/app/main.zig:7:9: 0x11dcc51 in main (main.zig)
_ = try dir.realPath(init.io, &dir_path_buffer);
^
run
└─ run exe Test failure
error: process exited with code 1
failed command: ./zig-out/bin/Test
Build Summary: 3/5 steps succeeded (1 failed)
run transitive failure
└─ run exe Test failure
dimdin August 1, 2026, 4:59pm 2
From the realPath documentation:
This function has limited platform support, and using it can lead to unnecessary failures and race conditions. It is generally advisable to avoid this function entirely.
pasta August 1, 2026, 5:15pm 3
I’m unsure this is because of real path at all. AFAIK .cwd() is special and can’t be used directly like this. Opening it first will likely “work”.
Zig Documentation is what you probably want though, avoiding the realpath problem mentioned above.
SquidBow August 1, 2026, 5:20pm 4
Oh, I see. It worked if I opened the directory with openDir, but not with cwd(), I guess it is in fact special. And actually I did endup using currentPath method in my project.
Any suggestions on how I would get the path of the directory if I will need to in the future, since realPath is not advised to use? Outside of calculating it myself.
pasta August 1, 2026, 5:23pm 5
Honestly that’s about it. IIRC Andrew discovered how cursed getting the “real path” can be for any cross platform library. It’s going to generally be best to try and avoid needing to get it, or calling the system API directly and understanding all it’s documented caveats.
SquidBow August 1, 2026, 5:25pm 6
Gona keep that in mind, thanks for help
spiffyk August 1, 2026, 5:48pm 7
Unless you really, really need to, just don’t work with paths. Work with file descriptors/handles instead (std.Io.File and std.Io.Dir are just thin wrappers around those, so use that), they give you everything you need without the caveats hiding behind working with paths. Only use paths when provided from the outside of your program and even then just immediately open them to get a file descriptor.
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.