Skip to content

Commit

Permalink
Normalize map-like settings in CoreOptions
Browse files Browse the repository at this point in the history
Keeps the analysis cache around if e.g. `--test_env=foo=baz` changes to `--test_env=foo=bar --test_env=foo=baz`.

Fixes #6667

Closes #18306.

PiperOrigin-RevId: 530192685
Change-Id: I2c2ee95be6635220c847eeee1e9df4baabe5e3af
  • Loading branch information
fmeum authored and Copybara-Service committed May 8, 2023
1 parent 28bfb19 commit 4dc5de8
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,19 @@ public LinkedHashMap<String, String> getNormalizedCommandLineBuildVariables() {
return flagValueByName;
}

// Normalizes list of map entries by keeping only the last entry for each key.
private static List<Map.Entry<String, String>> normalizeEntries(
List<Map.Entry<String, String>> entries) {
LinkedHashMap<String, String> normalizedEntries = new LinkedHashMap<>();
for (Map.Entry<String, String> entry : entries) {
normalizedEntries.put(entry.getKey(), entry.getValue());
}
if (normalizedEntries.size() == entries.size()) {
return entries;
}
return normalizedEntries.entrySet().stream().map(SimpleEntry::new).collect(toImmutableList());
}

/// Normalizes --features flags by sorting the values and having disables win over enables.
private static List<String> getNormalizedFeatures(List<String> features) {
// Parse out the features into a Map<String, boolean>, where the boolean represents whether
Expand Down Expand Up @@ -1104,6 +1117,11 @@ public CoreOptions getNormalized() {
// Normalize features.
result.defaultFeatures = getNormalizedFeatures(defaultFeatures);

result.actionEnvironment = normalizeEntries(actionEnvironment);
result.hostActionEnvironment = normalizeEntries(hostActionEnvironment);
result.testEnvironment = normalizeEntries(testEnvironment);
result.commandLineFlagAliases = normalizeEntries(commandLineFlagAliases);

return result;
}
}

0 comments on commit 4dc5de8

Please sign in to comment.