Skip to content

Commit f844ffb

Browse files
authored
feat: rewrite Kotlin indexing using the Analysis API (#1044)
> Co-authored-by: Michal Kielbowicz
1 parent 1841b93 commit f844ffb

68 files changed

Lines changed: 5080 additions & 5871 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/renovate.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
"matchPackageNames": ["eclipse-temurin"],
3636
"allowedVersions": "/^25(?:[.-]|$)/",
3737
"automerge": false
38+
},
39+
{
40+
"description": "The Analysis API engine pin is managed manually: it lives in the JetBrains intellij-dependencies repository, uses IDE-aligned version schemes, and must move in lockstep with the `kotlin` toolchain version.",
41+
"matchPackageNames": [
42+
"org.jetbrains.kotlin:kotlin-compiler",
43+
"*-for-ide",
44+
"com.intellij.platform:*"
45+
],
46+
"enabled": false
3847
}
3948
]
4049
}

‎.github/workflows/ci.yaml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ jobs:
2222

2323
- run: gradle spotlessCheck --no-daemon
2424

25-
kotlin_plugin:
25+
kotlin_indexer:
2626
runs-on: ubuntu-latest
27-
name: scip-kotlinc
27+
name: scip-kotlin-analysis
2828
steps:
2929
- uses: actions/checkout@v7
3030

@@ -37,8 +37,8 @@ jobs:
3737
with:
3838
gradle-version: 9.8.2
3939

40-
- name: scip-kotlinc tests
41-
run: gradle :scip-kotlinc:test --no-daemon
40+
- name: scip-kotlin-analysis tests
41+
run: gradle :scip-kotlin-analysis:test --no-daemon
4242

4343
docker_test:
4444
runs-on: ubuntu-latest
@@ -69,7 +69,7 @@ jobs:
6969
7070
ci-pass:
7171
if: always()
72-
needs: [format, kotlin_plugin, docker_test]
72+
needs: [format, kotlin_indexer, docker_test]
7373
runs-on: ubuntu-latest
7474
name: CI pass
7575
steps:

‎.github/workflows/repos.yaml‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,39 @@ jobs:
119119
expected_language: kotlin
120120
covers: Gradle, Kotlin multiplatform JVM target, Kotlin 2.4.0, Java 25 runtime
121121

122+
- name: gradle-kotlin19-kotlinpoet
123+
repository: square/kotlinpoet
124+
ref: 1.16.0
125+
directory: .
126+
build_tool: gradle
127+
build_command: ":kotlinpoet:compileKotlinJvm"
128+
java: 17
129+
bazel_version: ""
130+
expected_language: kotlin
131+
covers: Gradle, Kotlin multiplatform JVM target, legacy Kotlin 1.9.22, Java 17 runtime
132+
133+
- name: gradle-kotlin23-turbine
134+
repository: cashapp/turbine
135+
ref: ab1e9a0acbbbb175081a0c5d94cb4f4cf4da444a
136+
directory: .
137+
build_tool: gradle
138+
build_command: "compileKotlinJvm"
139+
java: 17
140+
bazel_version: ""
141+
expected_language: kotlin
142+
covers: Gradle, Kotlin multiplatform JVM target, Kotlin 2.3.21, Java 17 runtime
143+
144+
- name: gradle-kotlin24-detekt
145+
repository: detekt/detekt
146+
ref: 04f97401d462e899383156b25d026b7b8c181401
147+
directory: .
148+
build_tool: gradle
149+
build_command: ":detekt-api:compileKotlin"
150+
java: 21
151+
bazel_version: ""
152+
expected_language: kotlin
153+
covers: Gradle, Kotlin JVM, Kotlin 2.4.0, settings repositories (FAIL_ON_PROJECT_REPOS), Java 21 runtime
154+
122155
- name: gradle-mixed-kotlinpoet
123156
repository: square/kotlinpoet
124157
ref: 0182a25089ee1f4f2378568b497f82a8ebc3017d

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ These are the main components of the project.
5454
| ------------------------------------------------------- | -------- | ----------------------------------------------------------------------- |
5555
| `gradle test --no-daemon` | terminal | Run all Gradle tests. |
5656
| `gradle :scip-java:test --no-daemon` | terminal | Run CLI build-tool integration tests (Gradle, Maven, SCIP config). |
57-
| `gradle :scip-kotlinc:test --no-daemon` | terminal | Run Kotlin compiler-plugin tests. |
57+
| `gradle :scip-kotlin-analysis:test --no-daemon` | terminal | Run Kotlin indexer tests. |
5858
| `gradle :scip-snapshots:test --no-daemon` | terminal | Compare Java and Kotlin snapshot goldens. |
5959
| `gradle :scip-snapshots:saveSnapshots --no-daemon` | terminal | Regenerate Java and Kotlin snapshot goldens. |
6060
| `gradle :scip-java:installDist --no-daemon` | terminal | Build a local `scip-java` distribution under `scip-java/build/install/`. |

