fix(rules): resolve the label values in config_settings relative to the caller (#4508) The values of label flags set via `config_settings`, such as `--debugger`, were resolved by the transition relative to `rules_python`, so e.g. `":my_debugger"` or `"//tools:my_debugger"` referred to targets in `rules_python` instead of the caller's repository, and the build failed with a `no such target` error. Only canonical labels, e.g. from `str(Label(...))`, worked. The `py_binary` and `py_test` macros run in the context of the caller, so they now convert the values of label flags to canonical labels relative to the package of the caller, like label attributes. Values passed within a `select()` are left as-is. --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com>
diff --git a/news/config-settings-label-values.fixed.md b/news/config-settings-label-values.fixed.md new file mode 100644 index 0000000..fc15268 --- /dev/null +++ b/news/config-settings-label-values.fixed.md
@@ -0,0 +1,3 @@ +(rules) The label values of label flags in `config_settings`, such as +{flag}`--debugger`, are now resolved relative to the package of the caller, +like label attributes, instead of relative to `rules_python`.
diff --git a/python/private/attributes.bzl b/python/private/attributes.bzl index 0fd627f..0698a65 100644 --- a/python/private/attributes.bzl +++ b/python/private/attributes.bzl
@@ -432,6 +432,12 @@ directory. ::: +The values of settings that take a label, such as {flag}`--debugger`, are +resolved relative to the package of the caller, like label attributes. This is +done when the target is defined, so it is not possible when `config_settings` +is a `select()`. In that case, pass canonical label strings, e.g. +`str(Label(":my_debugger"))`, for the values. + :::{seealso} * {obj}`//command_line_option:build_runfile_links` * {obj}`//command_line_option:enable_runfiles` @@ -448,6 +454,11 @@ :::{versionadded} 1.7.0 ::: + +:::{versionchanged} VERSION_NEXT_PATCH +The label values of settings are resolved relative to the package of the +caller. +::: """, ), }
diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index c1fb758..7119dc5 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl
@@ -2182,10 +2182,40 @@ """ providers.append(create_output_group_info(py_info.transitive_sources, output_groups)) +# The settings whose values are labels. +_LABEL_SETTINGS = [ + labels.DEBUGGER, +] + +def _resolve_label_setting_values(config_settings): + """Resolves the label values of `config_settings` relative to the caller. + + Like for label attributes, e.g. `":my_debugger"` refers to a target in the + package of the caller. Otherwise, the transition would resolve it relative + to `rules_python`. + + Args: + config_settings: {type}`dict[Label | str, str]` the settings. + + Returns: + {type}`dict[Label | str, str]` the settings with the label values + converted to canonical label strings. + """ + resolved = {} + for key, value in config_settings.items(): + if value and str(native.package_relative_label(key)) in _LABEL_SETTINGS: + value = str(native.package_relative_label(value)) + resolved[key] = value + return resolved + def _add_config_setting_defaults(kwargs): config_settings = kwargs.get("config_settings", None) if config_settings == None: config_settings = {} + elif type(config_settings) == "dict": + # `config_settings` is configurable and may be a `select()`, which a + # macro cannot inspect, so only plain dicts are resolved here. + config_settings = _resolve_label_setting_values(config_settings) # NOTE: This code runs in loading phase within the context of the caller. # Label() must be used to resolve repo names within rules_python's @@ -2201,6 +2231,7 @@ config_settings = default | config_settings kwargs["config_settings"] = config_settings +# buildifier: disable=unnamed-macro def common_executable_macro_kwargs_setup(kwargs): convert_legacy_create_init_to_int(kwargs) _add_config_setting_defaults(kwargs)
diff --git a/tests/base_rules/py_executable_base_tests.bzl b/tests/base_rules/py_executable_base_tests.bzl index d1be72d..6662491 100644 --- a/tests/base_rules/py_executable_base_tests.bzl +++ b/tests/base_rules/py_executable_base_tests.bzl
@@ -132,6 +132,63 @@ _tests.append(_test_config_settings_extra_toolchains) +def _test_config_settings_label_value_relative_to_caller(name, config): + rt_util.helper_target( + config.rule, + name = name + "_subject", + srcs = ["main.py"], + main = "main.py", + config_settings = { + # The value is relative to the package of the caller. + "@rules_python//python/config_settings:debugger": ":" + name + "_debugger", + }, + ) + rt_util.helper_target( + py_library, + name = name + "_debugger", + srcs = [name + "_debugger.py"], + ) + analysis_test( + name = name, + impl = _test_config_settings_label_value_relative_to_caller_impl, + target = name + "_subject", + ) + +def _test_config_settings_label_value_relative_to_caller_impl(env, target): + env.expect.that_target(target).runfiles().contains_predicate( + matching.str_endswith(target.label.name.removesuffix("_subject") + "_debugger.py"), + ) + +_tests.append(_test_config_settings_label_value_relative_to_caller) + +def _test_config_settings_label_value_select(name, config): + # The values of a select() can't be resolved by the macro, so canonical + # label strings are used. + debugger = str(native.package_relative_label(":" + name + "_debugger")) + rt_util.helper_target( + config.rule, + name = name + "_subject", + srcs = ["main.py"], + main = "main.py", + config_settings = select({ + "//conditions:default": { + "@rules_python//python/config_settings:debugger": debugger, + }, + }), + ) + rt_util.helper_target( + py_library, + name = name + "_debugger", + srcs = [name + "_debugger.py"], + ) + analysis_test( + name = name, + impl = _test_config_settings_label_value_relative_to_caller_impl, + target = name + "_subject", + ) + +_tests.append(_test_config_settings_label_value_select) + def _test_cross_compile_to_unix(name, config): rt_util.helper_target( config.rule,