-
Notifications
You must be signed in to change notification settings - Fork 586
Add Wire Binary Compatibility plugin #3306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HLGreer
wants to merge
11
commits into
master
Choose a base branch
from
wire-binary-compatibility
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fe5b6f0
Add Wire Binary Compatibility plugin
HLGreer 8bf29b0
Update class name, use specific imports, remove Xcode by-pass from se…
HLGreer 30528fc
Add documentation comment to WireBinaryCompatibilityIrGenerationExten…
HLGreer cfd8a4b
Add readme with example
HLGreer cabbb63
spotlessApply
HLGreer 5ec88c2
Add simple BuildConfig object with GradlePluginArtifacts
HLGreer af75cfc
Use CompileOnly for gradle-plugin-api, wbc plugin
HLGreer b6bcc37
Use compileOnly for kotlin.gradlePlugin
HLGreer 54295d9
Generate BuildConfig for the plugin
HLGreer 3df3a26
Update wire-binary-compatibility-kotlin-plugin/src/main/kotlin/com/sq…
HLGreer 88ef84b
Use a testImplementation of 'gradle-plugin-api' during tests, and com…
HLGreer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Wire Binary Compatibility Plugin | ||
|
||
The Wire Binary Compatibility Kotlin compiler plugin adapts Wire-generated callsites to be more resilient | ||
to schema changes. | ||
|
||
## Current Support | ||
Generated constructor callsites are rewritten at compile-time to instead use the Builder. | ||
Generated copy() callsites are not yet supported. | ||
|
||
## Example | ||
Given a generated class, Dinosaur, from a proto defintion: | ||
```protobuf | ||
package com.squareup.dinosaurs; | ||
|
||
message Dinosaur { | ||
optional string name = 1; | ||
optional double avg_length_meters = 2; | ||
} | ||
``` | ||
|
||
With existing usage: | ||
```kotlin | ||
val newDinosaur = Dinosaur( | ||
name = "triceratops", | ||
avg_length_meters = 9.0, | ||
) | ||
``` | ||
|
||
When a new field is added to the Dinosaur schema, if there are competing versions of the compiled class and the new | ||
version is resolved at runtime, the usage above may encounter an error: | ||
``` | ||
java.lang.NoSuchMethodError: 'void com.squareup.dinosaurs.Dinosaur<init>(java.lang.String, java.lang.Double, | ||
{new_field})' | ||
``` | ||
|
||
Using the Wire Binary Compatibility Plugin will adapt the compiled binary to use the equivalent Builder: | ||
```kotlin | ||
val newDinosaur = Dinosaur.Builder() | ||
.name("triceratops") | ||
.avg_length_meters(9.0) | ||
.build() | ||
``` | ||
The rewritten, compiled code is backwards compatible with the new field. |
swankjesse marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import com.vanniktech.maven.publish.GradlePlugin | ||
import com.vanniktech.maven.publish.JavadocJar | ||
import com.vanniktech.maven.publish.MavenPublishBaseExtension | ||
|
||
plugins { | ||
id("java-gradle-plugin") | ||
`kotlin-dsl` | ||
kotlin("jvm") | ||
id("com.github.gmazzo.buildconfig") | ||
id("org.jetbrains.dokka") | ||
id("com.vanniktech.maven.publish.base") | ||
} | ||
|
||
dependencies { | ||
compileOnly(kotlin("gradle-plugin-api")) | ||
compileOnly(libs.kotlin.gradlePlugin) | ||
implementation(projects.wireBinaryCompatibilityKotlinPlugin) | ||
} | ||
|
||
buildConfig { | ||
useKotlinOutput { | ||
internalVisibility = true | ||
} | ||
val compilerPlugin = projects.wireBinaryCompatibilityKotlinPlugin | ||
val packageName = "com.squareup.wire.binarycompatibility.gradle" | ||
packageName(packageName) | ||
buildConfigField("String", "KOTLIN_PLUGIN_ID", "\"${packageName}\"") | ||
buildConfigField("String", "KOTLIN_PLUGIN_GROUP", "\"${compilerPlugin.group}\"") | ||
buildConfigField("String", "KOTLIN_PLUGIN_NAME", "\"${compilerPlugin.name}\"") | ||
buildConfigField("String", "KOTLIN_PLUGIN_VERSION", "\"${compilerPlugin.version}\"") | ||
} | ||
|
||
gradlePlugin { | ||
plugins { | ||
create("wireBinaryCompatibility") { | ||
id = "com.squareup.wire.binarycompatibility" | ||
displayName = "Wire Binary Compatibility" | ||
description = "Rewrites Wire callsites to be resilient to API changes" | ||
implementationClass = "com.squareup.wire.binarycompatibility.gradle.WireBinaryCompatibility" | ||
} | ||
} | ||
} | ||
|
||
configure<MavenPublishBaseExtension> { | ||
configure( | ||
GradlePlugin( | ||
javadocJar = JavadocJar.Empty() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why publish an empty javadoc jar? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It isn’t a library for external use, but Maven Central requires Javadoc |
||
) | ||
) | ||
} |
43 changes: 43 additions & 0 deletions
43
...main/kotlin/com/squareup/wire/binarycompatibility/gradle/WireBinaryCompatibilityPlugin.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2025 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.wire.binarycompatibility.gradle | ||
|
||
import org.gradle.api.provider.Provider | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin | ||
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact | ||
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption | ||
|
||
@Suppress("unused") // Created reflectively by Gradle. | ||
class WireBinaryCompatibilityPlugin : KotlinCompilerPluginSupportPlugin { | ||
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean = true | ||
|
||
override fun getCompilerPluginId(): String = "com.squareup.wire.binarycompatibility.kotlin" | ||
|
||
override fun getPluginArtifact(): SubpluginArtifact = SubpluginArtifact( | ||
groupId = BuildConfig.KOTLIN_PLUGIN_GROUP, | ||
artifactId = BuildConfig.KOTLIN_PLUGIN_NAME, | ||
version = BuildConfig.KOTLIN_PLUGIN_VERSION, | ||
) | ||
|
||
override fun applyToCompilation( | ||
kotlinCompilation: KotlinCompilation<*>, | ||
): Provider<List<SubpluginOption>> { | ||
return kotlinCompilation.target.project.provider { | ||
listOf() // No options. | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
wire-binary-compatibility-kotlin-plugin-tests/build.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
plugins { | ||
kotlin("jvm") | ||
id("org.jetbrains.dokka") | ||
} | ||
|
||
dependencies { | ||
testImplementation(kotlin("gradle-plugin-api")) | ||
testImplementation(project(":wire-binary-compatibility-kotlin-plugin")) | ||
testImplementation(kotlin("compiler-embeddable")) | ||
testImplementation(kotlin("test-junit")) | ||
testImplementation(libs.assertk) | ||
testImplementation(libs.kotlin.compile.testing) | ||
} |
125 changes: 125 additions & 0 deletions
125
...t/kotlin/com/squareup/wire/binarycompatibility/WireBinaryCompatibilityKotlinPluginTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright (C) 2025 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.wire.binarycompatibility.kotlin | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.contains | ||
import assertk.assertions.containsExactly | ||
import com.tschuchort.compiletesting.JvmCompilationResult | ||
import com.tschuchort.compiletesting.KotlinCompilation | ||
import com.tschuchort.compiletesting.SourceFile | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar | ||
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi | ||
|
||
@OptIn(ExperimentalCompilerApi::class) | ||
class WireBinaryCompatibilityKotlinPluginTest { | ||
@Test | ||
fun rewriteConstructorCallToBuilderCall() { | ||
val result = compile( | ||
sourceFile = SourceFile.kotlin( | ||
"Sample.kt", | ||
""" | ||
package com.squareup.wire | ||
|
||
val log = mutableListOf<String>() | ||
|
||
fun callConstructor() { | ||
log += "${'$'}{Money(5, "USD")}" | ||
} | ||
|
||
fun callConstructorWithDefaultParameters() { | ||
log += "${'$'}{Money(amount = 5)}" | ||
log += "${'$'}{Money(currencyCode = "USD")}" | ||
} | ||
|
||
data class Money( | ||
val amount: Long? = null, | ||
val currencyCode: String? = null, | ||
) : Message { | ||
|
||
class Builder : Message.Builder { | ||
var amount: Long? = null | ||
var currencyCode: String? = null | ||
|
||
fun amount(amount: Long) : Builder { | ||
log += "calling amount()!" | ||
this.amount = amount | ||
return this | ||
} | ||
|
||
fun currencyCode(currencyCode: String) : Builder { | ||
log += "calling currencyCode()!" | ||
this.currencyCode = currencyCode | ||
return this | ||
} | ||
fun build() : Money = Money(amount, currencyCode) | ||
} | ||
} | ||
|
||
interface Message { | ||
interface Builder | ||
} | ||
""", | ||
), | ||
) | ||
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode, result.messages) | ||
|
||
val testClass = result.classLoader.loadClass("com.squareup.wire.SampleKt") | ||
val log = testClass.getMethod("getLog") | ||
.invoke(null) as MutableList<String> | ||
|
||
testClass.getMethod("callConstructor").invoke(null) | ||
assertThat(log).containsExactly( | ||
"calling amount()!", | ||
"calling currencyCode()!", | ||
"Money(amount=5, currencyCode=USD)", | ||
) | ||
log.clear() | ||
|
||
testClass.getMethod("callConstructorWithDefaultParameters").invoke(null) | ||
assertThat(log).containsExactly( | ||
"calling amount()!", | ||
"Money(amount=5, currencyCode=null)", | ||
"calling currencyCode()!", | ||
"Money(amount=null, currencyCode=USD)", | ||
) | ||
log.clear() | ||
} | ||
} | ||
|
||
@ExperimentalCompilerApi | ||
fun compile( | ||
sourceFiles: List<SourceFile>, | ||
plugin: CompilerPluginRegistrar = WireBinaryCompatibilityCompilerPluginRegistrar(), | ||
): JvmCompilationResult { | ||
return KotlinCompilation().apply { | ||
sources = sourceFiles | ||
compilerPluginRegistrars = listOf(plugin) | ||
inheritClassPath = true | ||
kotlincArguments += "-Xverify-ir=error" | ||
kotlincArguments += "-Xverify-ir-visibility" | ||
}.compile() | ||
} | ||
|
||
@ExperimentalCompilerApi | ||
fun compile( | ||
sourceFile: SourceFile, | ||
plugin: CompilerPluginRegistrar = WireBinaryCompatibilityCompilerPluginRegistrar(), | ||
): JvmCompilationResult { | ||
return compile(listOf(sourceFile), plugin) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import com.vanniktech.maven.publish.JavadocJar | ||
import com.vanniktech.maven.publish.KotlinJvm | ||
import com.vanniktech.maven.publish.MavenPublishBaseExtension | ||
|
||
plugins { | ||
kotlin("jvm") | ||
id("com.github.gmazzo.buildconfig") | ||
id("com.vanniktech.maven.publish.base") | ||
} | ||
|
||
dependencies { | ||
compileOnly(kotlin("compiler-embeddable")) | ||
compileOnly(kotlin("stdlib")) | ||
} | ||
|
||
buildConfig { | ||
useKotlinOutput { | ||
internalVisibility = true | ||
} | ||
|
||
packageName("com.squareup.wire.binarycompatibility.kotlin") | ||
buildConfigField("String", "KOTLIN_PLUGIN_ID", "\"com.squareup.wire.binarycompatibility.kotlin\"") | ||
} | ||
|
||
configure<MavenPublishBaseExtension> { | ||
configure( | ||
KotlinJvm( | ||
javadocJar = JavadocJar.Empty() | ||
) | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# We want the stdlib as a compileOnly dependency. | ||
kotlin.stdlib.default.dependency=false |
27 changes: 27 additions & 0 deletions
27
...m/squareup/wire/binarycompatibility/kotlin/WireBinaryCompatibilityCommandLineProcessor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (C) 2025 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.wire.binarycompatibility.kotlin | ||
|
||
import org.jetbrains.kotlin.compiler.plugin.CliOption | ||
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor | ||
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi | ||
|
||
@OptIn(ExperimentalCompilerApi::class) | ||
class WireBinaryCompatibilityCommandLineProcessor : CommandLineProcessor { | ||
override val pluginId = "com.squareup.wire.binarycompatibility.kotlin" | ||
|
||
override val pluginOptions: List<CliOption> = listOf() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YASS!