Skip to content

Commit

Permalink
Fix proto autoloads and legacy_globals
Browse files Browse the repository at this point in the history
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
  • Loading branch information
comius authored and copybara-github committed Sep 27, 2024
1 parent 623a908 commit 3cb5bea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,31 @@ private ImmutableMap<String, Object> 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<String, Object> legacySymbols =
Expand All @@ -293,7 +308,7 @@ private ImmutableMap<String, Object> 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(
Expand Down Expand Up @@ -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(
Expand All @@ -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"))
Expand Down Expand Up @@ -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"))
Expand Down
8 changes: 4 additions & 4 deletions src/test/shell/integration/load_removed_symbols_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]
Expand Down

0 comments on commit 3cb5bea

Please sign in to comment.