bazelbuild / bazelbuild/rules_rust

Add `exec_target_compatible_with` to `rust.repository_set`

Open
#3,831 1 comment 0 reactions 0 assignees View on GitHub
awaiting-response core
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

`rust.repository_set` includes the [`target_compatible_with`](https://github.com/bazelbuild/rules_rust/blob/0.68.1/rust/extensions.bzl#L178) option to specify a set of constraints to match against the generated "target toolchain". `rules_rust` [also generates an "exec toolchain"](https://github.com/bazelbuild/rules_rust/blob/0.68.1/rust/repositories.bzl#L1106) for compiling proc-macros, and it [derives the set of constraints](https://github.com/bazelbuild/rules_rust/blob/0.68.1/rust/repositories.bzl#L693) for that toolchain strictly from the "target triple" (which, in this case, is the exec triple). This can cause problems when the target toolchain has a superset of the constraints of the exec toolchain.

For example, if I want a toolchain to compile using `musl` (using [this constraint](https://github.com/uber/hermetic_cc_toolchain/blob/57d458428a3ce0c617334ce59d70f7143b14c199/toolchain/libc/BUILD#L10) from `hermetic_cc_toolchain`), I might do this:

```bazel
rust.repository_set(
name = "rust.x86_64-linux.x86_64-linux-musl",
edition = RUST_EDITION,
exec_triple = "x86_64-unknown-linux-gnu",
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
"@zig_sdk//libc:musl",
],
target_triple = "x86_64-unknown-linux-musl",
versions = [RUST_VERSION],
)

use_repo(rust, "rust_toolchains")
register_toolchains("@rust_toolchains//:all")
```

The target toolchain will have the 3 constraints I listed. The exec toolchain will have a subset of those constraints: only `@platforms//cpu:x86_64` and `@platforms//os:linux`, which is strictly more permissive. That means both toolchains would match for a target that's trying to match the 3 specified in `target_compatible_with`, when I only want the target toolchain to match. In order to prevent the exec toolchain from matching, I would have to give it the `@zig_sdk//libc:unconstrained` constraint.

What I want:

```bazel
rust.repository_set(
name = "rust.x86_64-linux.x86_64-linux-musl",
edition = RUST_EDITION,
exec_target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
"@zig_sdk//libc:unconstrained",
],
exec_triple = "x86_64-unknown-linux-gnu",
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
"@zig_sdk//libc:musl",
],
target_triple = "x86_64-unknown-linux-musl",
versions = [RUST_VERSION],
)
```

Currently, I can work around this by making sure that the less-permissive toolchains are registered first, but that solution feels a bit fragile.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.