Hejsil / zig-clap

Command line argument parsing library
MIT License
939 stars 67 forks source link

A parameter without a long name crashes the `stage2` compiler #84

Closed paveloom closed 11 months ago

paveloom commented 1 year ago

So, here is an example from the tutorial, except I removed the , --help part from the first flag.

const std = @import("std");

const clap = @import("clap");

pub fn main() !void {
    // Define the parameters
    const params = comptime clap.parseParamsComptime(
        \\-h                     Display this help and exit.
        \\-n, --number <usize>   An option parameter, which takes a value.
        \\-s, --string <str>...  An option parameter which can be specified multiple times.
        \\<str>...
        \\
    );
    // Parse the arguments
    var diag = clap.Diagnostic{};
    var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
        .diagnostic = &diag,
    }) catch |err| {
        // Report useful error and exit
        diag.report(std.io.getStdErr().writer(), err) catch {};
        return err;
    };
    defer res.deinit();
}

Compiling this with Zig v0.11.0-dev.251+7c527c6df gives the following:

error: zig-clap-issue...
error: The following command terminated unexpectedly:
/var/home/paveloom/.zig/0.11.0-dev.251+7c527c6df/files/zig build-exe /var/home/paveloom/Playground/zig-clap-issue/src/main.zig --cache-dir /var/home/paveloom/Playground/zig-clap-issue/zig-cache --global-cache-dir /var/home/paveloom/.cache/zig --name zig-clap-issue --pkg-begin clap /var/home/paveloom/Playground/zig-clap-issue/.zigmod/deps/v/git/github.com/Hejsil/zig-clap/commit-dbc6b8e/clap.zig --pkg-end --enable-cache
error: the following build command failed with exit code 5:
/var/home/paveloom/Playground/zig-clap-issue/zig-cache/o/c2b998d2b5b9d41fadce3bee6d957fe9/build /var/home/paveloom/.zig/0.11.0-dev.251+7c527c6df/files/zig /var/home/paveloom/Playground/zig-clap-issue /var/home/paveloom/Playground/zig-clap-issue/zig-cache /var/home/paveloom/.cache/zig run

I would expect this syntax to work, considering you have tests for this use case (which pass for me):

https://github.com/Hejsil/zig-clap/blob/dbc6b8e54ad753b0605feaeecc8e79dba3572ed3/clap.zig#L1770-L1816

Hejsil commented 1 year ago

Seems you have crashed the compiler. A way to work around this is to use the stage1 compiler. This is done by either passing -fstage1 to zig build-exe or setting the exe.stage1 field to true in your build.zig file.

I'll have a look around to see if this has been reported to zig

Hejsil commented 1 year ago

This is the bug it seems https://github.com/ziglang/zig/issues/13707

Hejsil commented 1 year ago

Update. Another issue that needs fixing https://github.com/ziglang/zig/issues/13779