-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[8.5.0] Add --incompatible_eagerly_resolve_select_keys #27429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.5.0] Add --incompatible_eagerly_resolve_select_keys #27429
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
| if (labelConverter == null) { | ||
| StarlarkThreadContext ctx = thread.getThreadLocal(StarlarkThreadContext.class); | ||
| var targetDefinitionContext = ctx instanceof TargetDefinitionContext tdc | ||
| ? tdc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed this from
bazel/src/main/java/com/google/devtools/build/lib/packages/TargetDefinitionContext.java
Lines 163 to 167 in 3ab2a02
| public static TargetDefinitionContext fromOrNull(StarlarkThread thread) { | |
| StarlarkThreadContext ctx = thread.getThreadLocal(StarlarkThreadContext.class); | |
| return ctx instanceof TargetDefinitionContext targetDefinitionContext | |
| ? targetDefinitionContext | |
| : null; |
8.x.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the idiomatic Bazel 8-era way of saying this is
Package.Builder pkgBuilder = Package.Builder.fromOrNull(thread);
if (pkgBuilder != null) {
labelConverter = pkgBuilder.getLabelConverter();
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Thanks.
| public void testKeyResolution(@TestParameter boolean resolveSelectKeysEagerly) throws Exception { | ||
| var ctx = | ||
| BazelModuleContext.create( | ||
| Label.parseCanonicalUnchecked("//other/pkg:def.bzl"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplified from
bazel/src/test/java/com/google/devtools/build/lib/packages/SelectTest.java
Lines 162 to 163 in 3ab2a02
| BazelModuleKey.createFakeModuleKeyForTesting( | |
| Label.parseCanonicalUnchecked("//other/pkg:def.bzl")), |
BazelModuleKey doesn't exist in 8.x.
tetromino
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo the nit in SelectorList.java
| if (labelConverter == null) { | ||
| StarlarkThreadContext ctx = thread.getThreadLocal(StarlarkThreadContext.class); | ||
| var targetDefinitionContext = ctx instanceof TargetDefinitionContext tdc | ||
| ? tdc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the idiomatic Bazel 8-era way of saying this is
Package.Builder pkgBuilder = Package.Builder.fromOrNull(thread);
if (pkgBuilder != null) {
labelConverter = pkgBuilder.getLabelConverter();
}ef394f4
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)