Merge "release_config: cleanup how default values are used" into main
diff --git a/core/release_config.scl b/core/release_config.scl
index 728fc1b..c5815df 100644
--- a/core/release_config.scl
+++ b/core/release_config.scl
@@ -179,18 +179,23 @@
     validate(all_flags, _all_flags_schema)
     validate(all_values, _all_values_schema)
 
+    # Final values.
+    values = {}
     # Validate flags
     flag_names = []
     flags_dict = {}
     for flag in all_flags:
-        if flag["name"] in flag_names:
-            if equal_flag_declaration(flag, flags_dict[flag["name"]]):
+        name = flag["name"]
+        if name in flag_names:
+            if equal_flag_declaration(flag, flags_dict[name]):
                 continue
             else:
-                fail(flag["declared_in"] + ": Duplicate declaration of flag " + flag["name"] +
-                     " (declared first in " + flags_dict[flag["name"]]["declared_in"] + ")")
-        flag_names.append(flag["name"])
-        flags_dict[flag["name"]] = flag
+                fail(flag["declared_in"] + ": Duplicate declaration of flag " + name +
+                     " (declared first in " + flags_dict[name]["declared_in"] + ")")
+        flag_names.append(name)
+        flags_dict[name] = flag
+        # Set the flag value to the default value.
+        values[name] = {"name": name, "value": _format_value(flag["default"]), "set_in": flag["declared_in"]}
 
     # Record which flags go on which partition
     partitions = {}
@@ -206,7 +211,6 @@
 
     # Generate final values.
     # Only declared flags may have a value.
-    values = {}
     for value in all_values:
         name = value["name"]
         if name not in flag_names:
@@ -227,19 +231,13 @@
     for partition, names in partitions.items():
         result["_ALL_RELEASE_FLAGS.PARTITIONS." + partition] = names
     for flag in all_flags:
-        if flag["name"] in values:
-            val = values[flag["name"]]["value"]
-            set_in = values[flag["name"]]["set_in"]
-        else:
-            val = flag["default"]
-            set_in = flag["declared_in"]
-        val = _format_value(val)
+        val = _format_value(values[flag["name"]]["value"])
         result[flag["name"]] = val
         result["_ALL_RELEASE_FLAGS." + flag["name"] + ".PARTITIONS"] = flag["partitions"]
         result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DEFAULT"] = _format_value(flag["default"])
         result["_ALL_RELEASE_FLAGS." + flag["name"] + ".VALUE"] = val
         result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DECLARED_IN"] = flag["declared_in"]
-        result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = set_in
+        result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = values[flag["name"]]["set_in"]
         result["_ALL_RELEASE_FLAGS." + flag["name"] + ".ORIGIN"] = flag["origin"]
 
     return result