Skip to content

Commit d7490c8

Browse files
tamasvajkTamas VajkUebelAndre
authored
Fix platform label context in rule transitions (#3997)
## Summary Fix platform label resolution in rule transitions for `rust_binary`, `rust_shared_library`, `rust_static_library`, and `rust_test`. ## Problem When a platform label comes from the main repository (no repo name), `str(label)` produces `//platforms:foo` instead of `@//platforms:foo`. Bazel's `//command_line_option:platforms` setting requires the canonical form with `@`, so the transition silently selects the wrong platform or fails. This affects anyone using the `platform` attribute on Rust rules with a label defined in the main repository. Note: as mentioned in #3997 (comment), this only affects builds that use `--noincompatible_unambiguous_label_stringification`. ## Fix Extract a `_resolve_platform` helper that adds the `@` prefix when `attr.platform.repo_name` is empty, and apply it consistently to all four rule transitions. The original patch only fixed `rust_binary`; this PR fixes all four for completeness. --- > **Note:** This PR was largely AI-generated using Claude Code, with human review and guidance throughout. --------- Co-authored-by: Tamas Vajk <tamas.vajk@databricks.com> Co-authored-by: UebelAndre <github@uebelandre.com>
1 parent 8e9f73f commit d7490c8

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

rust/private/rust.bzl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,9 +1006,29 @@ rust_library = rule(
10061006
"""),
10071007
)
10081008

1009+
def _resolve_platform(settings, attr):
1010+
"""Resolve the platform label for a transition, adding @ prefix if needed.
1011+
1012+
With --noincompatible_unambiguous_label_stringification, str(label) for
1013+
main-repo labels omits the leading @, producing "//foo:bar" instead of
1014+
"@//foo:bar". The platform setting requires the canonical form with @.
1015+
See https://github.com/bazelbuild/bazel/issues/15916.
1016+
1017+
Note that this function will no longer be needed if
1018+
`--noincompatible_unambiguous_label_stringification` is dropped but
1019+
it's currently required internally by Google.
1020+
See https://github.com/bazelbuild/bazel/issues/16196
1021+
"""
1022+
if not attr.platform:
1023+
return settings["//command_line_option:platforms"]
1024+
platform = str(attr.platform)
1025+
if not platform.startswith("@"):
1026+
platform = "@" + platform
1027+
return platform
1028+
10091029
def _rust_static_library_transition_impl(settings, attr):
10101030
return {
1011-
"//command_line_option:platforms": str(attr.platform) if attr.platform else settings["//command_line_option:platforms"],
1031+
"//command_line_option:platforms": _resolve_platform(settings, attr),
10121032
}
10131033

10141034
_rust_static_library_transition = transition(
@@ -1049,7 +1069,7 @@ rust_static_library = rule(
10491069

10501070
def _rust_shared_library_transition_impl(settings, attr):
10511071
return {
1052-
"//command_line_option:platforms": str(attr.platform) if attr.platform else settings["//command_line_option:platforms"],
1072+
"//command_line_option:platforms": _resolve_platform(settings, attr),
10531073
}
10541074

10551075
_rust_shared_library_transition = transition(
@@ -1171,7 +1191,7 @@ _RUST_BINARY_ATTRS = {
11711191

11721192
def _rust_binary_transition_impl(settings, attr):
11731193
return {
1174-
"//command_line_option:platforms": str(attr.platform) if attr.platform else settings["//command_line_option:platforms"],
1194+
"//command_line_option:platforms": _resolve_platform(settings, attr),
11751195
}
11761196

11771197
_rust_binary_transition = transition(
@@ -1418,7 +1438,7 @@ rust_test_without_process_wrapper_test = rule(
14181438

14191439
def _rust_test_transition_impl(settings, attr):
14201440
return {
1421-
"//command_line_option:platforms": str(attr.platform) if attr.platform else settings["//command_line_option:platforms"],
1441+
"//command_line_option:platforms": _resolve_platform(settings, attr),
14221442
}
14231443

14241444
_rust_test_transition = transition(

0 commit comments

Comments
 (0)