'ToolchainResolution picking up toolchain for the wrong platform

I defined a new rust toolchain as follows (forked from https://github.com/bazelbuild/rules_rust):

toolchain(
    name = "toolchain_for_armv7-linux-androideabi",
    exec_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:windows"],
    target_compatible_with = ["@platforms//cpu:armv7", "@platforms//os:android"],
    toolchain = "@rust_windows_x86_64//:toolchain_for_armv7-linux-androideabi_impl",
    toolchain_type = "@rules_rust//rust:toolchain",
)

There is also an existing toolchain defined like this:

toolchain(
    name = "toolchain_for_x86_64-apple-darwin",
    exec_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:osx"],
    target_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:osx"],
    toolchain = "@rust_darwin_x86_64//:toolchain_for_x86_64-apple-darwin_impl",
    toolchain_type = "@rules_rust//rust:toolchain",
)

And building a target for that platform:

bazel build --toolchain_resolution_debug=@rules_rust//rust:toolchain --experimental_platforms_api vrst-test/android --platforms=//platforms:android_armv7

Here is a snippet of ToolchainResolution debug info:

INFO: ToolchainResolution:   Type @rules_rust//rust:toolchain: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @rust_darwin_x86_64//:toolchain_for_x86_64-apple-darwin_impl
INFO: ToolchainResolution:     Type @rules_rust//rust:toolchain: target platform @local_config_platform//:host: Rejected toolchain @rust_darwin_x86_64//:toolchain_for_armv7-linux-androideabi_impl; mismatching values: armv7, android

For some reason, target platform is @local_config_platform//:host despite me explicitly specifying the platform as android_armv7, which is defined as follows:

platform(
    name = "android_armv7",
    constraint_values = [
      "@platforms//cpu:armv7",
      "@platforms//os:android",
    ],
)```


Solution 1:[1]

Bazel resolves toolchains both for the target platforms, specified by --platforms, and the host platform, specified by --host_platform. If you actually want host tools to be built for toolchain_for_armv7-linux-androideabi, you will want to set --host_platform.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Benjamin Peterson