Trying to learn Standard Library; Declaration not found

Battling with the standard library. Please explain what the prescribed zig way is to master this area.

Is it simply, just start reading the source code? Or are there any resources I should be
checking out first?

All the best

My issue in detail

Trying to work out the secrets of zig files on Linux, Debian.

My code

const std = @import("std");

pub fn main() !void {
const subdirect2= try std.fs.cwd(); _=subdirect2;


main.zig:4:33: error: expected error union type, found 'fs.Dir'
const subdirect2= try std.fs.cwd(); _=subdirect2;

Interesting as I did not specify anything for subdirect2. It should get whatever cwd gives it.

Web; zig lan; standard lib 0.13.0; std; fs; cwd;

Source code has link Posix.At.FCWD


Declaration not found.

What am I doing wrong? Thanks in advanced. Obviously I am at a head slap moment, where I have done something stupid.

The problem is, with the keyword try you’re telling the compiler that the statement after it can fail, ie is an error union. But std.fs.cwd() can not fail. At least it is not planned. Just remove the try and it should work.
Std Lib Doc Cwd()


And it works. Thanks. I simply would never have worked that out from the error message. Which was:

error: expected error union type, found 'fs.Dir'
const subdirect2= **try** std.fs.cwd(); _=subdirect2;

The error makes much more sense now. The “try” unwraps any errors from the return type. Which means it was expecting a union (Error and return type). But because it never fails it can’t return an error. So the try has nothing to unwrap.

Any idea why I get “Declaration not found” when I tried to look up the return type from cwd on the Standard Library?

edit: spelling

posix.AT.FDCWD is not the return type of std.fs.cwd(), it’s the value that the fd field of the std.fs.Dir returned from cwd() is set to on non-Windows/non-WASI platforms (see this recent thread for more info). The Declaration not found is still a bug in the docs, though.

The return type of std.fs.cwd() is std.fs.Dir.


1 Like

This message makes sense if you break it down in pieces. The compiler was expecting an error union type (they result of a ! return type) but it found a fs.Dir instead. It really helps to read those messages from the perspective of types. Even so, it takes a while to get a handle on what they all mean.

1 Like

This is a good candidate for an advice line. Something like “try used here”, with a ^ pointer to the try line.