protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

[reopened] Generated Kotlin fails to compile when comments includes certain strings in brackets

Open unmultimedio opened this issue 9 months ago • 1 comments

This is a re-occurrence of a bug previously reported in https://github.com/protocolbuffers/protobuf/issues/9427 but I don't have permissions to reopen that issue.

What version of protobuf and what language are you using?

$ protoc --version
libprotoc 30.2

$ kotlinc -version
info: kotlinc-jvm 2.1.20 (JRE 23.0.2)

What operating system (Linux, Windows, ...) and version? MacOs

What runtime / compiler are you using (e.g., python version or gcc version) Maven

$ mvn --version
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /opt/homebrew/Cellar/maven/3.9.9/libexec
Java version: 23.0.2, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk/23.0.2/libexec/openjdk.jdk/Contents/Home
Default locale: en_CO, platform encoding: UTF-8
OS name: "mac os x", version: "15.1", arch: "aarch64", family: "mac"

What did you do?

Steps to reproduce the behavior:

  1. Prepare a foo/v1/foo.proto file:
syntax = "proto3";

package foo.v1;

message Foo {
  // Sample docs
  // 
  // Some doc containing [invalid.] Kdoc...
  // 
  // should still [produce...valid] Kotlin code.
  // 
  // End
  string foo = 1;
}
  1. Run the kotlin plugin:
$ protoc -I=foo/v1 --kotlin_out=gen/kotlin foo/v1/foo.proto
  1. The generated code is now in gen/kotlin

What did you expect to see

Valid Kotlin code that compiles with maven.

What did you see instead?

Invalid Kotlin code that does not compile with syntax errors.

The generated code includes invalid KDoc within <pre> and <code> tags in L51-64, although it includes valid KDoc within backticks in L31-43. Probably missing to apply the patch/fix in that other public fun clear<Field>() too?

gen/kotlin/foo/v1/FooKt.kt

// Generated by the protocol buffer compiler. DO NOT EDIT!
// NO CHECKED-IN PROTOBUF GENCODE
// source: foo.proto

// Generated files should ignore deprecation warnings
@file:Suppress("DEPRECATION")
package foo.v1;

@kotlin.jvm.JvmName("-initializefoo")
public inline fun foo(block: foo.v1.FooKt.Dsl.() -> kotlin.Unit): foo.v1.FooOuterClass.Foo =
  foo.v1.FooKt.Dsl._create(foo.v1.FooOuterClass.Foo.newBuilder()).apply { block() }._build()
/**
 * Protobuf type `foo.v1.Foo`
 */
public object FooKt {
  @kotlin.OptIn(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)
  @com.google.protobuf.kotlin.ProtoDslMarker
  public class Dsl private constructor(
    private val _builder: foo.v1.FooOuterClass.Foo.Builder
  ) {
    public companion object {
      @kotlin.jvm.JvmSynthetic
    @kotlin.PublishedApi
      internal fun _create(builder: foo.v1.FooOuterClass.Foo.Builder): Dsl = Dsl(builder)
    }

    @kotlin.jvm.JvmSynthetic
  @kotlin.PublishedApi
    internal fun _build(): foo.v1.FooOuterClass.Foo = _builder.build()

    /**
     * ```
     * Sample docs
     *
     * Some doc containing [invalid.] Kdoc...
     *
     * should still [produce...valid] Kotlin code.
     *
     * End
     * ```
     *
     * `string foo = 1;`
     */
    public var foo: kotlin.String
      @kotlin.jvm.JvmName("getFoo")
        get() = _builder.foo
      @kotlin.jvm.JvmName("setFoo")
        set(value) {
        _builder.foo = value
      }
    /**
     * <pre>
     * Sample docs
     *
     * Some doc containing [invalid.] Kdoc...
     *
     * should still [produce...valid] Kotlin code.
     *
     * End
     * </pre>
     *
     * <code>string foo = 1;</code>
     * @return This builder for chaining.
     */
    public fun clearFoo() {
      _builder.clearFoo()
    }
  }
}
@kotlin.jvm.JvmSynthetic
public inline fun foo.v1.FooOuterClass.Foo.copy(block: `foo.v1`.FooKt.Dsl.() -> kotlin.Unit): foo.v1.FooOuterClass.Foo =
  `foo.v1`.FooKt.Dsl._create(this.toBuilder()).apply { block() }._build()


And if I copy both generated comments to a simple demo file and try to compile:

package com.example.demo

/**
 * ```
 * Sample docs
 *
 * Some doc containing [invalid.] Kdoc...
 *
 * should still [produce...valid] Kotlin code.
 *
 * End
 * ```
 *
 * `string foo = 1;`
 */

/**
 * <pre>
 * Sample docs
 *
 * Some doc containing [invalid.] Kdoc...
 *
 * should still [produce...valid] Kotlin code.
 *
 * End
 * </pre>
 *
 * <code>string foo = 1;</code>
 * @return This builder for chaining.
 */
fun main(args: Array<String>) {
  print("hello")
}

Then I get the error:

[ERROR] .../com/example/demo/DemoApplication.kt: (21, 33) Identifier expected
[ERROR] .../com/example/demo/DemoApplication.kt: (23, 25) Closing bracket expected
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.800 s
[INFO] Finished at: 2025-04-09T16:06:16-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.9.25:compile (compile) on project demo: Compilation failure: Compilation failure: 
[ERROR] .../com/example/demo/DemoApplication.kt:[21,33] Identifier expected
[ERROR] .../com/example/demo/DemoApplication.kt:[23,25] Closing bracket expected
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Anything else we should know about your project / environment

Exactly same issue reported in https://github.com/protocolbuffers/protobuf/issues/9427 but appeared again in a new version of the Kotlin protoc plugin.

unmultimedio avatar Apr 09 '25 21:04 unmultimedio

Experimenting with other protoc releases, all v30.x fail, while release v29.3 works. Perhaps the bug was re-introduced in the v30 release?

unmultimedio avatar Apr 09 '25 21:04 unmultimedio