|
1 | 1 | package org.scip_code.scip_java.gradle; |
2 | 2 |
|
3 | 3 | import java.io.File; |
| 4 | +import java.nio.charset.StandardCharsets; |
4 | 5 | import java.nio.file.Files; |
5 | 6 | import java.nio.file.Path; |
6 | 7 | import java.nio.file.Paths; |
| 8 | +import java.security.MessageDigest; |
7 | 9 | import java.util.ArrayList; |
8 | 10 | import java.util.HashSet; |
| 11 | +import java.util.HexFormat; |
9 | 12 | import java.util.List; |
10 | 13 | import java.util.Map; |
11 | 14 | import java.util.Set; |
@@ -271,7 +274,8 @@ private static Set<String> addJavacPlugin(Project project, Object javacPluginDep |
271 | 274 | } |
272 | 275 |
|
273 | 276 | /** |
274 | | - * Dumps the sources and classpath of a Kotlin compile task to {@code kotlin-configs/<task>.txt}. |
| 277 | + * Dumps the sources and classpath of a Kotlin compile task to {@code |
| 278 | + * kotlin-configs/<task>-<hash>.txt}. |
275 | 279 | */ |
276 | 280 | private static void writeKotlinConfig(Task task, String sourceRoot, String targetRoot) { |
277 | 281 | try { |
@@ -317,7 +321,14 @@ private static void writeKotlinConfig(Task task, String sourceRoot, String targe |
317 | 321 |
|
318 | 322 | Path configDir = Paths.get(targetRoot, "kotlin-configs"); |
319 | 323 | Files.createDirectories(configDir); |
320 | | - String fileName = task.getPath().replaceAll("[^A-Za-z0-9]", "-") + ".txt"; |
| 324 | + // Task paths are only unique within one build, and every build of a |
| 325 | + // composite (included builds, buildSrc) writes here, so the build's |
| 326 | + // root directory is part of the identity. Hashing, unlike sanitizing, |
| 327 | + // keeps ":a-b:x" and ":a:b:x" apart; NUL cannot occur in a path. |
| 328 | + String identity = task.getProject().getRootDir().getAbsolutePath() + "\0" + task.getPath(); |
| 329 | + byte[] hash = |
| 330 | + MessageDigest.getInstance("SHA-256").digest(identity.getBytes(StandardCharsets.UTF_8)); |
| 331 | + String fileName = task.getName() + "-" + HexFormat.of().formatHex(hash, 0, 8) + ".txt"; |
321 | 332 | Files.write(configDir.resolve(fileName), lines); |
322 | 333 | } catch (Exception exc) { |
323 | 334 | throw new RuntimeException( |
|
0 commit comments