Getting path from cwd() fails with error

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

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.

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.

1 Like

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.

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.

1 Like

Gona keep that in mind, thanks for help