From 3cb5beaa1550896e0df54d00b66ea215b1e47048 Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 27 Sep 2024 14:46:48 -0700 Subject: [PATCH] Fix proto autoloads and legacy_globals With legacy_symbols I made a typo in 2 location: protobuf repo and in bazel_feature. At this point it's easier to rename it in Bazel. Tested manually on protobuf repository and this works with and without `--incompatible_autoload_externally=+@protobuf` PiperOrigin-RevId: 679735770 Change-Id: I5879060404deab9656acc045da3a6fb37c1d1e5f --- .../build/lib/packages/AutoloadSymbols.java | 29 ++++++++++++++----- .../integration/load_removed_symbols_test.sh | 8 ++--- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/packages/AutoloadSymbols.java b/src/main/java/com/google/devtools/build/lib/packages/AutoloadSymbols.java index 7387e5f4ce1ae8..6b27cfb7d08bbd 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/AutoloadSymbols.java +++ b/src/main/java/com/google/devtools/build/lib/packages/AutoloadSymbols.java @@ -269,16 +269,31 @@ private ImmutableMap modifyBuildBzlEnv( if (AUTOLOAD_CONFIG.get(symbol).isRule()) { nativeBindings.remove(symbol); } else { - envBuilder.remove(symbol); + if (symbol.equals("proto_common_do_not_use") + && envBuilder.get("proto_common_do_not_use") instanceof StarlarkInfo) { + // proto_common_do_not_use can't be completely removed, because the implementation of + // proto rules in protobuf still relies on INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION, + // that reads the build language flag. + envBuilder.put( + "proto_common_do_not_use", + StructProvider.STRUCT.create( + ImmutableMap.of( + "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", + ((StarlarkInfo) envBuilder.get("proto_common_do_not_use")) + .getValue("INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION")), + "no native symbol '%s'")); + } else { + envBuilder.remove(symbol); + } } } if (!isWithAutoloads) { - // In the repositories that don't have autoloads we also expose native.legacy_symbols. + // In the repositories that don't have autoloads we also expose native.legacy_globals. // Those can be used to fallback to the native symbol, whenever it's still available in Bazel. // Fallback using a top-level symbol doesn't work, because BzlCompileFunction would throw an // error when it's mentioned. - // legacy_symbols aren't available when autoloads are not enabled. The feature is intended to + // legacy_globals aren't available when autoloads are not enabled. The feature is intended to // be use with bazel_features repository, which can correctly report native symbols on all // versions of Bazel. ImmutableMap legacySymbols = @@ -293,7 +308,7 @@ private ImmutableMap modifyBuildBzlEnv( ? ((GuardedValue) e.getValue()).getObject() : e.getValue())); nativeBindings.put( - "legacy_symbols", StructProvider.STRUCT.create(legacySymbols, "no native symbol '%s'")); + "legacy_globals", StructProvider.STRUCT.create(legacySymbols, "no native symbol '%s'")); } envBuilder.put( @@ -525,7 +540,8 @@ private static SymbolRedirect renamedSymbolRedirect( symbolRedirect("@rules_cc//cc/common:cc_shared_library_hint_info.bzl", "cc_common")) .put( "cc_proto_aspect", - symbolRedirect("@protobuf//bazel/common:cc_proto_library.bzl", "cc_proto_library")) + symbolRedirect( + "@protobuf//bazel/private:bazel_cc_proto_library.bzl", "cc_proto_aspect")) .put( "ProtoInfo", symbolRedirect( @@ -537,7 +553,6 @@ private static SymbolRedirect renamedSymbolRedirect( "java_proto_library", "proto_lang_toolchain", "java_binary", - "py_extension", "proto_common_do_not_use")) .put( "proto_common_do_not_use", symbolRedirect("@protobuf//bazel/common:proto_common.bzl")) @@ -673,7 +688,7 @@ private static SymbolRedirect renamedSymbolRedirect( "propeller_optimize", ruleRedirect("@rules_cc//cc/toolchains:propeller_optimize.bzl")) .put( "proto_lang_toolchain", - ruleRedirect("@protobuf//bazel/toolchain:proto_lang_toolchain.bzl")) + ruleRedirect("@protobuf//bazel/toolchains:proto_lang_toolchain.bzl")) .put("proto_library", ruleRedirect("@protobuf//bazel:proto_library.bzl")) .put("py_binary", ruleRedirect("@rules_python//python:py_binary.bzl")) .put("py_library", ruleRedirect("@rules_python//python:py_library.bzl")) diff --git a/src/test/shell/integration/load_removed_symbols_test.sh b/src/test/shell/integration/load_removed_symbols_test.sh index 355fb78b950a3c..3c082c74f18406 100755 --- a/src/test/shell/integration/load_removed_symbols_test.sh +++ b/src/test/shell/integration/load_removed_symbols_test.sh @@ -433,7 +433,7 @@ EOF bazel query --incompatible_autoload_externally=+@rules_python ':py_library' --output=build >&$TEST_log 2>&1 || fail "build failed" } -function test_legacy_symbols() { +function test_legacy_globals() { setup_module_dot_bazel mock_rules_java @@ -446,10 +446,10 @@ def _init(specs): return {"specs": specs} def _proguard_spec_info(): - if hasattr(native, "legacy_symbols"): - if hasattr(native.legacy_symbols, "ProguardSpecProvider"): + if hasattr(native, "legacy_globals"): + if hasattr(native.legacy_globals, "ProguardSpecProvider"): print("Native provider") - return native.legacy_symbols.ProguardSpecProvider + return native.legacy_globals.ProguardSpecProvider print("Starlark provider") return provider(fields = ["specs"], init = _init)[0]