‎build-logic/src/main/kotlin/org/scip_code/scip_java/buildlogic/ScipCompile.kt‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import java.io.File
44
import org.gradle.api.Task
55
import org.gradle.api.artifacts.Configuration
66
import org.gradle.api.file.Directory
7-
import org.gradle.api.file.FileSystemLocation
87
import org.gradle.api.provider.Provider
98
import org.gradle.api.tasks.compile.JavaCompile
109

@@ -27,29 +26,6 @@ fun JavaCompile.useScipJavac(
2726
(options.forkOptions.jvmArgs ?: emptyList()) + JavacInternals.jvmOptions(rootDir)
2827
}
2928

30-
/**
31-
* Builds the `kotlinc` arguments that load the scip-kotlinc compiler plugin from [pluginClasspath]
32-
* (the resolved shaded jar) and point it at [sourceroot]/[targetroot].
33-
*
34-
* The mapping lives here, in compiled build logic, rather than in a build script: a `.map {}`
35-
* lambda declared in a `.gradle.kts` file captures a hidden reference to the script object, which
36-
* the configuration cache cannot serialize.
37-
*/
38-
fun scipKotlincPluginArgs(
39-
pluginClasspath: Provider<Set<FileSystemLocation>>,
40-
sourceroot: String,
41-
targetroot: String,
42-
): Provider<List<String>> =
43-
pluginClasspath.map { locations ->
44-
listOf(
45-
"-Xplugin=${locations.single().asFile.absolutePath}",
46-
"-P",
47-
"plugin:scip-kotlinc:sourceroot=$sourceroot",
48-
"-P",
49-
"plugin:scip-kotlinc:targetroot=$targetroot",
50-
)
51-
}
52-
5329
/**
5430
* Registers a `doFirst` action that empties [dir] (deletes then recreates it) before the task runs.
5531
*

‎gradle/libs.versions.toml‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ clikt = "5.1.0"
33
gradle-api = "8.11.1"
44
guava = "33.7.2-jre"
55
junit-jupiter = "5.11.4"
6-
kctfork = "0.14.0"
7-
kotest = "6.2.6"
86
kotlin = "2.4.21"
7+
kotlin-analysis-api = "2.4.0"
8+
kotlinx-collections-immutable = "0.3.8"
9+
kotlinx-coroutines-intellij = "1.8.0-intellij-13"
910
kotlinx-serialization = "1.12.0"
1011
lombok = "1.18.48"
1112
maven-plugin-annotations = "3.16.0"
@@ -19,21 +20,25 @@ spotless = "8.11.0"
1920
vanniktech-maven-publish = "0.37.0"
2021

2122
[libraries]
23+
caffeine = { module = "com.github.ben-manes.caffeine:caffeine", version = "2.9.3" }
2224
clikt-jvm = { module = "com.github.ajalt.clikt:clikt-jvm", version.ref = "clikt" }
2325
gradle-api = { module = "dev.gradleplugins:gradle-api", version.ref = "gradle-api" }
2426
gradle-test-kit = { module = "dev.gradleplugins:gradle-test-kit", version.ref = "gradle-api" }
2527
guava = { module = "com.google.guava:guava", version.ref = "guava" }
26-
kctfork-core = { module = "dev.zacsweers.kctfork:core", version.ref = "kctfork" }
27-
kotest-assertions-core = { module = "io.kotest:kotest-assertions-core-jvm", version.ref = "kotest" }
28-
kotlin-compiler-embeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-embeddable", version.ref = "kotlin" }
29-
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
30-
kotlin-scripting-common = { module = "org.jetbrains.kotlin:kotlin-scripting-common", version.ref = "kotlin" }
31-
kotlin-scripting-dependencies = { module = "org.jetbrains.kotlin:kotlin-scripting-dependencies", version.ref = "kotlin" }
32-
kotlin-scripting-dependencies-maven = { module = "org.jetbrains.kotlin:kotlin-scripting-dependencies-maven", version.ref = "kotlin" }
33-
kotlin-scripting-jvm = { module = "org.jetbrains.kotlin:kotlin-scripting-jvm", version.ref = "kotlin" }
28+
kotlin-analysis-api-api = { module = "org.jetbrains.kotlin:analysis-api-for-ide", version.ref = "kotlin-analysis-api" }
29+
kotlin-analysis-api-impl-base = { module = "org.jetbrains.kotlin:analysis-api-impl-base-for-ide", version.ref = "kotlin-analysis-api" }
30+
kotlin-analysis-api-k2 = { module = "org.jetbrains.kotlin:analysis-api-k2-for-ide", version.ref = "kotlin-analysis-api" }
31+
kotlin-analysis-api-platform-interface = { module = "org.jetbrains.kotlin:analysis-api-platform-interface-for-ide", version.ref = "kotlin-analysis-api" }
32+
kotlin-analysis-api-standalone = { module = "org.jetbrains.kotlin:analysis-api-standalone-for-ide", version.ref = "kotlin-analysis-api" }
33+
kotlin-analysis-compiler = { module = "org.jetbrains.kotlin:kotlin-compiler", version.ref = "kotlin-analysis-api" }
34+
kotlin-analysis-compiler-common = { module = "org.jetbrains.kotlin:kotlin-compiler-common-for-ide", version.ref = "kotlin-analysis-api" }
35+
kotlin-analysis-low-level-api-fir = { module = "org.jetbrains.kotlin:low-level-api-fir-for-ide", version.ref = "kotlin-analysis-api" }
36+
kotlin-analysis-symbol-light-classes = { module = "org.jetbrains.kotlin:symbol-light-classes-for-ide", version.ref = "kotlin-analysis-api" }
3437
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
3538
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
3639
kotlin-test-junit5 = { module = "org.jetbrains.kotlin:kotlin-test-junit5", version.ref = "kotlin" }
40+
kotlinx-collections-immutable-jvm = { module = "org.jetbrains.kotlinx:kotlinx-collections-immutable-jvm", version.ref = "kotlinx-collections-immutable" }
41+
kotlinx-coroutines-core-jvm = { module = "com.intellij.platform:kotlinx-coroutines-core-jvm", version.ref = "kotlinx-coroutines-intellij" }
3742
kotlinx-serialization-json-jvm = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json-jvm", version.ref = "kotlinx-serialization" }
3843
lombok = { module = "org.projectlombok:lombok", version.ref = "lombok" }
3944
maven-plugin-annotations = { module = "org.apache.maven.plugin-tools:maven-plugin-annotations", version.ref = "maven-plugin-annotations" }

‎scip-gradle-plugin/src/main/java/org/scip_code/scip_java/gradle/ScipGradlePlugin.java‎

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.scip_code.scip_java.gradle;
22

3+
import java.io.File;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
37
import java.util.ArrayList;
48
import java.util.HashSet;
59
import java.util.List;
@@ -9,6 +13,7 @@
913
import org.gradle.api.Project;
1014
import org.gradle.api.Task;
1115
import org.gradle.api.artifacts.Configuration;
16+
import org.gradle.api.file.FileCollection;
1217
import org.gradle.api.initialization.Settings;
1318
import org.gradle.api.internal.GradleInternal;
1419
import org.gradle.api.invocation.Gradle;
@@ -130,15 +135,14 @@ private void configureProject(Project project) {
130135
triggers.add("compileKotlin");
131136
triggers.add("compileTestKotlin");
132137
}
133-
134-
// The CLI's init script provides the path of the embedded scip-kotlinc jar.
135-
Object scipKotlinc = requiredExtra(extraProperties, "scipKotlincJar");
136-
project
137-
.getTasks()
138-
.configureEach(
139-
task -> configureKotlinCompileTask(task, scipKotlinc, sourceRoot, targetRoot));
140138
}
141139

140+
// Not limited to the plugin checks above: AGP 9's built-in Kotlin adds
141+
// Kotlin compile tasks to Android modules without applying a Kotlin plugin.
142+
project
143+
.getTasks()
144+
.configureEach(task -> configureKotlinCompileTask(task, sourceRoot, targetRoot));
145+
142146
project.getTasks().create("scipCompileAll").dependsOn(triggers);
143147

144148
project.getTasks().create("scipPrintDependencies", WriteDependencies.class);
@@ -232,39 +236,72 @@ private static Set<String> addJavacPlugin(Project project, Object javacPluginDep
232236
return pluginTasks;
233237
}
234238

235-
private static void configureKotlinCompileTask(
236-
Task task, Object scipKotlinc, String sourceRoot, String targetRoot) {
239+
/**
240+
* Kotlin sources are not indexed inside kotlinc: after each Kotlin compile task runs, its sources
241+
* and classpath are dumped to {@code <targetroot>/kotlin-configs/<task>.txt} for the scip-java
242+
* CLI to index after the build, decoupling indexing from the build's Kotlin compiler version.
243+
*/
244+
private static void configureKotlinCompileTask(Task task, String sourceRoot, String targetRoot) {
237245
if (!task.getClass().getSimpleName().contains("KotlinCompile")) {
238246
return;
239247
}
240248

241-
// Referring to KotlinCompile directly here triggers NoClassDefFoundError -
242-
// the plugin classpath is murky and we deliberately don't bundle the Kotlin
243-
// Gradle plugin. So we commit the sins of reflection for our limited needs.
244-
try {
245-
Object kotlinOptions = task.getClass().getMethod("getKotlinOptions").invoke(task);
246-
247-
@SuppressWarnings("unchecked")
248-
List<String> freeCompilerArgs =
249-
(List<String>)
250-
kotlinOptions.getClass().getMethod("getFreeCompilerArgs").invoke(kotlinOptions);
251-
252-
List<String> newArgs = new ArrayList<>(freeCompilerArgs.size() + 5);
253-
newArgs.addAll(freeCompilerArgs);
254-
newArgs.add("-Xplugin=" + scipKotlinc);
255-
newArgs.add("-P");
256-
newArgs.add("plugin:scip-kotlinc:sourceroot=" + sourceRoot);
257-
newArgs.add("-P");
258-
newArgs.add("plugin:scip-kotlinc:targetroot=" + targetRoot);
259-
260-
kotlinOptions
261-
.getClass()
262-
.getMethod("setFreeCompilerArgs", List.class)
263-
.invoke(kotlinOptions, newArgs);
264-
} catch (ReflectiveOperationException exc) {
265-
throw new RuntimeException(
266-
"scip-java: failed to configure Kotlin compile task '" + task.getName() + "'", exc);
267-
}
249+
task.doLast(
250+
ignored -> {
251+
try {
252+
List<String> lines = new ArrayList<>();
253+
lines.add("sourceroot " + sourceRoot);
254+
255+
// Referring to KotlinCompile directly triggers NoClassDefFoundError -
256+
// the plugin classpath is murky and we deliberately don't bundle the
257+
// Kotlin Gradle plugin. So we commit the sins of reflection.
258+
FileCollection libraries;
259+
try {
260+
libraries = (FileCollection) task.getClass().getMethod("getLibraries").invoke(task);
261+
} catch (ReflectiveOperationException exc) {
262+
libraries = (FileCollection) task.getClass().getMethod("getClasspath").invoke(task);
263+
}
264+
for (File entry : libraries.getFiles()) {
265+
lines.add("classpath " + entry.getAbsolutePath());
266+
}
267+
268+
int sources = 0;
269+
for (File file : task.getInputs().getSourceFiles().getFiles()) {
270+
if (file.getName().endsWith(".kt") || file.getName().endsWith(".kts")) {
271+
lines.add("source " + file.getAbsolutePath());
272+
sources++;
273+
}
274+
}
275+
// Java sources of the same module are listed too, so Kotlin
276+
// references to them resolve. The indexer only emits Kotlin files.
277+
try {
278+
FileCollection javaSources =
279+
(FileCollection) task.getClass().getMethod("getJavaSources").invoke(task);
280+
for (File file : javaSources.getFiles()) {
281+
if (file.getName().endsWith(".java")) {
282+
lines.add("source " + file.getAbsolutePath());
283+
}
284+
}
285+
} catch (ReflectiveOperationException exc) {
286+
// Kotlin Gradle Plugin without javaSources (pre 1.7).
287+
}
288+
if (sources == 0) {
289+
return;
290+
}
291+
292+
Path configDir = Paths.get(targetRoot, "kotlin-configs");
293+
Files.createDirectories(configDir);
294+
String fileName = task.getPath().replaceAll("[^A-Za-z0-9]", "-") + ".txt";
295+
Files.write(configDir.resolve(fileName), lines);
296+
} catch (Exception exc) {
297+
throw new RuntimeException(
298+
"scip-java: failed to extract Kotlin compile configuration for task '"
299+
+ task.getName()
300+
+ "': "
301+
+ exc,
302+
exc);
303+
}
304+
});
268305
}
269306

