Skip to content

Fix indexing in the test runner's log formatting #11703

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

Merged
merged 1 commit into from
Jul 24, 2022

Conversation

LewisGaul
Copy link
Contributor

Without this fix the following fails due to the index of the args being changed by the test runner log function.

const std = @import("std");
test {
    std.log.warn("{[0]d}, 0x{[0]X}", .{123});
}

@LewisGaul
Copy link
Contributor Author

The failing CI logs shows all tests passing (2094 passed; 59 skipped; 0 failed.) but "Step is killed (canceled)" - I don't think this is an issue with the PR.

@marler8997
Copy link
Contributor

Bleh didn't know about this potential issue until now.

Combining multiple format strings/arg tuples is something I do from time to time. Here's a solution I tried, it's a bit of a monstrosity. The direct call to multiFormat works but the logWithPrefixExample segfaults, I think Zig might have issues with tuples within tuples?

const std = @import("std");

pub fn multiFormat(
    writer: anytype,
    comptime fmts: []const []const u8,
    arg_tuples: anytype,
) !void {
    comptime std.debug.assert(fmts.len == arg_tuples.len);
    // NOTE: this should work but maybe we can do something more efficient here?
    inline for (fmts) |fmt, i| {
        try std.fmt.format(writer, fmt, arg_tuples[i]);
    }
}

pub fn logWithPrefixExample(level: []const u8, comptime fmt: []const u8, args: anytype) !void {
    const stderr = std.io.getStdErr().writer();
    std.debug.getStderrMutex().lock();
    defer std.debug.getStderrMutex().unlock();
    try multiFormat(
        stderr,
        &.{ "[{s}] ", fmt },
        .{ .{ level } , .{ args } }
    );
}

pub fn main() !void {
    try multiFormat(
        std.io.getStdOut().writer(),
        &.{ "hello '{0s}'", ", how are you doing '{0s}'?\n"},
        .{ .{ "person" }, .{ "today" }});
    // uncomment to try this, but it will segfault
    //try logWithPrefixExample("yo", "hello again '{0s}'", .{ "person" });
}

@motiejus
Copy link
Contributor

I think Zig might have issues with tuples within tuples?

Yes, see #3915 . Anonymous tuples in anonymous tuples don't really work in stage1.

@Vexu Vexu force-pushed the LG-testing-log-fix branch from ae722d6 to 18aa4f5 Compare July 23, 2022 12:05
@Vexu Vexu force-pushed the LG-testing-log-fix branch from 18aa4f5 to 9ae5fd7 Compare July 23, 2022 13:14
@Vexu Vexu merged commit a08b0fa into ziglang:master Jul 24, 2022
@LewisGaul LewisGaul deleted the LG-testing-log-fix branch August 7, 2022 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants