-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete the ksp caches when the task is restored from cache
Without this change, the task's caches will be in an incorrect state and the next incremental run can lead to compile errors. Now the caches have been marked as local state and Gradle will remove them when the task is restored from the build cache. This fixes #2042
- Loading branch information
Showing
12 changed files
with
203 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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
47 changes: 47 additions & 0 deletions
47
integration-tests/src/test/kotlin/com/google/devtools/ksp/test/BuildCacheIncrementalIT.kt
This file contains 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,47 @@ | ||
package com.google.devtools.ksp.test | ||
|
||
import org.gradle.testkit.runner.GradleRunner | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
import java.io.File | ||
|
||
@RunWith(Parameterized::class) | ||
class BuildCacheIncrementalIT(useKSP2: Boolean) { | ||
@Rule | ||
@JvmField | ||
val project: TemporaryTestProject = TemporaryTestProject("buildcache-incremental", useKSP2 = useKSP2) | ||
|
||
// See https://github.com/google/ksp/issues/2042 for details | ||
@Test | ||
fun testIncrementalBuildCache() { | ||
val buildCacheDir = File(project.root, "build-cache").absolutePath.replace(File.separatorChar, '/') | ||
File(project.root, "gradle.properties").appendText("\nbuildCacheDir=$buildCacheDir") | ||
|
||
val gradleRunner = GradleRunner.create().withProjectDir(project.root) | ||
val k1 = "workload/src/main/kotlin/p1/K1.kt" | ||
val k2 = "workload/src/main/kotlin/p1/K2.kt" | ||
|
||
gradleRunner.withArguments("assemble").build() | ||
|
||
File(project.root, k2).writeText( | ||
"package p1\n\n@MyAnnotation\nclass K2\n" | ||
) | ||
gradleRunner.withArguments("assemble").build() | ||
|
||
File(project.root, k2).delete() | ||
gradleRunner.withArguments("assemble").build() | ||
|
||
File(project.root, k1).writeText( | ||
"package p1\n\nclass K1(val foo: String)\n" | ||
) | ||
gradleRunner.withArguments("assemble").build() | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters(name = "KSP2={0}") | ||
fun params() = listOf(arrayOf(true), arrayOf(false)) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
integration-tests/src/test/resources/buildcache-incremental/build.gradle.kts
This file contains 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,8 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} |
1 change: 1 addition & 0 deletions
1
integration-tests/src/test/resources/buildcache-incremental/gradle.properties
This file contains 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 @@ | ||
org.gradle.caching=true |
27 changes: 27 additions & 0 deletions
27
integration-tests/src/test/resources/buildcache-incremental/settings.gradle.kts
This file contains 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 @@ | ||
pluginManagement { | ||
val kotlinVersion: String by settings | ||
val kspVersion: String by settings | ||
val testRepo: String by settings | ||
plugins { | ||
id("com.google.devtools.ksp") version kspVersion | ||
kotlin("jvm") version kotlinVersion | ||
} | ||
repositories { | ||
maven(testRepo) | ||
gradlePluginPortal() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} | ||
} | ||
|
||
buildCache { | ||
val buildCacheDir: String by settings | ||
local { | ||
directory = File(buildCacheDir) | ||
removeUnusedEntriesAfterDays = 30 | ||
} | ||
} | ||
|
||
rootProject.name = "playground" | ||
|
||
include(":workload") | ||
include(":test-processor") |
24 changes: 24 additions & 0 deletions
24
integration-tests/src/test/resources/buildcache-incremental/test-processor/build.gradle.kts
This file contains 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,24 @@ | ||
val kspVersion: String by project | ||
val testRepo: String by project | ||
|
||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
group = "com.example" | ||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
maven(testRepo) | ||
mavenCentral() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib")) | ||
implementation("com.google.devtools.ksp:symbol-processing-api:$kspVersion") | ||
} | ||
|
||
sourceSets.main { | ||
java.srcDirs("src/main/kotlin") | ||
} |
50 changes: 50 additions & 0 deletions
50
...esources/buildcache-incremental/test-processor/src/main/kotlin/TestBuildCacheProcessor.kt
This file contains 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.google.devtools.ksp.processing.* | ||
import com.google.devtools.ksp.symbol.* | ||
import com.google.devtools.ksp.validate | ||
import java.io.OutputStreamWriter | ||
|
||
class TestBuildCacheProcessor : SymbolProcessor { | ||
lateinit var codeGenerator: CodeGenerator | ||
lateinit var logger: KSPLogger | ||
|
||
fun init( | ||
options: Map<String, String>, | ||
kotlinVersion: KotlinVersion, | ||
codeGenerator: CodeGenerator, | ||
logger: KSPLogger, | ||
) { | ||
this.codeGenerator = codeGenerator | ||
this.logger = logger | ||
} | ||
|
||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
resolver.getSymbolsWithAnnotation("p1.MyAnnotation").forEach { decl -> | ||
decl as KSClassDeclaration | ||
|
||
val pkg = decl.packageName.asString() | ||
val name = decl.simpleName.asString() | ||
val generated = name + "Generated" | ||
val output = codeGenerator.createNewFile( | ||
Dependencies(false, decl.containingFile!!), | ||
pkg, generated | ||
) | ||
OutputStreamWriter(output).use { | ||
it.write("package $pkg\n\nclass $generated(val className: String = $name::class.java.simpleName)\n") | ||
} | ||
} | ||
resolver.getNewFiles().forEach { | ||
it.validate() | ||
} | ||
return emptyList() | ||
} | ||
} | ||
|
||
class TestBuildCacheProcessorProvider : SymbolProcessorProvider { | ||
override fun create( | ||
env: SymbolProcessorEnvironment, | ||
): SymbolProcessor { | ||
return TestBuildCacheProcessor().apply { | ||
init(env.options, env.kotlinVersion, env.codeGenerator, env.logger) | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
This file contains 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 @@ | ||
TestBuildCacheProcessorProvider |
19 changes: 19 additions & 0 deletions
19
integration-tests/src/test/resources/buildcache-incremental/workload/build.gradle.kts
This file contains 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,19 @@ | ||
val testRepo: String by project | ||
|
||
plugins { | ||
id("com.google.devtools.ksp") | ||
kotlin("jvm") | ||
} | ||
|
||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
maven(testRepo) | ||
mavenCentral() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib")) | ||
ksp(project(":test-processor")) | ||
} |
4 changes: 4 additions & 0 deletions
4
...gration-tests/src/test/resources/buildcache-incremental/workload/src/main/kotlin/p1/K1.kt
This file contains 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,4 @@ | ||
package p1 | ||
|
||
@MyAnnotation | ||
class K1 |
3 changes: 3 additions & 0 deletions
3
...sts/src/test/resources/buildcache-incremental/workload/src/main/kotlin/p1/MyAnnotation.kt
This file contains 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,3 @@ | ||
package p1 | ||
|
||
annotation class MyAnnotation |