From 9d6750935e6440a5f3ade988703b1b4f5c244fb6 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sun, 1 Jun 2025 20:48:44 +0200 Subject: [PATCH] std.Build.Step.ConfigHeader: handle empty keys more permissively ${} is never used in a cmake config header but still needs to be substituted instead of erroring because of an empty key simply omit the value --- lib/std/Build/Step/ConfigHeader.zig | 14 ++++++++------ test/standalone/cmakedefine/config.h.in | 3 +++ test/standalone/cmakedefine/expected_config.h | 3 +++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig index 6bfda7667bac..f1cbd463d3f8 100644 --- a/lib/std/Build/Step/ConfigHeader.zig +++ b/lib/std/Build/Step/ConfigHeader.zig @@ -748,10 +748,12 @@ fn expand_variables_cmake( const key_start = open_pos.target + open_var.len; const key = result.items[key_start..]; - if (key.len == 0) { - return error.MissingKey; - } - const value = values.get(key) orelse return error.MissingValue; + const value = values.get(key) orelse + if (key.len == 0) + .undef + else + return error.MissingValue; + result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len); switch (value) { .undef, .defined => {}, @@ -952,8 +954,8 @@ test "expand_variables_cmake simple cases" { // line with misc content is preserved try testReplaceVariablesCMake(allocator, "no substitution", "no substitution", values); - // empty ${} wrapper leads to an error - try std.testing.expectError(error.MissingKey, testReplaceVariablesCMake(allocator, "${}", "", values)); + // empty ${} wrapper is removed + try testReplaceVariablesCMake(allocator, "${}", "", values); // empty @ sigils are preserved try testReplaceVariablesCMake(allocator, "@", "@", values); diff --git a/test/standalone/cmakedefine/config.h.in b/test/standalone/cmakedefine/config.h.in index f6f973d004fd..7a1981130a69 100644 --- a/test/standalone/cmakedefine/config.h.in +++ b/test/standalone/cmakedefine/config.h.in @@ -94,6 +94,9 @@ // test10 // @noval@@stringval@@trueval@@zeroval@ +// empty key, removed +// ${} + // no substition // ${noval} diff --git a/test/standalone/cmakedefine/expected_config.h b/test/standalone/cmakedefine/expected_config.h index b991727ffff1..c783552a18a7 100644 --- a/test/standalone/cmakedefine/expected_config.h +++ b/test/standalone/cmakedefine/expected_config.h @@ -94,6 +94,9 @@ // test10 // test10 +// empty key, removed +// + // no substition //