From 5f2da94562ecebbb44e6cc5fd0f7cd789ad6f582 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Tue, 22 Sep 2026 15:55:54 +0200 Subject: [PATCH] go: keep internal helper targets manual --- modules/go/tests/BUILD.bazel | 9 ++++++++ modules/go/tests/test_macro.bzl | 41 ++++++++++++++++++++++++++++++++- modules/go/topt_go_test.bzl | 6 +++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/modules/go/tests/BUILD.bazel b/modules/go/tests/BUILD.bazel index a92cbd33..ef8f2327 100644 --- a/modules/go/tests/BUILD.bazel +++ b/modules/go/tests/BUILD.bazel @@ -32,6 +32,7 @@ load( "go_macro_general_mode_linker_flags_target", "go_macro_general_mode_linker_flags_wiring_test", "go_macro_inferred_importpath_metadata_test", + "go_macro_internal_helper_tags_loading_tests", "go_macro_multi_service_target", "go_macro_multi_service_wiring_test", "go_macro_orchestrion_pin_files_provider_test", @@ -799,6 +800,13 @@ go_macro_static_wiring_test( target_under_test = ":go_macro_static_target__raw_go_test", ) +go_macro_internal_helper_tags_loading_tests( + name = "go_macro_internal_helper_tags", + disabled_target = "go_macro_disabled_raw_target", + dynamic_target = "go_macro_single_service_target", + static_target = "go_macro_static_target", +) + go_macro_static_fallback_metadata_test( name = "go_macro_static_fallback_metadata_test", target_under_test = ":go_macro_static_target_topt_bazel_metadata", @@ -987,6 +995,7 @@ test_suite( ":go_macro_env_none_wiring_test", ":go_macro_explicit_service_wiring_test", ":go_macro_inferred_importpath_metadata_test", + ":go_macro_internal_helper_tags_tests", ":go_macro_multi_service_wiring_test", ":go_macro_orchestrion_pin_files_provider_test", ":go_macro_orchestrion_pin_files_wiring_test", diff --git a/modules/go/tests/test_macro.bzl b/modules/go/tests/test_macro.bzl index 5501a0a8..4d127633 100644 --- a/modules/go/tests/test_macro.bzl +++ b/modules/go/tests/test_macro.bzl @@ -18,7 +18,7 @@ with a lightweight fake executable rule so we can capture what the macro forwards at analysis time without compiling Go code. """ -load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts", "unittest") +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts", "loadingtest", "unittest") load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load( "@datadog-rules-test-optimization-go//:topt_go_infer.bzl", @@ -364,6 +364,45 @@ def go_macro_static_identity_mismatch_target(name, tags = None): tags = tags, ) +def go_macro_internal_helper_tags_loading_tests( + name, + dynamic_target, + static_target, + disabled_target): + """Assert generated implementation targets stay out of wildcard selection.""" + env = loadingtest.make(name) + + for target_kind, target_name in [ + ("dynamic", dynamic_target), + ("static", static_target), + ]: + for helper_kind, suffix in [ + ("payloads", "_topt_payloads"), + ("metadata", "_topt_bazel_metadata"), + ]: + helper = native.existing_rule(target_name + suffix) + loadingtest.equals( + env, + "%s_%s_helper_tags" % (target_kind, helper_kind), + ("manual",), + helper.get("tags", []) if helper else None, + ) + + # Generated exports already know they are disabled during macro expansion, + # so they must continue to emit only the caller's ordinary Go test. + loadingtest.equals( + env, + "disabled_payloads_helper_absent", + None, + native.existing_rule(disabled_target + "_topt_payloads"), + ) + loadingtest.equals( + env, + "disabled_metadata_helper_absent", + None, + native.existing_rule(disabled_target + "_topt_bazel_metadata"), + ) + def go_macro_disabled_raw_target(name, tags = None): """Target under test for the strict disabled raw go_test branch.""" dd_topt_go_test( diff --git a/modules/go/topt_go_test.bzl b/modules/go/topt_go_test.bzl index a21cadac..31be9f95 100644 --- a/modules/go/topt_go_test.bzl +++ b/modules/go/topt_go_test.bzl @@ -587,6 +587,9 @@ def dd_topt_go_test( # phase, keeping runtime logic and user callsites simple. topt_go_payloads_selector( name = selector_name, + # The public test owns selection; this implementation detail should + # never become a top-level target through wildcard expansion. + tags = ["manual"], embeds = embed_labels, explicit_importpath = explicit_importpath, fallback_importpath = fallback_importpath, @@ -611,6 +614,9 @@ def dd_topt_go_test( # configuration does not produce a false positive. topt_go_bazel_metadata( name = metadata_name, + # Keep metadata generation attached to the public test instead of + # exposing it as an independently selectable wildcard target. + tags = ["manual"], embeds = embed_labels, explicit_importpath = explicit_importpath or "", fallback_importpath = fallback_importpath or "",