Repository: bazelbuild/bazel
Issue: incompatible_auto_configure_host_platform: Switch to new auto-configured host platforms
Labels: P1
team-Configurability
incompatible-change

Flag: --incompatible_auto_configure_host_platform
Available since: 0.23
Will be flipped in: 1.0

**ADDED CONTEXT**

This also affects platforms that inherit from platforms in `@bazel_tools//platforms`.

**Symptoms**: your build works with Bazel 0.29.1 but fails with 'no matching toolchains found' error on local builds with Bazel 1.0 (or Bazel 0.29.1 with `--incompatible_auto_configure_host_platform` is set to true).

**What's happening**: In Bazel 1.0, the default value for `--incompatible_auto_configure_host_platform` was flipped from false to true. The label for the autoconfigured host platform changed from `@bazel_tools//platforms:host_platform` to `@local_config_platform//:host`. The old platform is no longer populated with proper constraints.

**How to fix it**: The quick fix is to replace all occurrences of `@bazel_tools//platforms:host_platform` with `@local_config_platform//:host`.

If your usage of `@bazel_tools//platforms:host_platform` is tied to `remote_execution_properties`, Bazel 1.0 has new features to help you simplify your set up and decouple it from future changes to platforms and toolchains. You might be able to reduce the number of platforms you maintain and remove the need for platform inheritance in the first place. See https://github.com/bazelbuild/bazel/issues/10266 for migration instructions.

**Original Content**

See https://github.com/bazelbuild/proposals/blob/master/designs/2018-10-22-autoconfigured-host-platform.md for more context.
The default host and target platforms are defined in `@bazel_tools//platforms`, and there is special-purpose code in the Platform rule implementation to support this. This complicates the platform rules implementation and makes them dependent on the configuration.

Once this flag is enabled, Bazel will instead default to using a host platform generated by a repository rule, located at `@local_config_platform//:host`, and which will be created only once for a given build. The target platform will also default to this. Once migration is complete, the legacy targets and code can be removed.

Migration notes:
This only affects builds where the `--host_platform` and `--platforms` flags are _not_ used. Any builds currently setting those flags will not be affected.

Any users who rely on `--host_platform` and `--platforms` being affected by the `--host_cpu` and `--cpu` flags will need to begin directly setting `--host_platform` and `--platforms`.


Issue comments:
This flag is part of the work on #6849 .
Changed the targetted flag-flip release, since this will require extensive testing and may need to wait for the projected migration plan for the cpu flag.
Hey @katre, I just re-discovered this flag, what's needed to flip this? Or at least to get to a place where we can ask users to try and to re-enable it on the CI to see what's broken on downstream?
Related: bazelbuild/rules_go#2089, bazelbuild/rules_cc#24, #8763, https://github.com/bazelbuild/bazel/issues/8766.
Major work left:
1. Set the right tags so that we get migration readiness reports from CI, which I apparently never get right.
2. Write directions so that users who currently use cross-compilation with `--cpu` and `--host_cpu` and platform-related rules can add a platform mapping and continue work. I suspect this is a small set of users but I can't prove that.
Looks like only Bazel is to be migrated: https://buildkite.com/bazel/bazelisk-plus-incompatible-flags/builds/175
Thanks for the pointer!
Logs: https://buildkite.com/bazel/bazelisk-plus-incompatible-flags/builds/191#annotation---incompatible_auto_configure_host_platform
Since this flag is already flipped, I'm temporarily removing the `migration-0.29` label from it because https://github.com/bazelbuild/bazel/issues/9104 is preventing the incompatible flags pipeline to give correct result on Windows and our update to Bazelisk is not ready yet.

FYI @dslomov 

Pull request review comments:
Is this line still within the char limit? Unsure whether this can be visualized in github review...
One l too many in untill.
Yes, 99 characters long according to vim.
Fixed.

