cargo-bazel: fix hub alias for workspace-member deps with build scripts#3974
Open
lamcw wants to merge 6 commits intobazelbuild:mainfrom
Open
cargo-bazel: fix hub alias for workspace-member deps with build scripts#3974lamcw wants to merge 6 commits intobazelbuild:mainfrom
lamcw wants to merge 6 commits intobazelbuild:mainfrom
Conversation
Problem: When workspace member A depends on workspace member B (where B has both a library target and a build script), cargo-bazel emits hub aliases pointing to @crate_index__B-<ver>//:B. However, extensions.bzl never creates spoke repos for workspace members (repository == null), so these aliases are dangling and cause "no such package" errors during analysis. Solution: In render_module_build_file, detect when a dep is itself a workspace member (context.workspace_members.contains_key(&dep.id)) before calling crate_label(). If it is a workspace member, look up override_targets["lib"] and use that label instead; if no override is set, skip the alias entirely rather than emitting a dangling reference. Callers set override_targets["lib"] via the crate.annotation( override_target_lib = ...) mechanism in MODULE.bazel, which records the hand-written workspace target to redirect to. Testing: - hub_alias_for_workspace_member_dep_with_build_script_uses_override_target: verifies that the hub alias points to the override label, not a spoke repo. - hub_alias_omitted_for_workspace_member_dep_with_no_override: verifies that no alias is emitted when override_targets["lib"] is absent.
Signed-off-by: Thomas Lam <thomaslam@canva.com>
- Update logic to suppress rename aliases for external dependencies that match the crate's name. - Add tests to ensure primary and shorthand aliases are emitted correctly while preventing duplicates. Signed-off-by: Thomas Lam <thomaslam@canva.com>
Author
|
The failing CI check looks like a flake to me? https://buildkite.com/bazel/rules-rust-rustlang/builds/16962#019d9981-e5a4-4085-ba0e-d4051d6bd13f I cannot retry the CI job though (potentially missing permissions?) -- can you please advise how I can proceed? |
Signed-off-by: Thomas Lam <thomaslam@canva.com>
…encies and add tests for external dependencies Signed-off-by: Thomas Lam <thomaslam@canva.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When workspace member A depends on workspace member B,
cargo-bazelemits hubalias()targets pointing to@{index}__B-<ver>//:B. However,extensions.bzlnever creates spoke repos for workspace members (repository == null, skipped atcrate_universe/extensions.bzl:722-724), so these aliases are dangling and cause "no such package" errors during analysis.Solution
In
render_hub_build_file, before callingcrate_label(), check whether the dep is itself a workspace member (is_workspace_member).krate.targetswithfind_map/override_target_key()to look upoverride_targets[key]. If an override is present, use it as the aliasactual; if absent, skip the alias entirely and emit atracing::warn!directing the user to setoverride_target_liboroverride_target_proc_macroinMODULE.bazel. Usingoverride_target_key()(rather than hardcoding"lib") correctly handles bothRule::Library → "lib"andRule::ProcMacro → "proc-macro"targets.extra_aliased_targets: skip all extra aliases for workspace-member deps (they require a spoke repo) and emit a singletracing::warn!per dep.crate_label()is called as before.Callers set
override_targetsviacrate.annotation(override_target_lib = ...)orcrate.annotation(override_target_proc_macro = ...)inMODULE.bazel.Tests
hub_alias_for_workspace_member_dep_with_build_script_uses_override_target: verifies the hub alias points to the override label (not a spoke-repo label) for a workspace-member dep with a build script.hub_alias_for_workspace_member_proc_macro_dep_uses_override_target: verifies the same for aRule::ProcMacrodep, exercising theoverride_target_key()path.hub_alias_omitted_for_workspace_member_dep_with_no_override: verifies noalias()rule is emitted when no override is set.workspace_member_dep_with_alias_same_as_crate_name_and_no_override: verifies that a workspace-member dep aliased under its own crate name with no override emits no hub alias at all.extra_aliased_targets_omitted_for_workspace_member_dep: verifiesextra_aliased_targetsaliases are suppressed for workspace-member deps.extra_aliased_targets_emitted_for_external_dep: verifiesextra_aliased_targetsaliases are still emitted normally for external deps.external_dep_with_rename_same_as_crate_name: verifies that for a non-workspace-member dep aliased under its own crate name, the rename alias is suppressed (deduplication guard) while the primary versioned and shorthand aliases are still emitted exactly once.