Migrating to Zig 0.16
TIP
This guide is for the 0.16 line. The current release targets Zig 0.17.0. For the build changes in 0.17, see Release Notes.
The experimental branch of zero adds support for Zig 0.16.0. The main branch stays on 0.15.2, which is the production baseline. This page lists what changes when you move an app to 0.16.
Which branch?
| Branch | Zig version | Status |
|---|---|---|
experimental | 0.16.0 | Experimental |
main | 0.15.2 | Production |
To install the 0.16 line, fetch the experimental branch:
bash
zig fetch --save https://github.com/im-ng/zero/archive/refs/heads/experimental.zipmain signature
Zig 0.16 passes the process init to main. You must pass the I/O handle and the environment map into zero:
zig
const std = @import("std");
const zero = @import("zero");
const App = zero.App;
const Context = zero.Context;
const utils = zero.utils;
pub fn main(init: std.process.Init) !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
const allocator = gpa.allocator();
// I/O reactor is injected directly into App.new (no global setIo call).
const app = try App.new(allocator, init.io, init.environ_map); // pass I/O + env map
try app.get("/json", jsonResponse);
try app.run();
}Here are the mechanical changes from 0.15.2:
- Change
pub fn main() !voidtopub fn main(init: std.process.Init) !void. Then inject the process I/O reactor by passinginit.ioas the second argument toApp.new. - Change
App.new(allocator)toApp.new(allocator, init.io, init.environ_map).zerono longer installs thestd.Ioreactor through the globalutils.setIo(init.io)call. Instead, it stores the reactor on thecontainerandContext(container.io/ctx.io). It also seeds a thinutils.ioglobal duringAppbootstrap, which stateless helpers use: utils, logger, zsutil, metricz, kvstore.
build.zig.zon must target the new compiler:
zig
.minimum_zig_version = "0.16.0",Switching Zig versions
Clear your caches before you switch Zig versions. A stale cache causes build failures and runtime corruption:
bash
rm -rf .zig-cache zig-out zig-pkg/Known gotchas
librdkafkais linked as a weak system library. Builds fail unless you installlibrdkafka-dev(apt install librdkafka-dev/brew install librdkafka).- On macOS,
build.zighardcodes include and lib paths at/usr/local/Cellar/librdkafka/2.13.0. src/cronz/scheduler.zigandsrc/mw/authProvider.zigimport with@import("../zero.zig")(a relative path), not@import("zero").0.15.2has a runtime breakage: insrc/logger.zig, astd.fs.File.stdout()I/O pattern silently fails on 0.15.2. There is no log output and the HTTP server never binds. Theexperimentalbranch (0.16.0) fixes it.