Skip to content

Commit b04c253

Browse files
committed
std.Build.Step.ConfigHeader: handle undefined keys and values correctly
We intend to mimic the behavior of cmake which means we need to handle undefined keys and values the same
1 parent c04be63 commit b04c253

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/std/Build/Step/ConfigHeader.zig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ fn expand_variables_cmake(
693693
}
694694

695695
const key = contents[curr + 1 .. close_pos];
696-
const value = values.get(key) orelse return error.MissingValue;
696+
const value = values.get(key) orelse .undef;
697697
const missing = contents[source_offset..curr];
698698
try result.appendSlice(missing);
699699
switch (value) {
@@ -748,10 +748,7 @@ fn expand_variables_cmake(
748748

749749
const key_start = open_pos.target + open_var.len;
750750
const key = result.items[key_start..];
751-
if (key.len == 0) {
752-
return error.MissingKey;
753-
}
754-
const value = values.get(key) orelse return error.MissingValue;
751+
const value = values.get(key) orelse .undef;
755752
result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len);
756753
switch (value) {
757754
.undef, .defined => {},
@@ -952,8 +949,8 @@ test "expand_variables_cmake simple cases" {
952949
// line with misc content is preserved
953950
try testReplaceVariablesCMake(allocator, "no substitution", "no substitution", values);
954951

955-
// empty ${} wrapper leads to an error
956-
try std.testing.expectError(error.MissingKey, testReplaceVariablesCMake(allocator, "${}", "", values));
952+
// empty ${} wrapper is removed
953+
try testReplaceVariablesCMake(allocator, "${}", "", values);
957954

958955
// empty @ sigils are preserved
959956
try testReplaceVariablesCMake(allocator, "@", "@", values);
@@ -1019,6 +1016,9 @@ test "expand_variables_cmake simple cases" {
10191016
// unknown key leads to an error
10201017
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@bad@", "", values));
10211018
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${bad}", "", values));
1019+
// unknown key is removed
1020+
try testReplaceVariablesCMake(allocator, "@bad@", "", values);
1021+
try testReplaceVariablesCMake(allocator, "${bad}", "", values);
10221022
}
10231023

10241024
test "expand_variables_cmake edge cases" {
@@ -1099,5 +1099,5 @@ test "expand_variables_cmake escaped characters" {
10991099
try testReplaceVariablesCMake(allocator, "$\\{string}", "$\\{string}", values);
11001100

11011101
// backslash is skipped when checking for invalid characters, yet it mangles the key
1102-
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${string\\}", "", values));
1102+
try testReplaceVariablesCMake(allocator, "${string\\}", "", values);
11031103
}

0 commit comments

Comments
 (0)