270307
private static Object requiredExtra(Map<String, Object> extraProperties, String name) {

‎scip-java/build.gradle.kts‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@ description = "Java and Kotlin indexer for SCIP"
1313

1414
val javacShadowJar = shadowJarArtifact(":scip-javac", "javacShadowJar")
1515
val gradlePluginShadowJar = shadowJarArtifact(":scip-gradle-plugin", "gradlePluginShadowJar")
16-
val kotlincShadowJar = shadowJarArtifact(":scip-kotlinc", "kotlincShadowJar")
1716

1817
dependencies {
1918
implementation(project(":scip-aggregator"))
19+
implementation(project(":scip-kotlin-analysis"))
2020
implementation(libs.clikt.jvm)
2121
implementation(libs.kotlin.stdlib)
22-
implementation(libs.kotlin.compiler.embeddable)
23-
implementation(libs.kotlin.scripting.common)
24-
implementation(libs.kotlin.scripting.jvm)
25-
implementation(libs.kotlin.scripting.dependencies)
26-
implementation(libs.kotlin.scripting.dependencies.maven)
2722
implementation(libs.kotlinx.serialization.json.jvm)
2823

2924
testImplementation(libs.kotlin.test)
@@ -46,9 +41,6 @@ val generateEmbeddedResources = tasks.register<Sync>("generateEmbeddedResources"
4641
from(gradlePluginShadowJar) {
4742
rename { "gradle-plugin.jar" }
4843
}
49-
from(kotlincShadowJar) {
50-
rename { "scip-kotlinc.jar" }
51-
}
5244
into(layout.buildDirectory.dir("generated/resources/embedded"))
5345
}
5446

‎scip-java/src/main/kotlin/org/scip_code/scip_java/Embedded.kt‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ object Embedded {
2525

2626
fun gradlePluginJar(tmpDir: Path): Path = copyFile(tmpDir, "gradle-plugin.jar")
2727

28-
fun scipKotlincJar(tmpDir: Path): Path = copyFile(tmpDir, "scip-kotlinc.jar")
29-
3028
private fun javacErrorpath(tmp: Path): Path = tmp.resolve("errorpath.txt")
3129

3230
data class CustomJavac(val executable: Path, val environment: Map<String, String>)

‎scip-java/src/main/kotlin/org/scip_code/scip_java/ScipJava.kt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.scip_code.scip_java
22

33
import java.io.PrintStream
4+
import kotlin.system.exitProcess
45

56
/**
67
* Public entry point for the scip-java CLI. The single [app] instance is shared across test suites
@@ -17,6 +18,8 @@ object ScipJava {
1718
@JvmStatic
1819
fun main(args: Array<String>) {
1920
app.runAndExitIfNonZero(args.toList())
21+
// The Analysis API indexer leaves non-daemon threads behind; exit explicitly.
22+
exitProcess(0)
2023
}
2124

2225
fun printHelp(out: PrintStream) {

0 commit comments

Comments
 (0)