Patch that resolved the issue:
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/PlatformOptions.java b/src/main/java/com/google/devtools/build/lib/analysis/PlatformOptions.java
index 45e50ab9df2e18..d6f9d6600ed2ab 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/PlatformOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/PlatformOptions.java
@@ -169,7 +169,7 @@ public class PlatformOptions extends FragmentOptions {
 
   @Option(
       name = "incompatible_auto_configure_host_platform",
-      defaultValue = "false",
+      defaultValue = "true",
       documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
       effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
       metadataTags = {


Test patch:
diff --git a/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java b/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java
index b8fb9623df9d20..d2f6d0b6d6e6f3 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java
@@ -35,8 +35,11 @@ public class PlatformTest extends BuildViewTestCase {
   @Rule public ExpectedException expectedException = ExpectedException.none();
 
   @Test
+  // TODO(https://github.com/bazelbuild/bazel/issues/6849): Remove this test when the functionality
+  // is removed, but until then it still needs to be verified.
   public void testPlatform_autoconfig() throws Exception {
-    useConfiguration("--host_cpu=piii", "--cpu=k8");
+    useConfiguration(
+        "--host_cpu=piii", "--cpu=k8", "--noincompatible_auto_configure_host_platform");
 
     scratch.file(
         "autoconfig/BUILD",
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/PlatformMappingFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/PlatformMappingFunctionTest.java
index 985cad1d77ec52..4fa80be5e57ca5 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/PlatformMappingFunctionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/PlatformMappingFunctionTest.java
@@ -63,7 +63,7 @@ public class PlatformMappingFunctionTest extends BuildViewTestCase {
       BuildOptions.diffForReconstruction(
           DEFAULT_BUILD_CONFIG_PLATFORM_OPTIONS, DEFAULT_BUILD_CONFIG_PLATFORM_OPTIONS);
   private static final Label DEFAULT_TARGET_PLATFORM =
-      Label.parseAbsoluteUnchecked("@bazel_tools//platforms:target_platform");
+      Label.parseAbsoluteUnchecked("@local_config_platform//:host");
 
   @Test
   public void testMappingFileDoesNotExist() throws Exception {
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/PlatformMappingValueTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/PlatformMappingValueTest.java
index f1efc3608419b9..b04f2d41f05ef8 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/PlatformMappingValueTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/PlatformMappingValueTest.java
@@ -57,7 +57,7 @@ public class PlatformMappingValueTest {
       BuildOptions.diffForReconstruction(
           DEFAULT_BUILD_CONFIG_PLATFORM_OPTIONS, DEFAULT_BUILD_CONFIG_PLATFORM_OPTIONS);
   private static final Label DEFAULT_TARGET_PLATFORM =
-      Label.parseAbsoluteUnchecked("@bazel_tools//platforms:target_platform");
+      Label.parseAbsoluteUnchecked("@local_config_platform//:host");
 
   @Test
   public void testMapNoMappings() throws OptionsParsingException {
diff --git a/src/test/java/com/google/devtools/build/lib/testutil/TestConstants.java b/src/test/java/com/google/devtools/build/lib/testutil/TestConstants.java
index 0a43017cffb6c0..41358c638fc110 100644
--- a/src/test/java/com/google/devtools/build/lib/testutil/TestConstants.java
+++ b/src/test/java/com/google/devtools/build/lib/testutil/TestConstants.java
@@ -121,6 +121,7 @@ public static void processSkyframeExecutorForTesting(SkyframeExecutor skyframeEx
   public static final ImmutableList<String> PRODUCT_SPECIFIC_FLAGS =
       ImmutableList.of(
           "--target_platform_fallback=@bazel_tools//platforms:default_target",
+          "--platforms=@bazel_tools//platforms:default_target",
           "--host_platform=@bazel_tools//platforms:default_host",
           // TODO(#7903): Remove once our own tests are migrated.
           "--incompatible_py3_is_default=false",
diff --git a/src/test/shell/integration/java_integration_test.sh b/src/test/shell/integration/java_integration_test.sh
index 9722ef0f5722db..bfec5452af8aa3 100755
--- a/src/test/shell/integration/java_integration_test.sh
+++ b/src/test/shell/integration/java_integration_test.sh
@@ -277,7 +277,7 @@ toolchain(
 )
 platform(
     name = 'platform',
-    parents = ['@bazel_tools//platforms:host_platform'],
+    parents = ['@local_config_platform//:host'],
     constraint_values = [':constraint'],
 )
 EOF
@@ -308,6 +308,7 @@ EOF
   # Set javabase to an absolute path.
   bazel build //$pkg/java/hello:hello //$pkg/java/hello:hello_deploy.jar \
       "$stamp_arg" --javabase="//$pkg/jvm:runtime" \
+      --incompatible_auto_configure_host_platform \
       --extra_toolchains="//$pkg/jvm:all,//tools/jdk:all" \
       --platforms="//$pkg/jvm:platform" \
       "$embed_label" >&"$TEST_log" \