Skip to content

Fix label resolution for external repos - #896

Open
fmeum wants to merge 2 commits into
bazelbuild:mainfrom
fmeum:claude/package-paths-bzlmod-toolchain-w8lcdu
Open

fmeum wants to merge 2 commits into
bazelbuild:mainfrom
fmeum:claude/package-paths-bzlmod-toolchain-w8lcdu

Conversation

@fmeum

@fmeum fmeum commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

%package(...)% resolved relative to rules_cc itself, not the location where the cc_toolchain was defined. For cc_shared_library, the existing initializer did resolve labels correctly, but failed to do so for select expressions.

…mapping

`cc_toolchain` resolved `%package(@repo//pkg)%` entries in
`cxx_builtin_include_directories` with `Label.relative`, which uses the
repository mapping of the .bzl file that calls it, i.e. that of rules_cc
(or of the Bazel builtins before the rules moved here). Under Bzlmod,
that mapping does not contain the apparent repository names used by the
module that defines the toolchain, so such entries fail with:

  'workspace_root' is not allowed on invalid Label
  @@[unknown repo 'x' requested from @@rules_cc+]//pkg:dummy_target

Use `ctx.package_relative_label` instead, which resolves the package
with the repository mapping of the package that defines the
`cc_toolchain` target, so apparent repository names mean the same thing
as in the BUILD file of the toolchain. This also drops the use of the
deprecated `Label.relative` API, which is gated behind
`--incompatible_enable_deprecated_label_apis`.

Add a regression test that analyzes a `cc_toolchain` defined in a
separate module (`test_repo`, known to rules_cc as `@cross_repo_test`)
whose config refers to that module by its own name. The test only runs
on Bazel 9+, where `cc_toolchain` is implemented in rules_cc; earlier
Bazel versions implement it natively or in the builtins.
`cc_shared_library` resolved `exports_filter` entries with
`Label.relative`, which uses the repository mapping of the .bzl file
that calls it rather than that of the package defining the target. The
rule's initializer worked around this by canonicalizing plain lists via
`native.package_relative_label`, but a `select` in `exports_filter`
bypasses it, so apparent repository names in a `select` fail with:

  'workspace_name' is not allowed on invalid Label
  @@[unknown repo 'x' requested from @@rules_cc+]//:__pkg__

Use `ctx.package_relative_label` in the rule implementation instead and
drop the initializer, which is now redundant. On Bazel versions that
implement cc_shared_library in the builtins, rules_cc forwards to
`native.cc_shared_library`, which has its own initializer.

Add an exports test for a `cc_shared_library` in the separate
`test_repo` module whose `exports_filter` is a `select` referring to
that module by its own name.
@fmeum
fmeum force-pushed the claude/package-paths-bzlmod-toolchain-w8lcdu branch from 8fcdcb9 to 0468ba0 Compare September 17, 2026 07:33
@fmeum
fmeum marked this pull request as ready for review September 17, 2026 08:37
@keith

keith commented Sep 17, 2026

Copy link
Copy Markdown
Member

some stuff about %package() in #277

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants