Skip to content
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

Deduplicate extra_providers against built-in providers #147

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/cc_define_test/cc_define_test.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
load("@with_cfg.bzl", "with_cfg")

_builder = with_cfg(native.cc_test)
_builder = with_cfg(
native.cc_test,
# Verify that duplicated providers are handled gracefully.
extra_providers = [DefaultInfo, CcInfo],
)
_builder.extend(
"copt",
select({
Expand Down
9 changes: 8 additions & 1 deletion with_cfg/private/rule_defaults.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
visibility("private")

DEFAULT_PROVIDERS = [
SPECIAL_CASED_PROVIDERS = [
DefaultInfo,
# Forwarding is handled by coverage_common.instrumented_files_info.
InstrumentedFilesInfo,
# RunEnvironmentInfo can't be returned from a non-executable, non-test rule and thus requires
# special handling so that it isn't returned by the transitioning alias.
RunEnvironmentInfo,
]

DEFAULT_PROVIDERS = [
AnalysisTestResultInfo,
CcInfo,
CcToolchainConfigInfo,
Expand Down
16 changes: 14 additions & 2 deletions with_cfg/private/with_cfg.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load(":builder.bzl", "make_builder")
load(":providers.bzl", "RuleInfo")
load(":rule_defaults.bzl", "DEFAULT_PROVIDERS", "IMPLICIT_TARGETS")
load(":rule_defaults.bzl", "DEFAULT_PROVIDERS", "IMPLICIT_TARGETS", "SPECIAL_CASED_PROVIDERS")

visibility("//with_cfg/...")

Expand Down Expand Up @@ -125,7 +125,7 @@ def with_cfg(
executable = executable,
test = test,
implicit_targets = implicit_targets,
providers = DEFAULT_PROVIDERS + extra_providers,
providers = _all_providers(extra_providers),
native = _is_native(kind),
supports_inheritance = _supports_inheritance(kind),
supports_extension = _supports_extension(kind),
Expand Down Expand Up @@ -174,3 +174,15 @@ def _supports_extension(kind):

def get_implicit_targets(rule_name):
return IMPLICIT_TARGETS.get(rule_name, [])

def _all_providers(extra_providers):
if not extra_providers:
return DEFAULT_PROVIDERS
all_providers = list(DEFAULT_PROVIDERS)

# Providers aren't hashable.
# TODO: Improve this after https://github.com/bazelbuild/bazel/pull/24848.
for p in extra_providers:
if p not in all_providers and p not in SPECIAL_CASED_PROVIDERS:
all_providers.append(p)
return all_providers
Loading