From c0f93dd2d9f84af9ba1ebe1dbbe6961cc0836c1f Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Tue, 14 May 2024 11:43:14 +0200 Subject: [PATCH] Remove explicit dependency on protobuf With `--incompatible_enable_proto_toolchain_resolution`, the root module is supposed to provide a `proto_lang_toolchain` for Java, which injects the runtime. Hardcoded dependencies on the `protobuf` module would negate the benefits of supplying a toolchain with precompiled `protoc` and runtime. Since the only used symbol from the protobuf runtime was an exception class, it has been replaced with a check for its class name. If more protobuf runtime symbols should be needed in the future, they should be obtained from a `current_java_proto_runtime` target that first looks for a `proto_lang_toolchain` for Java and only then falls back to the hardcoded reference. --- MODULE.bazel | 1 - .../google/devtools/build/skydoc/renderer/BUILD | 1 - .../build/skydoc/renderer/RendererMain.java | 16 +++++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 1217d43d..d55deab9 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -9,7 +9,6 @@ bazel_dep(name = "bazel_skylib", version = "1.4.2") bazel_dep(name = "rules_java", version = "6.3.0") bazel_dep(name = "rules_jvm_external", version = "5.2") bazel_dep(name = "rules_license", version = "0.0.7") -bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf") # Maven artifacts required by Stardoc; keep consistent with deps.bzl STARDOC_MAVEN_ARTIFACTS = [ diff --git a/src/main/java/com/google/devtools/build/skydoc/renderer/BUILD b/src/main/java/com/google/devtools/build/skydoc/renderer/BUILD index 69a08f36..5ccec659 100644 --- a/src/main/java/com/google/devtools/build/skydoc/renderer/BUILD +++ b/src/main/java/com/google/devtools/build/skydoc/renderer/BUILD @@ -32,7 +32,6 @@ java_library( deps = [ "//src/main/java/com/google/devtools/build/skydoc/rendering", "//stardoc/proto:stardoc_output_java_proto", - "@com_google_protobuf//:protobuf_java", "@stardoc_maven//:com_beust_jcommander", "@stardoc_maven//:com_google_guava_guava", ], diff --git a/src/main/java/com/google/devtools/build/skydoc/renderer/RendererMain.java b/src/main/java/com/google/devtools/build/skydoc/renderer/RendererMain.java index d7ececa7..c021d0ca 100644 --- a/src/main/java/com/google/devtools/build/skydoc/renderer/RendererMain.java +++ b/src/main/java/com/google/devtools/build/skydoc/renderer/RendererMain.java @@ -33,8 +33,6 @@ import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RepositoryRuleInfo; import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo; import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.StarlarkFunctionInfo; -import com.google.protobuf.ExtensionRegistry; -import com.google.protobuf.InvalidProtocolBufferException; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; @@ -81,9 +79,7 @@ public void println() { write("\n"); } }) { - ModuleInfo moduleInfo = - ModuleInfo.parseFrom( - new FileInputStream(inputPath), ExtensionRegistry.getEmptyRegistry()); + ModuleInfo moduleInfo = ModuleInfo.parseFrom(new FileInputStream(inputPath)); MarkdownRenderer renderer = new MarkdownRenderer( @@ -161,8 +157,14 @@ public void println() { printWriter.println(renderer.renderMarkdownFooter(moduleInfo)); } - } catch (InvalidProtocolBufferException e) { - throw new IllegalArgumentException("Input file is not a valid ModuleInfo proto.", e); + } catch (IOException e) { + // Avoid an explicit dependency on the Java protobuf runtime as it should be injected by the + // root module via a proto_lang_toolchain. + if (e.getClass().getName().equals("com.google.protobuf.InvalidProtocolBufferException")) { + throw new IllegalArgumentException("Input file is not a valid ModuleInfo proto.", e); + } else { + throw e; + } } }