[8.5.0] Add --incompatible_eagerly_resolve_select_keys#27429
Merged
Conversation
This avoids a common gotcha in which a `select` in a macro with repo names in string keys will generally not work correctly when loaded in the context of a different Bazel (Bzlmod) module that uses a different set of apparent names. In the rare case where resolution relative to the loading `BUILD` file is necessary (e.g. in Skylib's `selects.config_setting_group` or other macros that generate `config_setting`s and `select`s referring to them), the keys can be preprocessed with `native.package_relative_label`. RELNOTES: With the new `--incompatible_eagerly_resolve_select_keys` flag, the label string keys of `select` dicts in `.bzl` files are resolved relative to the containing file instead of relative to the BUILD file that ends up using the `select`. Use `native.package_relative_label` if this is not desired. Closes bazelbuild#26216. PiperOrigin-RevId: 811342967 Change-Id: I8a806ae9b94db248bcb18460984f6b418a1284d6
gregestren
commented
Oct 27, 2025
| if (labelConverter == null) { | ||
| StarlarkThreadContext ctx = thread.getThreadLocal(StarlarkThreadContext.class); | ||
| var targetDefinitionContext = ctx instanceof TargetDefinitionContext tdc | ||
| ? tdc |
Contributor
Author
There was a problem hiding this comment.
Changed this from
8.x.
Contributor
There was a problem hiding this comment.
Nit: the idiomatic Bazel 8-era way of saying this is
Package.Builder pkgBuilder = Package.Builder.fromOrNull(thread);
if (pkgBuilder != null) {
labelConverter = pkgBuilder.getLabelConverter();
}
Contributor
Author
There was a problem hiding this comment.
Updated. Thanks.
gregestren
commented
Oct 27, 2025
| public void testKeyResolution(@TestParameter boolean resolveSelectKeysEagerly) throws Exception { | ||
| var ctx = | ||
| BazelModuleContext.create( | ||
| Label.parseCanonicalUnchecked("//other/pkg:def.bzl"), |
Contributor
Author
There was a problem hiding this comment.
Simplified from
bazel/src/test/java/com/google/devtools/build/lib/packages/SelectTest.java
Lines 162 to 163 in 3ab2a02
BazelModuleKey doesn't exist in 8.x.
fmeum
approved these changes
Oct 27, 2025
tetromino
approved these changes
Oct 27, 2025
tetromino
left a comment
Contributor
There was a problem hiding this comment.
LGTM modulo the nit in SelectorList.java
| if (labelConverter == null) { | ||
| StarlarkThreadContext ctx = thread.getThreadLocal(StarlarkThreadContext.class); | ||
| var targetDefinitionContext = ctx instanceof TargetDefinitionContext tdc | ||
| ? tdc |
Contributor
There was a problem hiding this comment.
Nit: the idiomatic Bazel 8-era way of saying this is
Package.Builder pkgBuilder = Package.Builder.fromOrNull(thread);
if (pkgBuilder != null) {
labelConverter = pkgBuilder.getLabelConverter();
}
Merged
via the queue into
bazelbuild:release-8.5.0
with commit Oct 28, 2025
ef394f4
46 checks passed
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.
Cherry-pick of 054536e:
This avoids a common gotcha in which a
selectin a macro with repo names in string keys will generally not work correctly when loaded in the context of a different Bazel (Bzlmod) module that uses a different set of apparent names.In the rare case where resolution relative to the loading
BUILDfile is necessary (e.g. in Skylib'sselects.config_setting_groupor other macros that generateconfig_settings andselects referring to them), the keys can be preprocessed withnative.package_relative_label.RELNOTES: With the new
--incompatible_eagerly_resolve_select_keysflag, the label string keys ofselectdicts in.bzlfiles are resolved relative to the containing file instead of relative to the BUILD file that ends up using theselect. Usenative.package_relative_labelif this is not desired.For #26216.
Closes #26223 (comment)