Unreachable code when printing help menu
I ran across a bug (user error?) where the program panics with an unreachable code error when trying to print help text. It occurs specifically when trying to use an array option on a subcommand. Here is a minimal example that produces the error:
const std = @import("std");
const cli = @import("zig-cli");
const heap = std.heap;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
pub fn main() !void {
const action = try parseArgs();
return action();
}
fn parseArgs() cli.AppRunner.Error!cli.ExecFn {
var r = try cli.AppRunner.init(heap.page_allocator);
const app = cli.App{
.command = cli.Command{
.name = "demo",
.description = cli.Description{
.one_line = "Demo app",
},
.target = cli.CommandTarget{
.subcommands = &.{
try devCommand(&r),
},
},
},
};
return r.getAction(&app);
}
var dev_context = struct {
attach: []const []const u8 = undefined,
}{};
fn devCommand(r: *cli.AppRunner) !cli.Command {
return cli.Command{
.name = "dev",
.options = &.{
.{
.long_name = "attach",
.short_alias = 'a',
.help = "Attach to service output",
.value_ref = r.mkRef(&dev_context.attach),
},
},
.target = cli.CommandTarget{
.action = cli.CommandAction{
.exec = execDevCommand,
},
},
};
}
fn execDevCommand() !void {
std.debug.print("Not implemented.\n", .{});
}
I can then cause the error by running zig build run -- dev --help.
I'm using Zig version 0.13.0-dev.266+0b0625ccf, but this occurs on version 0.12.0 as well.
I'm using the latest zig-cli version from this commit 9a94c4803a52e54c26b198096d63fb5bde752da2.
@mitchelldw01 Thanks for reporting. I can reproduce this issue. It happens when you call with zig build run -- .... It's working when you call ./zig-out/bin/dev --help. I will look into.
The error does not happen if there is no zig-cache and zig-out directories. The error happens only on the second run when the folders already present.
Here is the error
thread 136939 panic: reached unreachable code
/Users/alexei/opt/zig/lib/std/debug.zig:403:14: 0x102cca337 in assert (build)
if (!ok) unreachable; // assertion failure
^
/Users/alexei/opt/zig/lib/std/Build/Cache.zig:896:15: 0x102e0f8c3 in writeManifest (build)
assert(self.have_exclusive_lock);
^
/Users/alexei/opt/zig/lib/std/Build/Step.zig:554:26: 0x102e10233 in writeManifest (build)
man.writeManifest() catch |err| {
^
/Users/alexei/opt/zig/lib/std/Build/Step/Run.zig:713:31: 0x102db120b in make (build)
try step.writeManifest(&man);
^
/Users/alexei/opt/zig/lib/std/Build/Step.zig:182:13: 0x102d4165b in make (build)
s.makeFn(s, prog_node) catch |err| switch (err) {
^
/Users/alexei/opt/zig/lib/compiler/build_runner.zig:948:31: 0x102d06c8f in workerMakeOneStep (build)
const make_result = s.make(&sub_prog_node);
^
/Users/alexei/opt/zig/lib/std/Thread/Pool.zig:102:39: 0x102d075fb in runFn (build)
@call(.auto, func, closure.arguments);
^
/Users/alexei/opt/zig/lib/std/Thread/Pool.zig:191:18: 0x102d6b977 in worker (build)
runFn(&run_node.data);
^
/Users/alexei/opt/zig/lib/std/Thread.zig:408:13: 0x102d3fea7 in callFn__anon_14051 (build)
@call(.auto, f, args);
^
/Users/alexei/opt/zig/lib/std/Thread.zig:674:30: 0x102d064a7 in entryFn (build)
return callFn(f, args_ptr.*);
^
???:?:?: 0x19b2a6f93 in ??? (libsystem_pthread.dylib)
???:?:?: 0x957b00019b2a1d33 in ??? (???)
It can be also a bug in the zig build system. This issue in zig https://github.com/ziglang/zig/issues/19973 seems to be related.
@mitchelldw01 I cannot reproduce this error any more with zig-master. I'm going to close this issue. But I can reopen if it's not the case for you.