When finding a remote match for a selected local Xcode version, attempt to match on local aliases in addition to local version number.

The local versions contain build identifiers, so in cases where the remote Xcode doesn't contain an alias that matches that build identifier, we're currently not recognizing these versions as being the same (and they should be).

RELNOTES: None
PiperOrigin-RevId: 298894872
diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfig.java b/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfig.java
index aa867d7..a4736f8 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfig.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfig.java
@@ -377,12 +377,21 @@
       localVersion =
           remoteAliasesToVersionMap.get(localVersions.getDefaultVersion().getVersion().toString());
     } else {
-      ruleContext.ruleWarning(
-          "You passed --experimental_prefer_mutual_xcode=false, which prevents Bazel from"
-              + " selecting an Xcode version that optimizes your performance. Please consider"
-              + " using --experimental_prefer_mutual_xcode=true.");
-      availability = Availability.LOCAL;
-      localVersion = localVersions.getDefaultVersion();
+      for (String versionNumber : localVersions.getDefaultVersion().getAliases()) {
+        if (remoteAliasesToVersionMap.containsKey(versionNumber)) {
+          availability = Availability.BOTH;
+          localVersion = remoteAliasesToVersionMap.get(versionNumber);
+          break;
+        }
+      }
+      if (localVersion == null) {
+        ruleContext.ruleWarning(
+            "You passed --experimental_prefer_mutual_xcode=false, which prevents Bazel from"
+                + " selecting an Xcode version that optimizes your performance. Please consider"
+                + " using --experimental_prefer_mutual_xcode=true.");
+        availability = Availability.LOCAL;
+        localVersion = localVersions.getDefaultVersion();
+      }
     }
     return Maps.immutableEntry(localVersion, availability);
   }
diff --git a/src/test/java/com/google/devtools/build/lib/rules/apple/XcodeConfigTest.java b/src/test/java/com/google/devtools/build/lib/rules/apple/XcodeConfigTest.java
index 7cea95d..72ff3ab 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/apple/XcodeConfigTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/apple/XcodeConfigTest.java
@@ -492,11 +492,127 @@
         "xcode_version(",
         "    name = 'version10',",
         "    version = '10',",
+        "    aliases = ['10.0.0.10E1001'],",
+        ")",
+        "xcode_version(",
+        "    name = 'other_version10',",
+        "    version = '10.0.0.10E1001',",
+        ")",
+        "available_xcodes(",
+        "    name = 'remote',",
+        "    versions = [':version512', ':version92', ':version9', ':version10'],",
+        "    default = ':version512',",
+        ")",
+        "available_xcodes(",
+        "    name = 'local',",
+        "    versions = [':version84', ':other_version10', ':version92', ':version9'],",
+        "    default = ':other_version10',",
+        ")");
+    useConfiguration(
+        "--xcode_version_config=//xcode:foo", "--experimental_prefer_mutual_xcode=false");
+    assertXcodeVersion("10");
+    assertAvailability(XcodeConfigInfo.Availability.BOTH);
+    assertHasRequirements(
+        ImmutableList.of(
+            ExecutionRequirements.REQUIRES_DARWIN, ExecutionRequirements.REQUIREMENTS_SET));
+
+    assertNoEvents();
+  }
+
+  @Test
+  public void testLocalDefaultMatchesOnLocalAlias() throws Exception {
+    scratch.file(
+        "xcode/BUILD",
+        "xcode_config(",
+        "    name = 'foo',",
+        "    remote_versions = ':remote',",
+        "    local_versions = ':local',",
+        ")",
+        "",
+        "xcode_version(",
+        "    name = 'version512',",
+        "    version = '5.1.2',",
+        "    aliases = ['5', '5.1'],",
+        ")",
+        "xcode_version(",
+        "    name = 'version84',",
+        "    version = '8.4',",
+        ")",
+        "xcode_version(",
+        "    name = 'version92',",
+        "    version = '9.2',",
+        ")",
+        "xcode_version(",
+        "    name = 'version9',",
+        "    version = '9',",
+        ")",
+        "xcode_version(",
+        "    name = 'version10',",
+        "    version = '10',",
         "    aliases = ['10.0'],",
         ")",
         "xcode_version(",
         "    name = 'other_version10',",
-        "    version = '10.0',",
+        "    version = '10.0.0.10E1001',",
+        "    aliases = ['10.0'],",
+        ")",
+        "available_xcodes(",
+        "    name = 'remote',",
+        "    versions = [':version512', ':version92', ':version9', ':version10'],",
+        "    default = ':version512',",
+        ")",
+        "available_xcodes(",
+        "    name = 'local',",
+        "    versions = [':version84', ':other_version10', ':version92', ':version9'],",
+        "    default = ':other_version10',",
+        ")");
+    useConfiguration(
+        "--xcode_version_config=//xcode:foo", "--experimental_prefer_mutual_xcode=false");
+    assertXcodeVersion("10");
+    assertAvailability(XcodeConfigInfo.Availability.BOTH);
+    assertHasRequirements(
+        ImmutableList.of(
+            ExecutionRequirements.REQUIRES_DARWIN, ExecutionRequirements.REQUIREMENTS_SET));
+
+    assertNoEvents();
+  }
+
+  @Test
+  public void testMatchLocalAliasToRemoteName() throws Exception {
+    scratch.file(
+        "xcode/BUILD",
+        "xcode_config(",
+        "    name = 'foo',",
+        "    remote_versions = ':remote',",
+        "    local_versions = ':local',",
+        ")",
+        "",
+        "xcode_version(",
+        "    name = 'version512',",
+        "    version = '5.1.2',",
+        "    aliases = ['5', '5.1'],",
+        ")",
+        "xcode_version(",
+        "    name = 'version84',",
+        "    version = '8.4',",
+        ")",
+        "xcode_version(",
+        "    name = 'version92',",
+        "    version = '9.2',",
+        ")",
+        "xcode_version(",
+        "    name = 'version9',",
+        "    version = '9',",
+        ")",
+        "xcode_version(",
+        "    name = 'version10',",
+        "    version = '10',",
+        "    aliases = ['10.0'],",
+        ")",
+        "xcode_version(",
+        "    name = 'other_version10',",
+        "    version = '10.0.0.10E1001',",
+        "    aliases = ['10'],",
         ")",
         "available_xcodes(",
         "    name = 'remote',",