Skip to content

Add --incompatible_eagerly_resolve_select_keys #26216

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

fmeum
Copy link
Collaborator

@fmeum fmeum commented Jun 3, 2025

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_settings), 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.

@fmeum fmeum changed the title WIP: Add --incompatible_eagerly_resolve_select_keys WIP: Add --incompatible_resolve_select_keys_eagerly Jun 4, 2025
@fmeum fmeum force-pushed the resolve-select-keys branch from 187c6d1 to e46e8dc Compare June 4, 2025 12:44
@fmeum fmeum changed the title WIP: Add --incompatible_resolve_select_keys_eagerly Add --incompatible_eagerly_resolve_select_keys Jun 4, 2025
@fmeum fmeum force-pushed the resolve-select-keys branch from e46e8dc to 5e6ea48 Compare June 4, 2025 13:11
@fmeum fmeum marked this pull request as ready for review June 4, 2025 14:17
@github-actions github-actions bot added the awaiting-review PR is awaiting review from an assigned reviewer label Jun 4, 2025
@fmeum
Copy link
Collaborator Author

fmeum commented Jun 4, 2025

@bazel-io fork 8.3.0

@iancha1992 iancha1992 added team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts and removed team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel labels Jun 4, 2025
@tetromino tetromino requested a review from brandjon June 8, 2025 15:07
@tetromino
Copy link
Contributor

I didn't have time to look at the code yet. The described behavior change seems reasonable at first glance, but I would like @brandjon to take a look and verify for gotchas.

Also, this this change will require documentation updates (can be in followup PRs).

@Wyverald
Copy link
Member

Could you add a sentence or two as RELNOTES?

fmeum added 2 commits June 10, 2025 22:52
# Conflicts:
#	src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java
@fmeum fmeum force-pushed the resolve-select-keys branch from 3202026 to 1de426a Compare June 10, 2025 20:52
@fmeum
Copy link
Collaborator Author

fmeum commented Jun 10, 2025

Could you add a sentence or two as RELNOTES?

Done!

Also, this this change will require documentation updates (can be in followup PRs).

I can add it in a separate PR. Should I make the docs conditional on the flag value or only change them after the flip?

@fmeum
Copy link
Collaborator Author

fmeum commented Jun 12, 2025

I filed #26281.

@brandjon
Copy link
Member

I agree with the spirit of this change, i.e. having strings canonicalized into label objects at the point where they're passed to select(). This could simplify some logic and be more consistent with user expectations.

One problem might be native.existing_rules(), where due to user convenience and legacy, labels are represented as strings in the starlarkifyValue() conversion. This would mean that round-tripping a select() through this feature would potentially re-resolve the string in the context of the macro rather than preserve its original value. This is more an indictment of the existing_rules API than of your change, but do you have a plan for how to handle this? (E.g., is it a non-issue because it's perhaps rare, or already broken in practice? Or is it a breaking change worth pushing through?)

I can add it in a separate PR. Should I make the docs conditional on the flag value or only change them after the flip?

Usually we include the effect of a flag in the documentation of the API that it affects. So the same PR that introduces the flag would also update the user docstrings for select() or the APIs that read it (e.g. native.existing_rules()). (It might be acceptable to only document the flag itself rather than the Starlark APIs if the existing Starlark API docs don't specify the type.)

packageContext != null
? Label.parseWithPackageContext(
labelString, packageContext, repoMappingRecorder)
: labelString,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit disappointed that we're adding complexity to the select machinery without getting the benefit of ensuring that SelectorValues hold Label keys in all cases, which would perhaps be progress toward merging this class into BuildType.Selector in the future. Is there any fundamental reason we can't normalize to Labels even when there is no packageContext / repoMappingRecorder?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I got this now, thanks for pushing me. It also avoids explicit references to repoMappingRecorder.

@fmeum
Copy link
Collaborator Author

fmeum commented Jun 13, 2025

One problem might be native.existing_rules(), where due to user convenience and legacy, labels are represented as strings in the starlarkifyValue() conversion. This would mean that round-tripping a select() through this feature would potentially re-resolve the string in the context of the macro rather than preserve its original value. This is more an indictment of the existing_rules API than of your change, but do you have a plan for how to handle this? (E.g., is it a non-issue because it's perhaps rare, or already broken in practice? Or is it a breaking change worth pushing through?)

I think that this problem is mostly orthogonal to this change, but I sent #26290 to fix these issues. Since selects aren't inspectable (unless you parse the string representation), it doesn't seem like much of a breaking change to preserve Labels in keys, so I did that.

@fmeum
Copy link
Collaborator Author

fmeum commented Jun 13, 2025

I also added doc updates, PTAL.

@fmeum fmeum requested a review from brandjon June 13, 2025 15:28
@gregestren
Copy link
Contributor

I have no feedback beyond the existing conversation: particularly @brandjon 's approval.

@fmeum
Copy link
Collaborator Author

fmeum commented Jul 1, 2025

@brandjon Friendly ping

@fmeum
Copy link
Collaborator Author

fmeum commented Jul 15, 2025

@brandjon @gregestren Friendly ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review PR is awaiting review from an assigned reviewer team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants