bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Add `--incompatible_eagerly_resolve_select_keys`

Open fmeum opened this issue 6 months ago • 9 comments

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 avatar Jun 03 '25 18:06 fmeum

@bazel-io fork 8.3.0

fmeum avatar Jun 04 '25 14:06 fmeum

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

tetromino avatar Jun 08 '25 15:06 tetromino

Could you add a sentence or two as RELNOTES?

Wyverald avatar Jun 10 '25 20:06 Wyverald

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 avatar Jun 10 '25 20:06 fmeum

I filed https://github.com/bazelbuild/bazel/issues/26281.

fmeum avatar Jun 12 '25 14:06 fmeum

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

brandjon avatar Jun 12 '25 17:06 brandjon

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 https://github.com/bazelbuild/bazel/pull/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 avatar Jun 13 '25 15:06 fmeum

I also added doc updates, PTAL.

fmeum avatar Jun 13 '25 15:06 fmeum

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

gregestren avatar Jun 13 '25 21:06 gregestren

@brandjon Friendly ping

fmeum avatar Jul 01 '25 18:07 fmeum

@brandjon @gregestren Friendly ping

fmeum avatar Jul 15 '25 09:07 fmeum

@brandjon @gregestren Friendly ping

fmeum avatar Sep 06 '25 19:09 fmeum

LGTM but what are the recent commits & failing tests?

gregestren avatar Sep 22 '25 15:09 gregestren

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

fmeum avatar Sep 22 '25 15:09 fmeum