Preserve fallback platform in exec/host transition.

Previously the exec/host transition would set the fallback target platform back to its default whereas the --target_platform_fallback flag's semantics were meant to apply everywhere the platform_mapping does not get used. This change ensures the fallback uses the value of the passed flag in all instances (unless explicitly reset by a transition).

RELNOTES: --target_platform_fallback now also applies to exec/host configurations
PiperOrigin-RevId: 277557685
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 d6f9d66..9d1699c 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
@@ -225,6 +225,7 @@
     host.toolchainResolutionOverrides = this.toolchainResolutionOverrides;
     host.autoConfigureHostPlatform = this.autoConfigureHostPlatform;
     host.useToolchainResolutionForJavaRules = this.useToolchainResolutionForJavaRules;
+    host.targetPlatformFallback = this.targetPlatformFallback;
     return host;
   }
 
diff --git a/src/test/shell/bazel/platform_mapping_test.sh b/src/test/shell/bazel/platform_mapping_test.sh
index 9be52af..8ce3822 100755
--- a/src/test/shell/bazel/platform_mapping_test.sh
+++ b/src/test/shell/bazel/platform_mapping_test.sh
@@ -191,5 +191,95 @@
   expect_log "platform: //plat:platform2"
 }
 
+function test_target_platform_fallback() {
+  cat > platform_mappings <<EOF
+flags:
+  --cpu=k8
+    //plat:platform1
+EOF
+
+  cat > package/BUILD <<EOF
+load("//report:report.bzl", "report_flags")
+report_flags(name = "report")
+EOF
+
+  bazel build --cpu=arm64 package:report \
+      --noincompatible_auto_configure_host_platform \
+      --target_platform_fallback=//plat:platform2 &> "${TEST_log}" \
+      || fail "Build failed unexpectedly"
+  expect_log "platform: //plat:platform2"
+}
+
+function test_target_platform_fallback_after_exec() {
+  echo "" > platform_mappings
+
+  cat > package/rule.bzl <<EOF
+def _my_transition_impl(settings, attrs):
+  return {
+    # Platforms *must* be wiped for transitions to correctly participate in
+    # platform mapping.
+    "//command_line_option:platforms": [],
+  }
+
+
+my_transition = transition(
+  implementation = _my_transition_impl,
+  inputs = [],
+  outputs = [
+      "//command_line_option:platforms",
+  ],
+)
+
+
+def _transitioning_rule_impl(ctx):
+  return []
+
+
+transitioning_rule = rule(
+  implementation = _transitioning_rule_impl,
+  attrs = {
+      "deps": attr.label_list(cfg = my_transition),
+      "_whitelist_function_transition": attr.label(
+          default = "@//tools/whitelists/function_transition_whitelist"),
+  }
+)
+
+
+def _other_rule_impl(ctx):
+  return []
+
+
+other_rule = rule(
+  implementation = _other_rule_impl,
+  attrs = {
+      "tools": attr.label_list(cfg = "exec"),
+  },
+)
+EOF
+
+  cat > package/BUILD <<EOF
+load("//report:report.bzl", "report_flags")
+load("//package:rule.bzl", "other_rule", "transitioning_rule")
+
+other_rule(
+  name = "custom",
+  tools = [ ":transitioning" ],
+)
+
+transitioning_rule(
+  name = "transitioning",
+  deps = [ ":report" ]
+)
+
+report_flags(name = "report")
+EOF
+
+  bazel build --host_platform=//plat:platform1 package:custom \
+      --noincompatible_auto_configure_host_platform \
+      --target_platform_fallback=//plat:platform2 &> "${TEST_log}" \
+      || fail "Build failed unexpectedly"
+  expect_log "platform: //plat:platform2"
+}
+
 run_suite "platform mapping test"