Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.scip_code.scip_java.gradle;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -271,7 +274,8 @@ private static Set<String> addJavacPlugin(Project project, Object javacPluginDep
}

/**
* Dumps the sources and classpath of a Kotlin compile task to {@code kotlin-configs/<task>.txt}.
* Dumps the sources and classpath of a Kotlin compile task to {@code
* kotlin-configs/<task>-<hash>.txt}.
*/
private static void writeKotlinConfig(Task task, String sourceRoot, String targetRoot) {
try {
Expand Down Expand Up @@ -317,7 +321,14 @@ private static void writeKotlinConfig(Task task, String sourceRoot, String targe

Path configDir = Paths.get(targetRoot, "kotlin-configs");
Files.createDirectories(configDir);
String fileName = task.getPath().replaceAll("[^A-Za-z0-9]", "-") + ".txt";
// Task paths are only unique within one build, and every build of a
// composite (included builds, buildSrc) writes here, so the build's
// root directory is part of the identity. Hashing, unlike sanitizing,
// keeps ":a-b:x" and ":a:b:x" apart; NUL cannot occur in a path.
String identity = task.getProject().getRootDir().getAbsolutePath() + "\0" + task.getPath();
byte[] hash =
MessageDigest.getInstance("SHA-256").digest(identity.getBytes(StandardCharsets.UTF_8));
String fileName = task.getName() + "-" + HexFormat.of().formatHex(hash, 0, 8) + ".txt";
Files.write(configDir.resolve(fileName), lines);
} catch (Exception exc) {
throw new RuntimeException(
Expand Down
5 changes: 5 additions & 0 deletions scip-java/src/test/kotlin/tests/GradleBuildToolTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ class GradleBuildToolTest : BuildToolHarness() {
runs = 2,
)
)
// Regression test: Kotlin compile tasks whose paths are equal in different
// builds of a composite (`:compileKotlin` in the root and included build),
// or equal after sanitizing (`:a-b` and `:a:b`), overwrote each other's
// configs, so only one of their sources was indexed.
add(checkGradleBuild("kotlin-composite", "gradle/kotlin-composite", expectedScipFiles = 4))
add(
checkGradleBuild(
"implementation-deps",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package ab
object Dashed {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package ab
object Nested {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '2.4.0'
}
allprojects {
repositories {
mavenCentral()
}
}
// `scipCompileAll` only runs tasks of the root build.
tasks.named('compileKotlin') {
dependsOn gradle.includedBuild('included').task(':compileKotlin')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '2.4.0'
}
repositories {
mavenCentral()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'included'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package included
object Included {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rootProject.name = 'kotlin-composite'
// `:a-b` and `:a:b` both sanitize to `-a-b-compileKotlin`.
include 'a-b', 'a:b'
// Also has a `:compileKotlin` task.
includeBuild 'included'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package root
object Root {}
Loading