Skip to content

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 and selects 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.

@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 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

@fmeum
Copy link
Collaborator Author

fmeum commented Sep 6, 2025

@brandjon @gregestren Friendly ping

Copy link
Contributor

@gregestren gregestren left a comment

Choose a reason for hiding this comment

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

other macros that generate config_settings

Sorry, can you clarify this part? It's not just a macro generating a config_setting, but including a select(), isn't it?

@fmeum fmeum force-pushed the resolve-select-keys branch from a9cf114 to c57fbbb Compare September 20, 2025 06:55
@fmeum fmeum requested a review from gregestren September 20, 2025 06:55
@gregestren
Copy link
Contributor

LGTM but what are the recent commits & failing tests?

@fmeum fmeum force-pushed the resolve-select-keys branch from c57fbbb to f41e591 Compare September 22, 2025 15:30
@fmeum
Copy link
Collaborator Author

fmeum commented Sep 22, 2025

@gregestren I rebased onto master and didn't notice the build failure that caused. Should be fixed now.

@gregestren gregestren added awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally and removed awaiting-review PR is awaiting review from an assigned reviewer labels Sep 22, 2025
@github-actions github-actions bot removed the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Sep 25, 2025
gregestren pushed a commit to gregestren/bazel that referenced this pull request Oct 27, 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_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
github-merge-queue bot pushed a commit that referenced this pull request Oct 28, 2025
Cherry-pick of
054536e:

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.

For #26216.
Closes
#26223 (comment)

---------

Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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