`localhost` lookup on macOS

I’m having trouble with this on macOS;

const std = @import("std");
const Io = std.Io;

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    var addresses_buffer: [16]Io.net.HostName.LookupResult = undefined;
    var canon_name_buffer: [Io.net.HostName.max_len]u8 = undefined;

    var queue: Io.Queue(Io.net.HostName.LookupResult) = .init(&addresses_buffer);
    try std.Io.net.HostName.lookup(try .init("localhost"), io, &queue, .{
        .port = 8000,
        .canonical_name_buffer = &canon_name_buffer,
    });
}

When I compile this with zig build-exe and run it on macOS, I get the following output;

error: UnknownHostName
/opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/Io/Threaded.zig:13756:36: 0x100d8c62b in netLookupFallible (main)
                        .NONAME => return error.UnknownHostName,
                                   ^
/opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/Io/Threaded.zig:13475:21: 0x100d8b483 in netLookup (main)
        else => |e| return e,
                    ^
/opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/Io/net/HostName.zig:164:5: 0x100df1743 in lookup (main)
    return io.vtable.netLookup(io.userdata, host_name, resolved, options);
    ^
/Users/johndevries/repos/poc/src/main.zig:11:5: 0x100dedb2f in main (main)
    try std.Io.net.HostName.lookup(try .init("localhost"), io, &queue, .{
    ^

Is this expected behavior / am I missing something?

Putting this into context, I got the same error when I tried to run Zine on macOS;

*-----------------------------------------------*
|    WARNING: THIS IS A DEBUG BUILD OF ZINE     |
|-----------------------------------------------|
| Debug builds enable expensive sanity checks   |
| that reduce performance.                      |
|                                               |
| To create a release build, run:               |
|                                               |
|           zig build --release=fast            |
|                                               |
| If you're investigating a bug in Zine, then a |
| debug build might turn confusing behavior     |
| into a crash.                                 |
|                                               |
| To disable all forms of concurrency, you can  |
| add the following flag to your build command: |
|                                               |
|              -Dsingle-threaded                |
|                                               |
*-----------------------------------------------*

host: localhost
error: unable to start live webserver: UnknownHostNamethread 4126194 panic: 

(Zine debug stack trace)

error return context:
/opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/Io/Threaded.zig:13756:36: 0x1050ae427 i
n netLookupFallible (zine)
                        .NONAME => return error.UnknownHostName,
                                   ^
/opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/Io/Threaded.zig:13475:21: 0x1050ad27f i
n netLookup (zine)
        else => |e| return e,
                    ^
/opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/Io/net/HostName.zig:164:5: 0x1053ba967 
in lookup (zine)
    return io.vtable.netLookup(io.userdata, host_name, resolved, options);
    ^
/Users/johndevries/repos/zine/src/cli/serve.zig:503:9: 0x10538d1ff in start (zine)
        try Io.net.HostName.lookup(try .init(host), s.io, &queue, .{
        ^

stack trace:
/Users/johndevries/repos/zine/src/fatal.zig:7:48: 0x10539e343 in msg__anon_396889 (zi
ne)
    if (builtin.mode == .Debug) std.debug.panic("\n\n(Zine debug stack trace)\n", .{}
);
                                               ^
/Users/johndevries/repos/zine/src/cli/serve.zig:151:44: 0x10538e497 in serve (zine)
    server.start(cmd) catch |err| fatal.msg(
                                           ^
/Users/johndevries/repos/zine/src/main.zig:118:39: 0x105112cef in main (zine)
        @import("cli/serve.zig").serve(io, gpa, args[1..]) catch fatal.oom();
                                      ^
/opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/start.zig:737:30: 0x10510f463 in callMa
in (zine)
    return wrapMain(root.main(.{
                             ^
???:?:?: 0x18377b153 in start (/usr/lib/dyld)
[1]    67068 abort      ../../zine/zig-out/bin/zine

I found that the hostname Zine sets by default is localhost, which led me to make the minimal POC.

Have you looked at your /etc/hosts file and checked there are lines:

127.0.0.1       localhost   XYZ
::1             localhost

with XYZ replaced by your hostname?

I am running macOS Sequoia 5.7.3 on a MacBook M1 and have no problems compiling & running Zine.

2 Likes

Your sample code works without errors on macOS 26.4.1

My /etc/hosts is:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

In case your DNS cache is poisoned, try:

sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
1 Like

Ah this is super weird; my /etc/hosts content is good but file mode was set to mode 400 for some reason. I changed it back to 644 and then all is good.