Zig Error: root source file struct 'X' has no member named 'Y'
error: root source file struct 'X' has no member named 'Y' means you wrote X.Y, where X is a whole file (like std or one of its parts), and that file has nothing called Y. If you’re following an older tutorial, this is the error you’ll see most: root source file struct 'std' has no member named 'io' is what Zig 0.16 and 0.17 say to the hello world from almost every older guide. The fix is usually to find the new name. This page has a table of the most common ones. For the other errors in this series, see Zig Errors Explained.
The error message
Section titled “The error message”const std = @import("std");
pub fn main() !void { const stdout = std.io.getStdOut().writer(); try stdout.print("Hello, {s}!\n", .{"world"});}main.zig:4:24: error: root source file struct 'std' has no member named 'io' const stdout = std.io.getStdOut().writer(); ^~.../lib/std/std.zig:1:1: note: struct declared herepub const AutoHashMap = hash_map.AutoHashMap;^~~The caret points at io, the name that’s missing. The note points at the first line of std.zig, the file Zig searched. That line has nothing to do with your mistake; it’s just where the file starts.
What it means
Section titled “What it means”In Zig, every file is a struct. When you write const std = @import("std");, std becomes a struct whose members are everything the file std.zig declares with pub. “Root source file struct” is the compiler’s way of saying “the struct made from a whole file”. The same goes for std.fs, std.heap or std.time: each is its own file, so the message names whichever one you looked in, like root source file struct 'fs' has no member named 'cwd'.
So the error means one of three things: the name was renamed or removed (very common, Zig is still pre-1.0), you misspelled it, or it’s in your own file and isn’t pub.
How to fix it
Section titled “How to fix it”Fix 1: Use the new name from the current Zig
Section titled “Fix 1: Use the new name from the current Zig”Zig 0.16 moved all I/O into a new std.Io interface, and a lot of older names went away. Here’s the hello world above, written for 0.17.1:
const std = @import("std");
pub fn main(init: std.process.Init) !void { var buf: [256]u8 = undefined; var w = std.Io.File.stdout().writer(init.io, &buf); const stdout = &w.interface; try stdout.print("Hello, {s}!\n", .{"world"}); try stdout.flush();}Output:
Hello, world!Two new ideas: main receives init, which carries init.io (the thing that does the actual I/O), and the writer has a buffer, so you call flush() at the end. Forget flush and nothing is printed. Zig Output walks through it slowly. For quick debug prints, std.debug.print still works as before.
These are the old names people hit most, all checked on 0.17.1:
| Old code | Error says | Now |
|---|---|---|
std.io.getStdOut() |
'std' has no member named 'io' |
std.Io.File.stdout(), with init.io. See Zig Output |
std.fs.cwd() |
'fs' has no member named 'cwd' |
std.Io.Dir.cwd(), methods take io. See Zig File I/O |
std.heap.GeneralPurposeAllocator(.{}) |
'heap' has no member named 'GeneralPurposeAllocator' |
init.gpa. See Zig Allocators |
std.process.argsAlloc(...) |
'process' has no member named 'argsAlloc' |
init.minimal.args.toSlice(arena). See Zig CLI Arguments |
std.os.getenv("X") |
'os' has no member named 'getenv' |
init.environ_map.get("X"). See Zig Environment Variables |
std.time.sleep(ns), std.Thread.sleep(ns) |
'time' has no member named 'sleep' |
try io.sleep(.fromMilliseconds(10), .awake) |
std.time.Timer, std.time.timestamp() |
'time' has no member named 'Timer' |
std.Io.Timestamp.now(io, .awake). See Zig Performance |
std.Thread.Mutex |
'Thread' has no member named 'Mutex' |
std.Io.Mutex. See Zig Shared State |
std.Thread.Pool |
'Thread' has no member named 'Pool' |
io.async(...), io.concurrent(...). See Zig Async |
std.rand |
'std' has no member named 'rand' |
std.Random |
For anything not in this table, the release notes list what moved: 0.16.0 for the big I/O change, and 0.17.0 for the latest one. Searching the standard library source (in lib/std/ next to your zig binary) for the old name’s job often finds the new one quickly.
Fix 2: Fix the spelling
Section titled “Fix 2: Fix the spelling”Not every miss is a rename. std.debug.prnt gives the same kind of error:
const std = @import("std");
pub fn main() void { std.debug.prnt("hi\n", .{});}main.zig:4:14: error: root source file struct 'debug' has no member named 'prnt' std.debug.prnt("hi\n", .{}); ~~~~~~~~~^~~~~Names are case sensitive too: std.Io (the new interface) and std.io (gone) are different names. Typing std.debug. in an editor with ZLS shows the list of members, which settles it.
Fix 3: Mark your own declarations pub
Section titled “Fix 3: Mark your own declarations pub”With your own files, the member may exist but be private. A function in math.zig without pub gives a clearer message:
main.zig:5:36: error: 'add' is not marked 'pub' std.debug.print("{d}\n", .{math.add(2, 3)}); ~~~~^~~~math.zig:1:1: note: declared herefn add(a: i32, b: i32) i32 {^~~~~~~~~~~~~~~~~~~~~~~~~~Write pub fn add(...) in math.zig. If the message says has no member instead, the name really isn’t in that file: check that you imported the file you meant. Zig Modules covers imports and pub.
The same error on your own structs
Section titled “The same error on your own structs”Constants and functions declared inside a struct are members too, and a missing one gives a shorter version of this message:
const Point = struct { x: i32, y: i32,};
pub fn main() void { const o = Point.origin; _ = o;}main.zig:7:20: error: struct 'main.Point' has no member named 'origin' const o = Point.origin; ~~~~~^~~~~~~main.zig:1:15: note: struct declared hereconst Point = struct { ^~~~~~Add it inside the struct (pub const origin: Point = .{ .x = 0, .y = 0 };) or fix the name.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- Code or tutorials for Zig 0.15 and older, especially anything with
std.io,std.fsorGeneralPurposeAllocator. - AI-generated Zig code, which often mixes APIs from several old versions.
- Typos in long
std.paths. - Your own files, when a declaration isn’t
pubor the import points at the wrong file.
Related errors
Section titled “Related errors”- no field or member function named ‘x’: the same kind of miss on a value (
list.push) instead of a file or type. - use of undeclared identifier ‘x’: the name before the dot doesn’t exist at all (often a missing
@import). - expected N argument(s), found M: the name exists, but its parameters changed.
- All Zig error pages: the full list.
Practice
Section titled “Practice”1. Update an old program
Section titled “1. Update an old program”This program was written for an older Zig. Make it build on 0.17.1 and print 5 files, using a buffered stdout writer.
const std = @import("std");
pub fn main() !void { const stdout = std.io.getStdOut().writer(); const count: u32 = 5; try stdout.print("{d} files\n", .{count});}Hint: main needs init for init.io, and the writer needs a buffer and a flush().
Show solution
const std = @import("std");
pub fn main(init: std.process.Init) !void { var buf: [64]u8 = undefined; var w = std.Io.File.stdout().writer(init.io, &buf); const stdout = &w.interface; const count: u32 = 5; try stdout.print("{d} files\n", .{count}); try stdout.flush();}Output:
5 filesstd.Io.File.stdout() replaces std.io.getStdOut(). Take the address of w.interface (copying it compiles but breaks the writer), and flush before main returns.
- Every file is a struct, so
std,std.fsand your own files have members. - The error means the file has no
pubdeclaration with that name. - Most often it’s old code:
std.io,std.fs.cwd,GeneralPurposeAllocator,argsAllocand friends moved in 0.16 and 0.17. - Otherwise check the spelling and case (
std.Io, notstd.io). - In your own files, declarations need
pubto be seen from other files.
Next up: expected N argument(s), found M, the error for calling a function with the wrong number of arguments.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- 0.16.0 release notes: the move to
std.Ioand the newmain(init). - 0.17.0 release notes: the latest renames and removals.
- @import in the Zig language reference: why a file is a struct.
- Zig Output: printing to stdout in Zig 0.17, step by step.