square / wire

gRPC and protocol buffers for Android, Kotlin, Swift and Java.
https://square.github.io/wire/
Apache License 2.0
4.23k stars 572 forks source link

JavaScript name hashCode is generated for different inherited members [Kotlin Multiplatform] [JS] #2948

Open xit0c opened 3 months ago

xit0c commented 3 months ago

In a Kotlin Multiplatform project that targets JS, a Wire generated class shows this error in its constructor: JavaScript name hashCode is generated for different inherited members: var hashCode: Int and fun hashCode(): Int.

image

Setting buildersOnly = true in the plugin configuration (as shown in Wire's js sample) still gives the same error.

Fortunately, the build is not broken by that error and the class can still be used without problems, but since I noticed it I thought it was worth reporting it.

Maybe this is a duplicate of #1424, but that issue doesn't have any progress and it is inactive since 2020.

Reproduction Steps

Environment

IDE: Android Studio Jellyfish | 2023.3.1 Kotlin Version: 1.9.24 Wire Version: both 4.9.9 and 5.0.0-alpha01 (I didn't test older versions)

oldergod commented 3 months ago

What's your idea for a fix?

xit0c commented 3 months ago

I don't know wire's internals but it seems it's happening because of a conflict with Message's hashcode property.

https://github.com/square/wire/blob/b20024e04504df1578462fbae3eb827892d0d6bd/wire-runtime/src/commonMain/kotlin/com/squareup/wire/Message.kt#L28

I see that in the js implementation @JsName is used to override its name, so maybe it's the annotation that is not working. Any thoughts on that?

https://github.com/square/wire/blob/b20024e04504df1578462fbae3eb827892d0d6bd/wire-runtime/src/jsMain/kotlin/com/squareup/wire/Message.kt#L34

A possible solution could be to rename that property to something like internalHashcode (or cachedHashCode as in the annotation) and at that point the error should be gone and the annotation won't even be needed anymore. But again, since I don't know wire's internals and therefore the implications this little refactor could have, maybe this solution isn't feasible.