Skip to content

Commit

Permalink
Patch kotlin v23.4 plugin to escape package names (#676)
Browse files Browse the repository at this point in the history
Update the kotlin v23.4 plugin to include the fix for
protocolbuffers/protobuf#13310, which is
important to fix compilation issues with the generated code when the
package name contains a potential name clash with one of the protobuf
field names.
  • Loading branch information
pkwarren committed Jul 24, 2023
1 parent 48e8beb commit ce7b1b6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions plugins/protocolbuffers/kotlin/v23.4/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*
!BUILD
!Dockerfile
!escape-package-names.patch
!kotlin.cc
2 changes: 2 additions & 0 deletions plugins/protocolbuffers/kotlin/v23.4/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ WORKDIR /build
RUN curl -fsSL -o protoc.tar.gz https://github.com/protocolbuffers/protobuf/releases/download/v23.4/protobuf-23.4.tar.gz \
&& tar --strip-components=1 -zxf protoc.tar.gz \
&& rm protoc.tar.gz
COPY escape-package-names.patch .
RUN patch -p1 -u < escape-package-names.patch
RUN bazel build '//:protoc_lib'
COPY --link BUILD kotlin.cc plugins/
RUN bazel build '//plugins:protoc-gen-kotlin.stripped'
Expand Down
64 changes: 64 additions & 0 deletions plugins/protocolbuffers/kotlin/v23.4/escape-package-names.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
diff --git a/src/google/protobuf/compiler/java/message.cc b/src/google/protobuf/compiler/java/message.cc
index 3ef504526..3d46c2e43 100644
--- a/src/google/protobuf/compiler/java/message.cc
+++ b/src/google/protobuf/compiler/java/message.cc
@@ -1337,8 +1337,7 @@ void ImmutableMessageGenerator::GenerateTopLevelKotlinMembers(
"message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)),
"message_kt",
- EscapeKotlinKeywords(
- name_resolver_->GetKotlinExtensionsClassName(descriptor_)));
+ name_resolver_->GetKotlinExtensionsClassNameEscaped(descriptor_));

for (int i = 0; i < descriptor_->nested_type_count(); i++) {
if (IsMapEntry(descriptor_->nested_type(i))) continue;
diff --git a/src/google/protobuf/compiler/java/message_lite.cc b/src/google/protobuf/compiler/java/message_lite.cc
index b5fddc2a9..7321ed31f 100644
--- a/src/google/protobuf/compiler/java/message_lite.cc
+++ b/src/google/protobuf/compiler/java/message_lite.cc
@@ -859,8 +859,7 @@ void ImmutableMessageLiteGenerator::GenerateTopLevelKotlinMembers(
"message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)),
"message_kt",
- EscapeKotlinKeywords(
- name_resolver_->GetKotlinExtensionsClassName(descriptor_)));
+ name_resolver_->GetKotlinExtensionsClassNameEscaped(descriptor_));

for (int i = 0; i < descriptor_->nested_type_count(); i++) {
if (IsMapEntry(descriptor_->nested_type(i))) continue;
diff --git a/src/google/protobuf/compiler/java/name_resolver.cc b/src/google/protobuf/compiler/java/name_resolver.cc
index 0017e172a..b027865b1 100644
--- a/src/google/protobuf/compiler/java/name_resolver.cc
+++ b/src/google/protobuf/compiler/java/name_resolver.cc
@@ -358,6 +358,19 @@ std::string ClassNameResolver::GetKotlinExtensionsClassName(
descriptor->file(), true, true, true);
}

+std::string ClassNameResolver::GetKotlinExtensionsClassNameEscaped(
+ const Descriptor* descriptor) {
+ std::string name_without_package = ClassNameWithoutPackageKotlin(descriptor);
+ std::string full_name = GetClassFullName(name_without_package,
+ descriptor->file(), true, true, true);
+ std::string name_without_package_suffix = absl::StrCat(".", name_without_package, "Kt");
+ size_t package_end = full_name.rfind(name_without_package_suffix);
+ if (package_end != std::string::npos) {
+ return absl::StrCat("`", full_name.substr(0, package_end), "`", name_without_package_suffix);
+ }
+ return full_name;
+}
+
std::string ClassNameResolver::GetJavaMutableClassName(
const Descriptor* descriptor) {
return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, false),
diff --git a/src/google/protobuf/compiler/java/name_resolver.h b/src/google/protobuf/compiler/java/name_resolver.h
index 205bdc6a4..c602c489c 100644
--- a/src/google/protobuf/compiler/java/name_resolver.h
+++ b/src/google/protobuf/compiler/java/name_resolver.h
@@ -124,6 +124,7 @@ class ClassNameResolver {
std::string GetJavaImmutableClassName(const EnumDescriptor* descriptor);
std::string GetKotlinFactoryName(const Descriptor* descriptor);
std::string GetKotlinExtensionsClassName(const Descriptor* descriptor);
+ std::string GetKotlinExtensionsClassNameEscaped(const Descriptor* descriptor);
std::string GetJavaMutableClassName(const Descriptor* descriptor);
std::string GetJavaMutableClassName(const EnumDescriptor* descriptor);
// Gets the outer class and the actual class for downgraded mutable messages.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:tTzWdcobZKjb+WkboVJg4wZEa0hP+ruWe3fhMMMONCc=
h1:0ZuiLYGk3o4fFrZg82O6Xx8IRqOL24wL628tpzQexZw=
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:P31qNT7XYAEpL55sGp61Y3/jdJYF4HqmvQxoH003TIY=
h1:xI/rWelC9LxTSkLLFcljOUBS8kiJecIdeE3HjxCiAFA=

0 comments on commit ce7b1b6

Please sign in to comment.