Skip to content

Improper format triggered from build.zig #19608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Validark opened this issue Apr 11, 2024 · 1 comment
Closed

Improper format triggered from build.zig #19608

Validark opened this issue Apr 11, 2024 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@Validark
Copy link
Contributor

Zig Version

0.12.0-dev.3630+215de3ee6

Steps to Reproduce and Observed Behavior

Running these commands:

mkdir bad-zig-project
cd ./bad-zig-project
zig init
zig fetch --save https://github.com/InKryption/rpmalloc-zig-port/archive/56966ca426ef13efa0ab5223fc27aade4866b6a4.tar.gz
sed -i '$s/}$/\n    const rpmalloc = b.dependency("rpmalloc-zig-port", .{ .target = target, .optimize = optimize });\n    exe.root_module.addImport("rpmalloc", rpmalloc.module("rpmalloc"));\n}/' ./build.zig
zig build -freference-trace

produces this error:

/home/niles/zig/0.12.0-dev.3630+215de3ee6/files/lib/std/zig.zig:804:13: error: expected {}, {p}, {_}, {p_} or {_p}, found {s}
            @compileError("expected {}, {p}, {_}, {p_} or {_p}, found {" ++ fmt ++ "}");

Expected Behavior

Expected no error.

The following change makes it "work", but I doubt this is the appropriate fix.

/// Print the string as a Zig identifier, escaping it with `@""` syntax if needed.
fn formatId(
    bytes: []const u8,
    comptime fmt: []const u8,
    options: std.fmt.FormatOptions,
    writer: anytype,
) !void {
    const allow_primitive, const allow_underscore = comptime parse_fmt: {
        var allow_primitive = false;
        var allow_underscore = false;
        for (fmt) |char| {
            switch (char) {
                'p' => if (!allow_primitive) {
                    allow_primitive = true;
                    continue;
                },
                '_' => if (!allow_underscore) {
                    allow_underscore = true;
                    continue;
                },
+               's' => continue,
                else => {},
            }
            @compileError("expected {}, {p}, {_}, {p_} or {_p}, found {" ++ fmt ++ "}");
        }
        break :parse_fmt .{ allow_primitive, allow_underscore };
    };

    if (isValidId(bytes) and
        (allow_primitive or !std.zig.isPrimitive(bytes)) and
        (allow_underscore or !isUnderscore(bytes)))
    {
        return writer.writeAll(bytes);
    }
    try writer.writeAll("@\"");
    try stringEscape(bytes, "", options, writer);
    try writer.writeByte('"');
}
@Validark Validark added the bug Observed behavior contradicts documented or intended behavior label Apr 11, 2024
@ianprime0509
Copy link
Contributor

The issue appears to be here: https://github.com/InKryption/rpmalloc-zig-port/blob/56966ca426ef13efa0ab5223fc27aade4866b6a4/build.zig#L73 This should be {}, not {s}; as the error message indicates, std.zig.fmtId does not allow the format specifier {s} as of #18920.

However, the reference trace seems to be misleading in this case, which might be a bug:

/var/home/ian/src/zig/lib/std/zig.zig:804:13: error: expected {}, {p}, {_}, {p_} or {_p}, found {s}
            @compileError("expected {}, {p}, {_}, {p_} or {_p}, found {" ++ fmt ++ "}");
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    format__anon_20497: /var/home/ian/src/zig/lib/std/fmt.zig:1480:26
    formatType__anon_20340: /var/home/ian/src/zig/lib/std/fmt.zig:494:32
    format__anon_19633: /var/home/ian/src/zig/lib/std/fmt.zig:185:23
    print__anon_18932: /var/home/ian/src/zig/lib/std/io/Writer.zig:23:26
    print: /var/home/ian/src/zig/lib/std/io.zig:324:47
    printType__anon_20563: /var/home/ian/src/zig/lib/std/Build/Step/Options.zig:73:24
    addOptionFallible__anon_20562: /var/home/ian/src/zig/lib/std/Build/Step/Options.zig:44:18
    addOption__anon_20480: /var/home/ian/src/zig/lib/std/Build/Step/Options.zig:39:29
    make: /var/home/ian/src/zig/lib/std/Build/Step/Options.zig:421:23
    create: /var/home/ian/src/zig/lib/std/Build/Step/Options.zig:26:23
    addOptions: /var/home/ian/src/zig/lib/std/Build.zig:635:24
    build: /var/home/ian/.cache/zig/p/12204e2334e652e8188f2298d4b29d03d78e6d101b6b231f0b35c7e7ad137f3be3b9/build.zig:49:32
    runBuild__anon_15700: /var/home/ian/src/zig/lib/std/Build.zig:2074:27
    dependencyInner__anon_15134: /var/home/ian/src/zig/lib/std/Build.zig:2055:29
    dependency__anon_14641: /var/home/ian/src/zig/lib/std/Build.zig:1912:35
    build: /var/home/ian/tmp/bad-zig-project/build.zig:92:34
    runBuild__anon_8989: /var/home/ian/src/zig/lib/std/Build.zig:2074:27
    main: /var/home/ian/src/zig/lib/compiler/build_runner.zig:310:29
    callMain: /var/home/ian/src/zig/lib/std/start.zig:511:32
    callMainWithArgs: /var/home/ian/src/zig/lib/std/start.zig:469:12
    posixCallMainAndExit: /var/home/ian/src/zig/lib/std/start.zig:425:20
    _start: /var/home/ian/src/zig/lib/std/start.zig:338:40

This is pointing at code in std.Build.Step.Options as the source of the error, but that's not where the error is actually being caused. This may be related to #18900.

@Vexu Vexu closed this as not planned Won't fix, can't repro, duplicate, stale Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

3 participants